473,804 Members | 3,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1454


On Nov 29, 12:53 pm, prouleau...@gma il.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*********@gma il.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...@python ware.comwrote:
prouleau...@gma il.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
3753
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 using the Debug-Version of the program, but it fails (or leads to false results) if we are using the Release-Version. After a long debugging session we found out, that our program was working correctly, but the floating point processing...
8
10413
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 is not much help here either. Googling has given me a little help. This is my current understanding -- I would appreciate any comments or corrections... "" -- this means an empty string when applied to String data type, and also to Variant...
687
23778
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 believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is basically the same. in C there arent the simplest things present like constants, each struct and enum...
24
2264
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 floating point when dealing with dollars and cents.
3
3801
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 programmers who don't range- check their data can e.g. multiply two very tiny values and end up with a very large one. This is (AFAICT) quite fine according to the Standard but is certainly a QoI issue and I'd like to raise ours. I believe that it's...
15
3935
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 the EPSILON? I know in float.h a FLT_EPSILON is defined to be 10^-5. Does this mean that the computer cannot distinguish between 2 numbers that differ by less than this epsilon?
14
2620
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
2211
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 describing here. ======================================================== #include<stdio.h> #include<stdlib.h>
15
8064
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
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.