CompareTo behavior for double.NaN and double.NegativeInfinity

I did some statistical operations in C # (.Net 4) with double[] , then I discovered strange behavior with the CompareTo and double.NaN . when i try this code:

 double.NaN.CompareTo(double.NegativeInfinity) //returns -1 

This means double.NaN even smaller than double.NegativeInfinity ! can anyone explain why this is so?

+4
source share
3 answers

CompareTo does not tell you that one thing is smaller than the other. It tells you that one instance precedes (-), follows (+), or is interchangeable with (0) another instance when ordering instances.

Why here really depends on the behavior of developers for primitives in the common language runtime.

IComparable The goal is to streamline type instances. Thus, for NaN, a real double value, it was decided to order it before any other type instance.

Please note that CompareTo is not necessarily the same, in meaning or in purpose, as a number is more or less than operations. CompareTo is designed to provide an order for a variety of values โ€‹โ€‹that a double can take. For instance,

 double.NaN.CompareTo(double.NaN) 

will return 0. But

 double.NaN == double.NaN 

is false. Similarly

 double.NaN.CompareTo(double.NegativeInfinity) 

returns -1 but

 double.NaN < double.NegativeInfinity 

returns false. Thus, the CompareTo method does not mean that mathematically double.NaN is less than double.NegativeInfinity. Less than the operator actually says this is not true. But he says that when ordering values, double.NaN follows first.

Below is a link to the LessThan Operator Double-type documentation. Reading this, as well as the meaning of IComparable.CompareTo side by side should help clarify the difference in what the two methods are trying to express.

+7
source

double.NaN less than negative infinity.

From the metadata information that they explained, for example:

 public const double NegativeInfinity = -1.0 / 0.0 public const double NaN = 0.0 / 0.0; 

From the Double.CompareTo() method;

Compares this instance with the specified double-precision floating-point number and returns an integer indicating whether the value of this instance is less than, equal to, or greater than the value of the specified double-precision floating-point number.

If this instance is not a number (NaN), but the value is a number

Double.CompareTo() method returns a negative integer

Look at this sample (here DEMO );

 void Main() { double a = double.NaN; double b = double.NegativeInfinity; Console.WriteLine(a.CompareTo(b)); } 

Even when we look at the code IL, double.NaN represents from 00 00 00 00 00 00 F8 FF and double.NegativeInfinity represents from 00 00 00 00 00 00 F0 FF ;

 IL_0000: ldc.r8 00 00 00 00 00 00 F8 FF IL_0009: stloc.0 IL_000A: ldc.r8 00 00 00 00 00 00 F0 FF IL_0013: stloc.1 IL_0014: ldloca.s 00 IL_0016: ldloc.1 IL_0017: call System.Double.CompareTo IL_001C: call System.Console.WriteLine 
+6
source

At the end of the day, comparing double.nan numerically makes no sense. But if you have a double list and you want to do something with it, you would like all of them to be at the end of the list, so that you can do all the meaningful work first and so you can stop when you see the first. This is like a list in which some elements have a null value, they end to the end.

A few cases where double.nan occurs:

 Dim d1 as double = 0/0 Dim d2 as double = Double.PositiveInfinity / Double.PositiveInfinity Dim d3 as double = Double.PositiveInfinity / Double.NegativeInfinity Dim d4 as double = Double.PositiveInfinity / Double.PositiveInfinity Dim d5 as double = Double.PositiveInfinity / Double.NegativeInfinity 
0
source

All Articles