NaN support etc.

A

Andreas Beyer

Hi,

How do I find out if NaN, infinity and alike is supported on the current
python platform?
I could do the following:
try:
nan = float('NaN')
have_nan = True
except ValueError:
have_nan = False

Is there an 'official' handle for obtaining this information?
Similar: How do I get the maximum/minimum double for current machine?

Thanks! Andreas

Please, cc me, as I am not on the list. Thanks!
 
?

=?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=

Search for:
+ fpconst / PEP 754
+ Tim Peters IEEE 754 accident

"""what-the-world-needs-now-is-nannanny.py-ly y'rs"" - SB
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Andreas said:
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)
Is there an 'official' handle for obtaining this information?
No.

Similar: How do I get the maximum/minimum double for current machine?

By experimentation, and/or reading vendor documentation.

Regards,
Martin
 
?

=?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=

Martin v. Löwis a écrit :
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
 
I

Ivan Van Laningham

Hi All--

Martin v. Löwis said:
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)


By experimentation, and/or reading vendor documentation.

Something that might help a little is

http://www.pauahtun.org/TYPython/machar.zip

It's C source that you can compile on a unix system, and an .exe for
windows, to probe the limits of the IEEE-754 support on a system. It's
not set up to show you NaN, but by studying the docs on your particular
system you could modify the code to print stuff like that out, I'd
think.

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
 
H

Harry George

Sébastien Boisgérault said:
Martin v. Löwis a écrit : [snip]

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

I use fpconst too. I've been concerned that its source home seems to
wander. Any chance of it being added to the base distro?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,239
Messages
2,571,200
Members
47,840
Latest member
Tiffany471

Latest Threads

Top