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.
source share