Smallest float different from 0.0?

Mark Dickinson dickinsm at gmail.com
Mon Sep 7 11:23:58 EDT 2009


On Sep 7, 3:47 pm, kj <no.em... at please.post> wrote:
> Is there some standardized way (e.g. some "official" module of such
> limit constants) to get the smallest positive float that Python
> will regard as distinct from 0.0?
>
> TIA!
>
> kj

There's sys.float_info.min:

>>> import sys
>>> sys.float_info
sys.float_info(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)
>>> sys.float_info.min
2.2250738585072014e-308

But that only gives you the smallest *normal* positive float, which
is 2**-1022 on most systems.  The smallest positive subnormal value
is usually 2**-1074.  If you want something that would still work
if Python ever switched to using IEEE 754 binary128 format (or some
other IEEE 754 format), then

sys.float_info.min * 2**(1-sys.float_info.mant_dig)

will work.

But on 99.99%+ of systems you'll find Python running on, it's going
to be 2**-1074.

Mark



More information about the Python-list mailing list