Sorting floating-point values, and NaN

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Nov 12 12:08:58 PST 2013


On 11/12/13 10:31 AM, tn wrote:
> On Tuesday, 12 November 2013 at 15:56:25 UTC, Andrei Alexandrescu wrote:
>> On 11/12/13 12:54 AM, tn wrote:
>>> ...
>>> An alternative would be to check for nans explicitly.
>>
>> I think NaNs are singular unordered values that make invalid inputs
>> for either sort or isSorted. We should simply add an assert to isSorted.
>
> But sort and isSorted both allow user to provide a custom "less"
> function. What if the user needs NaNs and, for example, wants to sort
> them before all the other values? Then he calls "arr.sort!((a,b) => a <
> b || (isnan(a) && !isnan(b)))" and the code asserts?

I think this is a misunderstanding. I never advocated inserting 
specialized asserts for floating-point numbers. http://goo.gl/3dGGkf 
takes care of that the right way - by making sure that the less 
predicate is sensible for the kind of work isSorted and sort do. That is 
regardless of the nature of the objects being sorted.

Andrei



More information about the Digitalmars-d mailing list