infinity

Chad Netzer cnetzer at mail.arc.nasa.gov
Wed Oct 9 22:43:41 CEST 2002


On Wednesday 09 October 2002 13:09, Gregor Lingl wrote:

> Is there a token or a name to input
> this strange float object,
>   __repr__esented by 1.0#INF
> directly?

Well, you can make a global (or module global), to use as a label:

INF = 1e999999999   # This should always be big enough

This should even be portable to most "mainstream" systems (ie. Not raise an 
exception, coredump, etc.).  Can more knowledgeable people say for sure?

Also, you can (usually) get a NaN out of it, too:

NaN = INF - INF

which has interesting (and important) properties:

NaN == NaN
NaN == 0.0
NaN == 1.0
NaN == <any floating point number>
NaN == INF
NaN + NaN == NaN
NaN - NaN == NaN
NaN + INF == NaN
etc...

Note that Python typically relies on the underlying behavior of the machines 
floating point (rather than enforcing its own, portable implementation), so 
relying on specific floating point behavior isn't always portable.  (It's a 
long standing comp-sci problem that is improving, however)

-- 

Chad Netzer
cnetzer at mail.arc.nasa.gov




More information about the Python-list mailing list