Result of ``a is b''

David MacQuigg dmq at gain.com
Fri Mar 19 21:51:00 EST 2004


On Fri, 19 Mar 2004 12:54:56 -0800, Erik Max Francis <max at alcyone.com>
wrote:

>Andrew Koenig wrote:
>
>> I'm pretty sure that IEEE floating point requires NaN to be defined in
>> such
>> a way that if x is NaN, then x == x must yield False.
>
>That's correct.  Python itself doesn't do this, though:
>
>Python 2.3.3 (#1, Dec 22 2003, 23:44:26) 
>[GCC 3.2.3] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
>>>> a = 1e3000
>>>> a
>inf
>>>> a/a
>nan
>>>> n = a/a
>>>> n == n
>True
>>>> 
>
>[Similar things are shown for Python on Solaris 8.]
>

On a Windows system I get different results, depending on whether I
run Python from a command line, or from IDLE !!

Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)]
on win32
...
>>> a = 1e3000
>>> a
1.#INF
>>> a/a
-1.*IND
>>> n = a/a
>>> n == n
False
>>> n
-1.#IND
>>> n + 1
-1.#IND
>>>

Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)]
on win32
...
IDLE 1.0.2      
>>> a = 1e3000
>>> a
1.0
>>> a/a
1.0
>>> n = a/a
>>> n == n
True
>>> n
1.0
>>> n + 1
2.0
>>>

How can IDLE be messing with what happens at the machine level?
Returning an innocent-looking result like 1.0 can hide a serious
problem in the code.  This is not just a display problem.  As you can
see from the last line, subsequent calculations are screwed up (in
IDLE only).

>>> hex(n)
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in -toplevel-
    hex(n)
TypeError: hex() argument can't be converted to hex
>>> 

but I see this is true for any float.  It would be nice to see what is
really there.

-- Dave




More information about the Python-list mailing list