473,395 Members | 1,652 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,395 software developers and data experts.

Inconsistent float behaviour on "inf" - current state?

Hello,

I've found another inconsistency, and looking through the
list archives I can find mentions of funky stuff like

print float('inf')

giving

Infanity

with Python 1.5.2 on Solaris 7 in 2001, and a couple closed bug
reports on why "float('inf')" isn't supported everywhere, and I
understand the underlying problem:

libc simply isn't that good - never mind standard - with regard to floats.

However, seeing how Python does a great job with long ints
by using something better than standard libc, I fail to see how
reliance on libc should be a reason not to provide at least consistency:

Why does conversion from a long constant not use the same
library function that conversion from a long string would use?

a = float(1234....298299300) -> OverflowError: long int too large
to convert to float
a = float('1234....298299300') -> inf, which led to silly errors in
SQL (table has no column 'inf')

a = inf -> NameError: name 'inf' is not defined
a = float('inf') -> inf

Now, I could understand how that may need some changes on lexing
the source code, so I'm not actually opening more bugs having
found workarounds for my own troubles here (quality guys insisting
on entering silly numbers, hah, try that with a 50 character input
field limit!).

Still, I'm interested to hear what the current state of affairs is
on using possibly inconsistent libc for floats?

Bye,

Peter Knörrich

Jun 27 '06 #1
1 2319
Peter Knörrich wrote:
.... why "float('inf')" isn't supported everywhere, and I
understand the underlying problem:
libc simply isn't that good - never mind standard - with regard to floats.

I think you believe there is an underlying model of floating point
numbers that all machines share. That is not true. Many systems
(even those that support IEEE 754) make distinct choices about what
particular bit patterns in memory represent what particular values.

You can take a pickle of a data structure in CPython on one machine,
carry it to a different machine with a different CPU & FPU and a
different OS, and reasonably expect that the unpickled data structure
running on the CPython on the other machine will produce essentially the
same value. That is _amazing_. You might not have the same number
of bits of precision, you might not have the same range of exponents,
and the base of the exponent might even be different (well, ... perhaps
nobody is running Python on 360s -- I may exaggerate here). This is not
accomplished by lots of clever coding of distinct implementations across
the range of machines CPython runs on. It is accomplished by staying
close to the middle of the road in the C89 standard. And it breaks down
for infinities and the various denormals and NaNs that you can at least
detect in C99.

C89, the standard we go by for CPython, provides pretty good support
for floating point numbers in "reasonable" ranges with "reasonable"
amounts of precision. That is to say, you can calculate with C doubles
and get pretty similar results for the same C program running on quite
different compilers, operating systems, and hardware _if_ you "walk the
middle of the road" (staying away from rough edges, boundary values,
things like calculating sin(1e300) [trigs work better w/in -2pi to 2pi]
and the like. C89 does not mention the Not-A-Number group at all, and
only says that infinity is something that an "implementation may
support." That "may" means that a C89 compiler is free to do nothing
for Infinities, and even if it does something, it it doesn't have to
behave like some other C89 compiler's version of what to do with
Infinities. So it is no wonder that values like plus and minus
infinity "behave funny" across different CPython systems.

In the eighties (when C89 was being worked on), it was not clear whether
IEEE 754 would be _the_ standard or _a_ standard for floating point. It
would have been pointless to force all C implementers (and they included
Lifeboat Systems who did C compilers for Z80s as well as Cray Research,
who did C compilers for slightly larger machines) to specify how to make
positive and negative zeroes, how and when to produce "denormalized"
numbers, how to produce positive and negative infinity (and whether they
are distinct from each other), how create the various signaling and
quiet NaNs when may systems did not have IEEE 754 floating point
hardware.

C is kind of a "glorified assembler," and (as such) it reveals the
hardware it works on. That is why the C expression ((-7) % 4) can be
1 or -1; different hardware solves that problem differently, and C
doesn't want to slow down all divisions on some hardware just to get
a consistent result. If you stick to positive numbers for division,
both kinds of hardware get the same result, and the programmer can
put the test in if he needs it. It would not be in the spirit of C
to dictate a floating point behavior that would require a lot of
code generated on each operation, and without common hardware, the
infinity / denormal / NaN behavior would mean code at every floating
point operation. The C standards body was not interested in that work.

--Scott David Daniels
sc***********@acm.org
Jun 27 '06 #2

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

Similar topics

10
by: Boštjan Jerko | last post by:
Hello ! I need to know if the result of math formula is nan (Not a number). How can I do that? Thanks, B.
6
by: asmirnov1234567890 | last post by:
Hi my python 2.3.4 for windows refuse to execute line float("NaN"). It says: >>> float("NaN") Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid literal for...
24
by: hjbortol | last post by:
Hi! Is the expression "a >= b" equivalent to "a - b >= 0" in C/C++? Is this equivalence an IEEE/ANSI rule? Or is this machine/compiler dependent? Any references are welcome! Thanks in...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
0
by: JH | last post by:
I can install an 'INF' file by just right clicking and choosing 'Install ' option. How can I do this programmatically in VB.NET? Thanks
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
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...
0
by: Andreas Schmitt | last post by:
I wrote a small timer class for use in a graphics engine I am working on for teaching myself. The small time based animations I tried with it seem to work fine but displaying the frame rate I...
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?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.