Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing NaN and null returns illogic value

Tags:

c#

Why does the following code snippet returns 1:

double i = double.NaN;
double? i2 = null;
i.CompareTo(i2);

From my point of view it makes no sense. An Exception would be more appropriate.

What do you think was the reasoning behind the decision.

like image 570
rudimenter Avatar asked Apr 18 '13 15:04

rudimenter


1 Answers

From the documentation on CompareTo:

The value parameter must be null or an instance of Double; otherwise, an exception is thrown. Any instance of Double, regardless of its value, is considered greater than null.

The value parameter in your example is null. NaN is therefore considered greater than null which is why CompareTo correctly returns 1.

like image 52
DGibbs Avatar answered Oct 11 '22 12:10

DGibbs