Comparisons of incompatible types
John Nagle
nagle at animats.com
Wed Dec 8 23:16:57 EST 2010
On 12/8/2010 7:56 PM, geremy condra wrote:
> On Wed, Dec 8, 2010 at 1:01 PM, John Nagle<nagle at animats.com> wrote:
>> On 12/7/2010 3:59 PM, Mark Wooding wrote:
>>>>
>>>> Exactly one of
>>>>>
>>>>> a > b
>>>>> a = b
>>>>> a < b
>>>>>
>>>>> is true, or an type exception must be raised.
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.
It's not good to break trichotomy.
John Nagle
More information about the Python-list
mailing list