jSh4rk
jSh4rk

Reputation: 55

Python small floats - arithmetic precision

If I run the following code in python 3

1.0 + 1e-16

I get

1.0

as a result. Im wondering why? Assuming the numbers are stored as IEE754 double precision (64 bit) then I have the following representation of these numbers:

Number 1:

Number 1e-16:

In order to add both numbers I have to increase the exponent of the the small number by 50 to fit the exponent of the representation of number 1.0. If I do that the mantissa becomes:

After adding both mantissas my result should be:

and this is not euqal to 1.0. Can someone explain or give me a hint what I'm overlooking?

Upvotes: 3

Views: 143

Answers (1)

Thomas
Thomas

Reputation: 182038

It seems your conversion of 1e-16 to binary is not correct: float.hex(1e-16) produces 0x1.cd2b297d889bcp-54, meaning the (unbiased) exponent is -54 in decimal, not -50. Then the leading 1 drops off the end of the significand and you end up with zero.

Upvotes: 3

Related Questions