NaN support etc.

Sébastien Boisgérault Sebastien.Boisgerault at gmail.com
Wed May 18 16:55:07 EDT 2005


Martin v. Löwis a écrit :
> Andreas Beyer wrote:
> > How do I find out if NaN, infinity and alike is supported on the
current
> > python platform?
>
> To rephrase Sebastian's (correct) answer: by
>
> 1. studying the documentation of the CPU vendor
> 2. studying the documentation of the compiler vendor, and performing
>    extensive tests on how the compiler deals with IEEE-754
> 3. studying the documentation of the system's C library, reading
>    its source code (if available), and performing extensive tests
>    of the IEEE-754 support in the C libray
> 4. studying Python's source code (you can spare yourself reading
>    documentation because there is none)

That's the theory. Granted, the floating point special
values handling may vary widely across platforms. The
fact that

    float('nan') == float('nan')

may be False (correct) for my Python 2.4 interpreter
(Linux Mandrake 10.x, compiled from the sources) and
True (too bad ..) for my Python 2.3 interpreter (rpm)
for the SAME operating system and computer is rather
unsettling ... at first.

But, practically, I have never found a platform where
the following fpconst-like code did not work:

    import struct
    cast = struct.pack

    big_endian = cast('i',1)[0] != '\x01'
    if big_endian:
       nan = cast('d', '\x7F\xF8\x00\x00\x00\x00\x00\x00')[0]
    else:
       nan = cast('d', '\x00\x00\x00\x00\x00\x00\xf8\xff')[0]

Can anybody provide an example of a (not too old or
exotic) platform where this code does not behave as
expected ?

Cheers,

SB




More information about the Python-list mailing list