473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Portably generating infinity and NaN

What's the best way to portably generate binary floating point infinity
and NaNs? I only know two solutions:

1. Using the fpconst module proposed in IEEE 754, which I believe shifts
bits around.

2. Using an extension module (for example, numarray.ieeespecial will do it).

I thought of using float(Decimal("nan")), but apparently
Decimal.__float__(self) is float(str(self)), which isn't portable.
--
Michael Hoffman
Apr 12 '07 #1
17 2368
Michael Hoffman wrote:
2. Using an extension module (for example, numarray.ieeespecial will do
it).
I guess I could always use ctypes as well, and say, get -inf from
libc.log(ctypes.c_double(0.0)). Although we're venturing away from
portable territory then, since specifying how to load libc will be
different on different platforms.
--
Michael Hoffman
Apr 12 '07 #2
Michael Hoffman wrote:
What's the best way to portably generate binary floating point infinity
and NaNs? I only know two solutions:

1. Using the fpconst module proposed in IEEE 754, which I believe shifts
bits around.

2. Using an extension module (for example, numarray.ieeespecial will do it).

I thought of using float(Decimal("nan")), but apparently
Decimal.__float__(self) is float(str(self)), which isn't portable.
This is what numpy does (translated from the C):

mul = 1e10
inf = mul
tmp = 0.0
while True:
inf *= mul
if inf == tmp:
break
tmp = inf

nan = inf / inf

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Apr 12 '07 #3
Michael Hoffman schrieb:
What's the best way to portably generate binary floating point infinity
and NaNs? I only know two solutions:

1. Using the fpconst module proposed in IEEE 754, which I believe shifts
bits around.

2. Using an extension module (for example, numarray.ieeespecial will do
it).
Use the struct module to pack/unpack a double value. On an IEEE 754
system, this should work - on a non-IEEE-754 system, it's not clear
that +/-inf and NaN actually exist.

Regards,
Martin
Apr 12 '07 #4

MichaelWhat's the best way to portably generate binary floating point
Michaelinfinity and NaNs?

I take it this isn't portable even though it works on Mac, Linux and Solaris
across a number of different Python versions and a couple GCC versions:

