[Python-Dev] NaN / Infinity in Python

2007-06-07 Thread Armin Ronacher
Hi,

It's one of those "non issues" but there are still some situations where you
have to deal with Infinity and NaN, even in Python. Basically one the problems
is that the repr of floating point numbers is platform depending and sometimes
yields "nan" which is not evaluable. It's true that eval() is probably a bad
thing but there are some libraries that use repr/%r for code generation and it
could happen that they produce erroneous code because of that. Also there is no
way to get the Infinity and NaN values and also no way to test if they exist.

Maybe changing the repr of `nan` to `math.NaN` and `inf` to `math.Infinity` as
well as `-inf` to `(-math.Infinity)` and add that code to the math module (of
course as a C implementation, there are even macros for testing for NaN 
values)::

Infinity = 1e1
NaN = Infinity / Infinity

def is_nan(x):
return type(x) is float and x != x

def is_finite(x):
return x != Infinity

Bugs related to this issue:

 - http://bugs.python.org/1732212 [repr of 'nan' floats not parseable]
 - http://bugs.python.org/1481296 [long(float('nan'))!=0L]

Regards,
Armin

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] NaN / Infinity in Python

2007-06-07 Thread Josiah Carlson

Armin Ronacher <[EMAIL PROTECTED]> wrote:
> It's one of those "non issues" but there are still some situations where you
> have to deal with Infinity and NaN, even in Python. Basically one the problems
> is that the repr of floating point numbers is platform depending and sometimes
> yields "nan" which is not evaluable. It's true that eval() is probably a bad
> thing but there are some libraries that use repr/%r for code generation and it
> could happen that they produce erroneous code because of that. Also there is 
> no
> way to get the Infinity and NaN values and also no way to test if they exist.
> 
> Maybe changing the repr of `nan` to `math.NaN` and `inf` to `math.Infinity` as
> well as `-inf` to `(-math.Infinity)` and add that code to the math module (of
> course as a C implementation, there are even macros for testing for NaN 
> values)::

That would work for eval(repr(x)), but it fails for float(repr(x)).  I
believe this particular issue has been brought up before, as well as the
particular solution, but I can't remember the final outcome.


> Infinity = 1e1

Has the storage of infinity in .pyc files been fixed?  For a while it
was broken.

> NaN = Infinity / Infinity
> 
> def is_nan(x):
> return type(x) is float and x != x
> 
> def is_finite(x):
> return x != Infinity

you mean

def is_finite(x):
return x not in (Infinity, -Infinity)


 - Josiah

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com