Prevent double NAN or Infinity conditions
- Certain operations in .Net can give a double variable the values "NAN" or "1.#INF". That's great flexibility but I would rather an exception be thrown any time a double is given one of these values. In my program these values should not exist, and when it does it inevitably leads to an error down the road. I keep trying to put in checks using double.IsNAN or double.IsInfinity but certain things keep slipping by. So there is any setting in .Net to just make it always invalid for a double to be set to these values?
(Edit: also annoying that I think you have to use two checks: IsNaN and IsInfinity to catch these two possibilities. If anyone knows of one check that catches any double that is not a real numeric value it would be a help.)
Réponses
- I have not tested it, but what about something like this.
Class myDouble Private _value As [Double] Public Sub New(ByVal value As [Double]) If [Double].IsInfinity(value) OrElse [Double].IsNaN(value) Then Throw New NotImplementedException() End If _value = value End Sub Public Shared Widening Operator CType(ByVal value As [Double]) As myDouble Return New myDouble(value) End Operator Public Shared Widening Operator CType(ByVal value As myDouble) As [Double] Return value._value End Operator End Class
- Proposé comme réponseMaDFroG20091013 mardi 10 novembre 2009 07:50
- Marqué comme réponseTekito mardi 10 novembre 2009 18:20
Toutes les réponses
- You might want to check out this Microsoft article for ideas:
Test for NaN Correctly
Mike McIntyre's .Net Journal
Mike McIntyre Senior Developer / Partner aZ Software Developers- Proposé comme réponseMaDFroG20091013 mardi 10 novembre 2009 07:50
- I have not tested it, but what about something like this.
Class myDouble Private _value As [Double] Public Sub New(ByVal value As [Double]) If [Double].IsInfinity(value) OrElse [Double].IsNaN(value) Then Throw New NotImplementedException() End If _value = value End Sub Public Shared Widening Operator CType(ByVal value As [Double]) As myDouble Return New myDouble(value) End Operator Public Shared Widening Operator CType(ByVal value As myDouble) As [Double] Return value._value End Operator End Class
- Proposé comme réponseMaDFroG20091013 mardi 10 novembre 2009 07:50
- Marqué comme réponseTekito mardi 10 novembre 2009 18:20
jgalley - that is an interesting solution. I'm still surprised there isn't a well-known solution (other than repeatedly using IsNAN and IsInfinity) since I imagine there are lots of routines out there that aren't designed to handle numbers of these special types.
Do you know what the performance ramifications are of using CType? If myDouble gets converted everytime it's used in an arithmetic operation (thinking about large loops with lots of calculations), will that have a big impact? Since it is just returning the private _value I would hope not, but I don't know much about implementing CType. (Edit: and I wonder if there would be a performance improvement if using a structure instead of class...)
- It's hard to tell the performanceof using CType, but i think the answer would be yes.
From what I've read, CType can have a significant performance impact. I don't know if there is any difference between existing CType's and user-created implementations like the above. I will give galley the answer, but when numbers are concerned I think calculation performance could be a big issue (at least for what I'm doing). I'll try to test it out if I ever find the time.