% python
Python 2.4.2 (#1, Feb 23 2006, 12:48:31)
[GCC 3.4.1] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>import struct
struct.pack("f", float("NaN"))
'\xff\xff\xff\x7f'
>>struct.pack("f", float("Inf"))
'\x00\x00\x80\x7f'

$ python
Python 2.5 (release25-maint:53536, Jan 23 2007, 18:15:37)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>import struct
struct.pack("f", float("NaN"))
'\x00\x00\xc0\x7f'
>>struct.pack("f", float("Inf"))
'\x00\x00\x80\x7f'

% python
Python 2.6a0 (trunk:54264M, Mar 10 2007, 15:19:48)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>import struct
struct.pack("f", float("Inf"))
'\x7f\x80\x00\x00'
>>struct.pack("f", float("NaN"))
'\x7f\xc0\x00\x00'

(Note the absence of a demonstration on Windows.) Can't the above be
blessed as the One True Way and wormed around in floatmodule.c for those
platforms where float'ing "NaN" or "Inf" doesn't currently work?

Skip
Apr 13 '07 #5
(Note the absence of a demonstration on Windows.) Can't the above be
blessed as the One True Way and wormed around in floatmodule.c for those
platforms where float'ing "NaN" or "Inf" doesn't currently work?
How would you do the worming-around?

Regards,
Martin
Apr 13 '07 #6
>(Note the absence of a demonstration on Windows.) Can't the above be
blessed as the One True Way and wormed around in floatmodule.c for
those platforms where float'ing "NaN" or "Inf" doesn't currently
work?
MartinHow would you do the worming-around?

I don't know. On I was just asking. On unixoid systems I sort of assume
you could add tests to the configure script to detect what worked. If
converting the strings works you're done. If not, maybe Robert Kern's numpy
code could be run in the configure script to generate constants for NaN and
Inf that could be used in floatmodule.c. Windows would probably have to be
hard-coded, but except for 32-bit and 64-bit differences it should be the
same all over, yes?

Skip

Apr 13 '07 #7
sk**@pobox.com wrote:
>(Note the absence of a demonstration on Windows.) Can't the above be
>blessed as the One True Way and wormed around in floatmodule.c for
>those platforms where float'ing "NaN" or "Inf" doesn't currently
>work?

MartinHow would you do the worming-around?

I don't know. On I was just asking. On unixoid systems I sort of assume
you could add tests to the configure script to detect what worked. If
converting the strings works you're done. If not, maybe Robert Kern's numpy
code could be run in the configure script to generate constants for NaN and
Inf that could be used in floatmodule.c. Windows would probably have to be
hard-coded, but except for 32-bit and 64-bit differences it should be the
same all over, yes?
If you're going to change CPython to do this, I think adopting PEP 754,
and using the fpconst module would be better than changing how float()
works when called on string literals. The only thing I don't like about
it is the camelcasing of the functions.

http://www.python.org/dev/peps/pep-0754/
--
Michael Hoffman
Apr 14 '07 #8
Michael Hoffman wrote:
sk**@pobox.com wrote:
> >(Note the absence of a demonstration on Windows.) Can't the
above be
> >blessed as the One True Way and wormed around in floatmodule.c for
>those platforms where float'ing "NaN" or "Inf" doesn't currently
>work?

MartinHow would you do the worming-around?

I don't know. On I was just asking. On unixoid systems I sort of assume
you could add tests to the configure script to detect what worked. If
converting the strings works you're done. If not, maybe Robert Kern's
numpy
code could be run in the configure script to generate constants for
NaN and
Inf that could be used in floatmodule.c. Windows would probably have
to be
hard-coded, but except for 32-bit and 64-bit differences it should be the
same all over, yes?

If you're going to change CPython to do this, I think adopting PEP 754,
and using the fpconst module would be better than changing how float()
works when called on string literals. The only thing I don't like about
it is the camelcasing of the functions.

http://www.python.org/dev/peps/pep-0754/
I notice in the tracker, kousu suggested an alternative design, where
the test functions become part of the float type. So you'd have
float.isnan(), float.isfinite(), etc.

http://sourceforge.net/tracker/?func...70&atid=305470

--
Michael Hoffman
Apr 14 '07 #9
I don't know. On I was just asking. On unixoid systems I sort of assume
you could add tests to the configure script to detect what worked. If
converting the strings works you're done. If not, maybe Robert Kern's numpy
code could be run in the configure script to generate constants for NaN and
Inf that could be used in floatmodule.c. Windows would probably have to be
hard-coded, but except for 32-bit and 64-bit differences it should be the
same all over, yes?
I cannot imagine what the constants would look like. For +/-inf, it
might be possible to write down an expression that causes an overflow.
For NaN, I cannot think of such an expression (also, there are many
possible values that are all not-a-number - which of them should be
denoted by float("NaN")?).

Even if you determine at configure time whether or not the C library
already supports strtod("inf") and even if you then special-case
Windows - what do you do with platforms where neither approach works?

In short, I cannot think of a way to do that portably on all platforms
in C. If there was such a way, it probably would have been implemented
long ago.

Regards,
Martin
Apr 14 '07 #10
sk**@pobox.com wrote:
% python
Python 2.6a0 (trunk:54264M, Mar 10 2007, 15:19:48)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more
information.
>>import struct
>>struct.pack("f", float("Inf"))
'\x7f\x80\x00\x00'
>>struct.pack("f", float("NaN"))
'\x7f\xc0\x00\x00'

(Note the absence of a demonstration on Windows.) Can't the above be
blessed as the One True Way and wormed around in floatmodule.c for
those platforms where float'ing "NaN" or "Inf" doesn't currently work?
How about doing the same in reverse to generate the values on Windows? It
should be pretty portable, otherwise the struct module doesn't work as
advertised:
>>NaN, Inf = struct.unpack("!2f", '\x7f\xc0\x00\x00\x7f\x80\x00\x00')
NaN,Inf
(1.#QNAN, 1.#INF)

This also means you can choose which NaN you generate, although given
that it compares unequal with itself it probably doesn't matter.
Apr 14 '07 #11

MichaelIf you're going to change CPython to do this, I think adopting
MichaelPEP 754, and using the fpconst module would be better than
Michaelchanging how float() works when called on string literals.

But PEP 754 will only work for architectures supporting IEEE 754. I realize
that's the vast majority of systems, but aren't there still a few Crays and
VMS machines out there? (Do those architectures support NaN and Inf?)

Skip
Apr 14 '07 #12
sk**@pobox.com wrote:
MichaelIf you're going to change CPython to do this, I think adopting
MichaelPEP 754, and using the fpconst module would be better than
Michaelchanging how float() works when called on string literals.

But PEP 754 will only work for architectures supporting IEEE 754. I realize
that's the vast majority of systems, but aren't there still a few Crays and
VMS machines out there? (Do those architectures support NaN and Inf?)
Will float("NaN") work on these systems? (I don't know.) I guess it
probably works on some system that isn't IEEE 754.
--
Michael Hoffman
Apr 15 '07 #13
sk**@pobox.com writes:
But PEP 754 will only work for architectures supporting IEEE 754. I realize
that's the vast majority of systems, but aren't there still a few Crays and
VMS machines out there? (Do those architectures support NaN and Inf?)
I wouldn't worry about it. There are Python subsystems (like threads) that
don't work on certain OS's, fine, total portability means not being able to
rely on them, and that's ok. I doubt any Crays are still running, or at least
running any numerical code written in Python. Same for VMS.
Apr 15 '07 #14
Paul Rubin wrote:
I doubt any Crays are still running, or at least
running any numerical code written in Python.
You are wrong.
Same for VMS.
Also wrong.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Apr 16 '07 #15
Paul Rubin wrote:
sk**@pobox.com writes:
>But PEP 754 will only work for architectures supporting IEEE 754. I realize
that's the vast majority of systems, but aren't there still a few Crays and
VMS machines out there? (Do those architectures support NaN and Inf?)

I wouldn't worry about it. There are Python subsystems (like threads) that
don't work on certain OS's, fine, total portability means not being able to
rely on them, and that's ok. I doubt any Crays are still running, or at least
running any numerical code written in Python. Same for VMS.
Think again.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 16 '07 #16

MichaelWill float("NaN") work on these systems? (I don't know.) I
Michaelguess it probably works on some system that isn't IEEE 754.

My thought was that in configure you could test if strtof("NaN") and
strtof("Inf") worked. If not, calculate the necessary bit patterns at that
point and use conditional compilation in floatmodule.c to generate them.

Skip

Apr 16 '07 #17
Paul Rubin wrote:
sk**@pobox.com writes:
>But PEP 754 will only work for architectures supporting IEEE 754. I realize
that's the vast majority of systems, but aren't there still a few Crays and
VMS machines out there? (Do those architectures support NaN and Inf?)

I wouldn't worry about it. There are Python subsystems (like threads) that
don't work on certain OS's, fine, total portability means not being able to
rely on them, and that's ok. I doubt any Crays are still running, or at least
running any numerical code written in Python. Same for VMS.
Do these systems provide infinities or NaN? If so, then fpconst could be
extended to include them.
--
Michael Hoffman
Apr 16 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: MrPugh | last post by:
Hi! I need to initialize a double with infinity (inf or 1.#INF). Now in Windows you can do that by simply assigning it this way: double x = numeric_limits<double>::infinity(); The problem...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
2
by: Russell Smith | last post by:
Timestamps support infinity. However if appears dates do not. When timestamps are cast to dates, there is no output. Is this an acceptable option or not? Below are a number of examples...
5
by: Peter Hansen | last post by:
I'm investigating a puzzling problem involving an attempt to generate a constant containing an (IEEE 754) "infinity" value. (I understand that special float values are a "platform-dependent...
2
by: Pierre Rouleau | last post by:
Hi all, When using Python 2.4.x on a Win32 box, marshal.loads(marshal.dumps(1e66666)) returns 1.0 instead of infinity as it should and does under Python 2.5 (also running on Win32 ). This...
12
by: horacius.rex | last post by:
Hi, I have a code that in some part of the program calculates 1/x for a lot of different x's. About 1 of 100 times x is equal to zero, so when I print the result I obtain inf. I wonder if there...
5
by: westhood | last post by:
In my program I must have some variables which values are infinity. It means the variable must be bigger than any integer. And if we add some to it, its value should still be infinity. I try...
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
13
by: kimiraikkonen | last post by:
Hello, I have an aritmetic calculation like this: First note that: i need a "timer" to get the value for value3. (however removing "timer" didn't differ) Dim value1 As Long Dim value2 As...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.