Home > Software engineering >  Python small floats - arithmetic precision
Python small floats - arithmetic precision

Time:10-29

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:

  • sign: 0
  • exponent: 01111111111 --> 1023 as decimal number
  • significand: 0...0

Number 1e-16:

  • sign: 0
  • exponent: 01111001101 --> 973 as decimal number
  • significand: 0010000000111010111110011110111001110101011000010110

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:

  • 0.0...0100

After adding both mantissas my result should be:

  • sign: 0
  • exponent: 01111111111
  • significand: 0000000000000000000000000000000000000000000000000100

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

CodePudding user response:

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.

  • Related