This function checks that the number is comparable.
This can be very important for values โโused as a key to a sort function or used in a search. The comparison used in sorting assumes that if A <B is true, then B <A will be false. If A or B is an incomparable value, both of these statements will be false.
Technically, what type is required is called strict weak order .
An item in the collection that has an incomparable value cannot be found. A list containing an incomparable value is not sortable. In addition, an optimized implementation may exit the array, which will be sorted and begin to corrupt memory or never complete.
The only disparate value that I know for the double is NaN. As others have pointed out, NaN will return false if used as the parameter whatIsIt()
. If NaN is a possible value for the numbers you are comparing, then you have to handle it, or bad things can happen.
Problems with std :: map and NaN are mentioned in this wikipedia article.
You can build a comparison to sort NaN at a given place in the list, but you cannot do this only with the built-in operators. Instead, you would do something like
if ( A < B ) then return -1; else if ( B < A ) then return 1; else return whatIsIt(A) - whatIsIt(B);
Aside, in SQL, NULL also cannot be compared in compatible implementations.
The secret is why isnan () was not used unless it was an interview question or something like that.
source share