0

i was suprised when value "NaN" comming into check of below code was recognized as correct value for double parse :O and went into if and nto else... Do you know why is that and how to secure my code better to avoid such situation. Value should go to if only if number can be converted to double.

 If Double.TryParse(array(3), doubleitem) Then

                array(3) = doubleitem
            Else
                array(3) = Nothing
            End If

enter image description here

Arie
  • 3,041
  • 7
  • 32
  • 63
  • when array(3) value was "NaN" it went to the line: array(3) = doubleitem – Arie May 26 '15 at 10:03
  • i have to for specific reasons only one i cant figure out that Double.Try parse recognize some strings as cooreect values for Parse. How to avoid such situation? I thought Double.TryParse will be enough but seems not. – Arie May 26 '15 at 10:06
  • not it receivs true in my case. Array is string type – Arie May 26 '15 at 10:06
  • Try && doubleitem <> Double.NaN ? – Kevin Hendricks May 26 '15 at 10:08
  • I see, it returns true if i use `InvariantCulture`: `Double.TryParse("NaN",NumberStyles.None, CultureInfo.InvariantCulture, doubleitem)`. In my culture(de-de) ` `doubleitem.ToString` returns `"n. def."`. – Tim Schmelter May 26 '15 at 10:08
  • see main post for picture, why it happens? :O – Arie May 26 '15 at 10:09

3 Answers3

3

You can try-parse it using InvariantCulture(since other languages use different names for NaN) and then check explicitly if it's <> Double.NaN:

If Double.TryParse(array(3), NumberStyles.Float, CultureInfo.InvariantCulture, doubleitem) _
    AndAlso Not double.IsNaN(doubleitem) Then
    ' ... '
Else
    ' ... '
End If

You have to use Double.IsNan to check if a number is a double, you cannot compare it.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
3

This is not a misbehavior of Double.TryParse, but a Misunderstanding on your side.
The Double structure actually contains a constant called NaN, that represents a value that is not a number. So when you do a Double.TryParse on the string "NaN", your result is Double.NaN which is a valid value for double.

Change your condition to this

If Double.TryParse(array(3), doubleitem) andalso not Double.IsNaN(doubleitem) Then
     array(3) = doubleitem
Else
     array(3) = Nothing
End If

and everything should work.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • is there any other situation which could be added to avoid similar situation or its enough? – Arie May 26 '15 at 10:17
  • @StackUser: using the correct datatype in the first place. Why have you strings like `NaN` in an array that stores numbers? – Tim Schmelter May 26 '15 at 10:24
  • I don't think so, I've tried by using a simple [.net fiddle](https://dotnetfiddle.net/bh81Zx) to test for other Double constants but they didn't seem to be passing the Double.TryParse. – Zohar Peled May 26 '15 at 10:24
0

Three correct solutions are:

Console.WriteLine( 0/zero = Double.NaN )
Console.WriteLine( 0/zero <> Double.NaN )
Console.WriteLine( Double.IsNaN(0/zero) )

0/zero being your doubleitem.

https://msdn.microsoft.com/en-us/library/bb264491.aspx

Kevin Hendricks
  • 785
  • 1
  • 8
  • 36