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

Surprise with special floating point values

Hi all,

While trying to use simplejson under Python 2.4.3 I have been
investigating the handling of special floating point values and found
that both Python 2.4 and 2.5 return False when comparing a NaN with
itself. Although surprising, I imagine it could also be correct since
NaN is not a number. But is it correct?

Notice the result of comparing c with itself in the following Python
2.5 session (it works the same on Python 2.4.3) under Win32::

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>a = 1e300 * 1e300
a
1.#INF
>>b = 1e66666
b
1.#INF
>>a == b
True
>>c = a - a
c
-1.#IND
>>d = b - b
d
-1.#IND
>>c == d
False
>>c == c
False
>>b == b
True
>>a == a
True
>>a is a
True
>>b is b
True
>>c is c
True
>>d is d
True
>>>
Thanks,

--
Pierre Rouleau

Nov 29 '06 #1
3 1436


On Nov 29, 12:53 pm, prouleau...@gmail.com wrote:
Hi all,

While trying to use simplejson under Python 2.4.3 I have been
investigating the handling of special floating point values and found
that both Python 2.4 and 2.5 return False when comparing a NaN with
itself. Although surprising, I imagine it could also be correct since
NaN is not a number. But is it correct?
And of course it is correct... As NaN does not compare with itself in
floating point: http://en.wikipedia.org/wiki/NaN

Should have read it earlier...

--
Pierre Rouleau
Nov 29 '06 #2
pr*********@gmail.com wrote:
While trying to use simplejson under Python 2.4.3 I have been
investigating the handling of special floating point values
note that JSON doesn't support non-numeric floating point values, as can
be seen by the "number" syntax description on this page:

http://www.json.org/

(and as I pointed out in another thread on this topic, Python relies on
the C library for serialization of floats, so non-numeric floating point
values aren't portable between Python versions either).

</F>

Nov 29 '06 #3


On Nov 29, 1:11 pm, Fredrik Lundh <fred...@pythonware.comwrote:
prouleau...@gmail.com wrote:
While trying to use simplejson under Python 2.4.3 I have been
investigating the handling of special floating point valuesnote that JSON doesn't support non-numeric floating point values, as can
be seen by the "number" syntax description on this page:

http://www.json.org/

(and as I pointed out in another thread on this topic, Python relies on
the C library for serialization of floats, so non-numeric floating point
values aren't portable between Python versions either).

</F>
That's true, but I ran into a problem with simplejson under Python
2.4.3 on Win32, where it fails to serialize 1.0 properly (and I
reported the problem to its author, and the fact that it works fine
under Python 2.5). There is another thread (Inconsistency producing
constant for float "infinity") that talks about differences between
Python 2.5 and earlier versions regarding treatment of non-numeric
floating point values. So, while investigating the simplejson problem,
I though I had found a problem with the handling of NaN (which
obviously is not the case).

- Pierre R.

Nov 29 '06 #4

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

Similar topics

5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
8
by: Lyn | last post by:
I am trying to get my head around the concept of default, special or empty values that appear in Access VBA, depending on data type. The Access Help is not much (help), and the manual that I have...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
24
by: j0mbolar | last post by:
C supports single precision floating point and double precision floating point but does it support fixed floating point? i've read that fixed floating point is more accurate than single precision...
3
by: Mark L Pappin | last post by:
<puts on Compiler Vendor hat> I've recently discovered that our compilers don't make any attempt to handle floating point overflow in add/subtract/ multiply/divide, with the result that...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
14
by: Robert Latest | last post by:
Hi guys, I'm sure this has been beaten to death on this newsgroup, but I can't find it in the CLC FAQ. Consider the following code: -------- double x = some_value_from_somewhere;
7
by: thisismyidentity | last post by:
Hi all, I am trying to predict the behaviour of floating point load and store operations on integer locations. I ve written a small piece of code having some inline assembly code which I am...
15
by: Mukesh_Singh_Nick | last post by:
Why does floating point have a rounding error? How to work around it? For example, the following: flaot f = 1234.12345678F; printf("%2f\n", f) //prints 1234.123413 and
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.