How to represent the infinite ?

Chris Liechti cliechti at gmx.net
Thu Jun 20 16:54:58 EDT 2002


Chris Barker <Chris.Barker at noaa.gov> wrote in
news:3D121A29.A733609F at noaa.gov: 

> Christophe Delord wrote:
>> There is another simple solution. Just use a float that is bigger
>> than any 64-bit float. For example 10^1000. This float has a special
>> encoding meaning +oo ! 
> 
> this is part of the IEEE 754 standard for floating point computation
> (http://cch.loria.fr/documentation/IEEE754/) It is not guaranteed to
> be implimented by Python (someone please correct me if I'm wrong).
> whether this works is a function of the machine, compiler and library
> used to compile Python. I'm not sure how common it is, but if you want
> your code portable, it may not be reliabel on all machines, which is
> too bad, because IEEE 754 is pretty darn good standard.
> 
> BTW: I'd love to see literals for Inf, -Inf and NaN in Python.

do it yourself ;-)
>>> Inf = struct.unpack('f', '\x00\x00\x80\x7f')[0]
>>> Inf
1.#INF
>>> -Inf
-1.#INF
>>> NaN = struct.unpack('f', '\x7f\xff\xff\x7f')[0]
>>> NaN
1.#QNAN

don't know if it works anywhere but it does on Py2.2 win32.

BTW:
>>> struct.unpack('>f', '\x00\x00\x00\x01')
(1.4012984643248171e-045,)
>>> struct.unpack('<f', '\x00\x00\x00\x01')
(2.350988701644575e-038,)
>>> struct.unpack('@f', '\x00\x00\x00\x01')
(2.350988701644575e-038,)

so the format code '<' is same as '@', little endian which is correct on my 
intel P3 but:

>>> struct.unpack('@f', '\x00\x00\x80\x7f')
(1.#INF,)
>>> struct.unpack('<f', '\x00\x00\x80\x7f')
(3.4028236692093846e+038,)

huh? they're no longer the same for special values of floats??

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list