max / min / smallest float value on Python 2.5

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Feb 6 21:09:53 EST 2010


On Sun, 07 Feb 2010 00:52:48 +0000, duncan smith wrote:

> Hello,
>        I'm trying to find a clean and reliable way of uncovering
> information about 'extremal' values for floats on versions of Python
> earlier than 2.6 (just 2.5 actually).  I don't want to add a dependence
> on 3rd party modules just for this purpose.  e.g. For the smallest
> positive float I'm using,

Assuming that your architecture is based on binary floats:

>>> x = 1.0/2
>>> while 0.0 + x != 0.0:
...     smallest = x
...     x /= 2.0
...
>>> smallest
4.9406564584124654e-324

which is the smallest number that can be distinguished from zero on my 
system.

If you're running on some weird platform with non-binary floats (perhaps 
a Russian ternary mainframe, or an old supercomputer with decimal floats) 
then you're on your own.

I calculated this using Python 2.5. In 2.6, I see this:

>>> sys.float_info
sys.floatinfo(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, 
min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, 
mant_dig=53, epsilon=2.2204460492503131e-16, radix=2, rounds=1)

So there's obviously a difference between how I calculate the smallest 
number and what Python thinks. The reason for this is left as an exercise.



-- 
Steven



More information about the Python-list mailing list