Exact integer-valued floats

Nobody nobody at nowhere.com
Sat Sep 22 06:19:33 EDT 2012


On Fri, 21 Sep 2012 15:23:41 -0700, Paul Rubin wrote:

> Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
>> Have I got this right? Is there a way to work out the gap between one
>> float and the next?
> 
> Yes, 53-bit mantissa as people have mentioned.  That tells you what ints
> can be exactly represented.  But, arithmetic in some situations can have a
> 1-ulp error.  So I wonder if it's possible that if n is large enough, you
> might have something like n+1==n even if the integers n and n+1 have
> distinct floating point representations.

Not for IEEE-754. Or for any sane implementation, for that matter. OTOH,
you can potentially get n != n due to the use of extended precision for
intermediate results.

For IEEE-754, addition, subtraction, multiplication, division, remainder
and square root are "exact" in the sense that the result is as if the
arithmetic had been performed with an infinite number of bits then rounded
afterwards. For round-to-nearest, the result will be the closest
representable value to the exact value.

Transcendental functions suffer from the "table-maker's dilemma", and the
result will be one of the two closest representable values to the exact
value, but not necessarily *the* closest.




More information about the Python-list mailing list