How to represent the infinite ?

Chris Liechti cliechti at
Thu Jun 20 22:54:58 CEST 2002

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

> 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
> ( 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
>>> -Inf
>>> NaN = struct.unpack('f', '\x7f\xff\xff\x7f')[0]
>>> NaN

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

>>> struct.unpack('>f', '\x00\x00\x00\x01')
>>> struct.unpack('<f', '\x00\x00\x00\x01')
>>> struct.unpack('@f', '\x00\x00\x00\x01')

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

>>> struct.unpack('@f', '\x00\x00\x80\x7f')
>>> struct.unpack('<f', '\x00\x00\x80\x7f')

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


Chris <cliechti at>

More information about the Python-list mailing list