max / min / smallest float value on Python 2.5
Mark Dickinson
dickinsm at gmail.com
Sun Feb 7 04:55:53 EST 2010
On Feb 7, 12:52 am, duncan smith <buzz... at urubu.freeserve.co.uk>
wrote:
> import platform
> if platform.architecture()[0].startswith('64'):
> TINY = 2.2250738585072014e-308
> else:
> TINY = 1.1754943508222875e-38
As Christian said, whether you're using 32-bit or 64-bit shouldn't
make a difference here. Just use the first TINY value you give.
> I'm not 100% sure how reliable this will be across platforms. Any ideas
> about the cleanest, reliable way of uncovering this type of information?
In practice, it's safe to assume that your 2.225....e-308 value is
reliable across platforms. That value is the one that's appropriate
for the IEEE 754 binary64 format, and it's difficult these days to
find CPython running on a machine that uses any other format for C
doubles (and hence for Python floats).
The smallest positive *normal* number representable in IEEE 754
binary64 is exactly 2**-1022 (or approximately
2.2250738585072014e-308). The smallest positive *subnormal* number
representable is exactly 2**-1074, or approximately
'4.9406564584124654e-324'. (Subnormals have fewer bits of precision
than normal numbers; it's the presence of subnormals that allows for
'gradual underflow'.) Some machines can/will treat subnormal numbers
specially for speed reasons, either flushing a subnormal result of a
floating-point operation to 0, or replacing subnormal inputs to an
floating-point operation with 0, or both. So for maximal portability,
and to avoid numerical problems, it's best to avoid the subnormal
region.
> The precise issue is that I'm supplying a default value of
> 2.2250738585072014e-308 for a parameter (finishing temperature for a
> simulated annealing algorithm) in an application. I develop on
> Ubuntu64, but (I am told) it's too small a value when run on a Win32
> server. I assume it's being interpreted as zero and raising an
> exception.
This is a bit surprising. What's the precise form of the error you
get? Do you still get the same error if you replace your TINY value
by something fractionally larger? (E.g., 2.23e-308.)
--
Mark
More information about the Python-list
mailing list