Sorting NaNs

Ned Batchelder ned at nedbatchelder.com
Sat Jun 2 19:50:55 EDT 2018


On 6/2/18 6:16 PM, Richard Damon wrote:
> On 6/2/18 4:51 PM, Ben Bacarisse wrote:
>> Paul Rubin <no.email at nospam.invalid> writes:
>>
>>> Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
>>>> it too will mess up sorting in unpredictable ways. So don't do that.
>>> Hmm.  GHCi 7.4.2:
>>>
>>>      Prelude> let x = 0.0 / 0.0
>>>      Prelude> x
>>>      NaN
>>>      Prelude> x==x
>>>      False
>>>      Prelude> :m Data.List
>>>      Prelude Data.List> sort [1,2,x,4,5]
>>>      [1.0,2.0,4.0,5.0,NaN]
>> But
>>
>>    Prelude Data.List> sort [1,x,2,4,5]
>>    [2.0,4.0,5.0,NaN,1.0]
>>
>> and
>>
>>    Prelude Data.List> sort [1,2,x,4,5,x]
>>    [NaN,1.0,2.0,4.0,5.0,NaN]
>>
>> and
>>
>>    Prelude Data.List> sort [1,2,x,4,5,x,1/0]
>>    [1.0,2.0,4.0,Infinity,NaN,5.0,NaN]
>>
>>> Not sure what to make of this but at least sorting seems to give a
>>> predictable result.
>> I suspect it is predictable if you know the algorithm, but I doubt it's
>> specified nor easily guessable from "outside".
>>
>> (GHCi, version 8.0.2 here)
>>
> The sorting algorithm is almost certainly deterministic (as is the
> comparisons with Nan), so given the same input you will get the same
> output,
Careful, "same input" is vague.  In Python2, object() instances are 
compared based on their id, in other words, their memory location. It 
would be easy to overlook the idea that the layout in memory is part of 
whether two inputs are "the same".

I have no idea what factors determine the result of comparing NaNs.

--Ned.



More information about the Python-list mailing list