Comparisons of incompatible types
Terry Reedy
tjreedy at udel.edu
Thu Dec 9 03:36:53 EST 2010
On 12/9/2010 2:58 AM, Steven D'Aprano wrote:
> On Wed, 08 Dec 2010 20:16:57 -0800, John Nagle wrote:
>
>> Here's an example where this issue produces invalid results in
>> Python.
>>
>> >>> NaN = float("nan")
>> >>> arr = [1.0, 4.0, 3.0, 2.0, 5.0, NaN, 6.0, 3.0, NaN, 0.0, 1.0, 4.0,
>> 3.0, 2.0, 5.0, NaN, 6.0, 3.0, NaN, 0.0]
>> >>> sorted(arr)
>> [0.0, 0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 5.0, nan, 5.0,
>> 6.0, nan, 4.0, nan, 6.0, nan]
>>
>> The sorted numerical values aren't in order. Note the 4.0 near the end,
>> after the 6.0. "sort" has failed because it assumes that a< b and b<
>> c implies a< c. But that's not a valid assumption here.
This is transitivity.
>> It's not good to break trichotomy.
I believe that is that exactly one of <,=.> are true.
--
Terry Jan Reedy
More information about the Python-list
mailing list