float('nan')==1 -> True
Tim Peters
tim.one at comcast.net
Sat Oct 19 15:20:29 EDT 2002
[Pearu Peterson]
> I've noticed that
>
> float('nan')==float_obj
>
> always returns True whatever is the value of float_obj in Python 1.5.2,
> 2.0.1, 2.1.3, 2.2.1.
> I would have expected False for all float values because Undefined (that
> should be the result of nan==float_obj) is closer to False than True,
> IMHO.
>
> I wonder what is the rationale behind this behaviour? Or is it a bug?
No rationale, and neither bug nor feature: Python knows nothing about NaNs,
and whatever happens on your platform is an accident composed of what kind
of code your C compiler generates, and what your C libraries happen to do.
Python doesn't define any behavior relative to IEEE-754 gimmicks, mostly
because C89 doesn't either, and nobody has cared to endure the pain of
trying to #ifdef their way out of this mess. On my platform,
>>> float('nan')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for float(): nan
>>>
My platform C's strtod() doesn't know what to do with that string; looks
like yours does. Also,
>>> inf = 1e400
That this worked is an accident.
>>> inf
1.#INF
That it displays this particulr string is an accident.
>>> nan = inf - inf
That this worked is an accident.
>>> nan
-1.#IND
That it displays this particulr string is an accident.
>>> nan == 1.0
False
Ditto.
>>> nan == nan
False
Not only an accident, but this one even differs across Python releases on
this platform.
so-if-you-really-really-want-false-upgrade-to-windows<wink>-ly y'rs - tim
More information about the Python-list
mailing list