Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Surprise with special floating point values

Reply
Thread Tools

Surprise with special floating point values

 
 
prouleau001@gmail.com
Guest
Posts: n/a
 
      11-29-2006
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

 
Reply With Quote
 
 
 
 
prouleau001@gmail.com
Guest
Posts: n/a
 
      11-29-2006


On Nov 29, 12:53 pm, (E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-29-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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>

 
Reply With Quote
 
prouleau001@gmail.com
Guest
Posts: n/a
 
      11-29-2006


On Nov 29, 1:11 pm, Fredrik Lundh <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
floating point problem... floating indeed :( teeshift Ruby 2 12-01-2006 01:16 AM
Equality of floating-point numbers (special case) Robert Latest C Programming 14 04-20-2006 04:12 PM
converting floating point to fixed point H aka N VHDL 15 03-02-2006 02:26 PM
Fixed-point format for floating-point numbers Motaz Saad Java 7 11-05-2005 05:33 PM



Advertisments