Exact integer-valued floats

Hans Mulder hansmu at xs4all.nl
Fri Sep 21 17:04:14 EDT 2012


On 21/09/12 22:26:26, Dennis Lee Bieber wrote:
> On 21 Sep 2012 17:29:13 GMT, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> declaimed the following in
> gmane.comp.python.general:
> 
>>
>> The question is, what is the largest integer number N such that every 
>> whole number between -N and N inclusive can be represented as a float?
>>
> 	Single precision commonly has 7 significant (decimal) digits. Double
> precision runs somewhere between 13 and 15 (decimal) significant digits 
> 
>> If my tests are correct, that value is 9007199254740992.0 = 2**53.

The expression 2 / sys.float_info.epsilon produces exactly that
number.  That's probably not a coincidence.

> 	For an encoding of a double precision using one sign bit and an
> 8-bit exponent, you have 53 bits available for the mantissa.

If your floats have 64 bits, and you use 1 bit for the sign and 8 for
the exponent, you'll have 55 bits available for the mantissa.

> This
> ignores the possibility of an implied msb in the mantissa (encodings
> which normalize to put the leading 1-bit at the msb can on some machines
> remove that 1-bit and shift the mantissa one more place; effectively
> giving a 54-bit mantissa).

My machine has 64-bits floats, using 1 bit for the sign, 11 for the
exponent, leaving 52 for the mantissa.  The mantissa has an implied
leading 1, so it's nominally 53 bits.

You can find this number in sys.float_info.mant_dig

> Something like an old XDS Sigma-6 used
> non-binary exponents (exponent was in power of 16 <> 2^4) and used
> "non-normalized" mantissa -- the mantissa could have up to three leading
> 0-bits); this affected the decimal significance...

Your Sigma-6 must have sys.float_info.radix == 16 then.


Hope this helps,

-- HansM



More information about the Python-list mailing list