[ACCEPTED]-Equality with Double.NaN-nan

Accepted answer
Score: 61

Perhaps you are looking for the IsNaN static 1 function?

Try something like this:

if (!Double.IsNaN(Price_Foreign))
   output.Append(String.Format("{0,-10:C} USD",Price_Foreign));
Score: 36

The IEEE 754 floating point standard states that comparing NaN with NaN 11 will always return false. If you must do this, use 10 Double.IsNaN().

But, this isn't the best way to do what 9 you're trying to do. Doubles are NOT precise, and 8 you're using them to represent prices here. I'm 7 betting that at some point, you're going 6 to want to compare prices for equality, too. That's 5 not going to work, because you can't rely on floating point equality.

You should really 4 look into using some integer type for these 3 values (that supports equality comparison) rather 2 than trying to use doubles. Doubles are 1 for scientific problems; not for finance.

Score: 9

Double.NaN is not equal to anything, not even itself.

See 6 the Double.NaN Field in the .NET Framework Class Library 5 documentation:

Use IsNaN to determine whether 4 a value is not a number. It is not possible 3 to determine whether a value is not a number 2 by comparing it to another value equal 1 to NaN.

Score: 8

As background information: what the IsNaN() method 1 does is return v != v;

More Related questions