Can you believe that 0.1 + 0.2 is not equal to 0.3 ?!

  • Single Precision Number (binary32): The IEEE single precision floating point standard representation requires 32 bits, which may be represented as numbered from 0 to 31, left to right. The first bit is the sign bit, S, the next 8 bits are the exponent bits, ‘E’, and the final 23 bits are the fraction ‘F’. It reduces the amount of precision achieved.
  • Double Precision Number (binary64): The IEEE double precision floating point standard representation requires 64 bits, which may be represented as numbered from 0 to 63, left to right. The first bit is the sign bit, S, the next 11 bits are the exponent bits, ‘E’, and the final 53 bits (52 bits explicitly stored) are the fraction ‘F’. Double floating point precision are used where high arithmetic precision is required and numbers like -2/19 have to be used.

On most machines today, floats are approximated using a binary fraction, with the numerator using the first 53 bits starting with the most significant bit, and with the denominator as a power of two.

print('{:.30f}'.format(0.1))
OR
print(format(0.1, '.30f'))
0.100000000000000005551115123126
0.200000000000000011102230246252
0.30000000000000004

Note that this is in the very nature of binary floating-point: this is not a bug in Python (or Perl, C, C++, Java, Fortran, and many others), and it is not a bug in your code either. You’ll see the same kind of thing in all languages that support your hardware’s floating-point arithmetic.

1 / 10 ~= J / (2**N)
J ~= 2**N / 10
2**52 <= 2**N // 10 < 2**53
10*(2**52) <= 2**56 < 10(2**53)
THEN
(~> 2**3)*(2**52) <= 2**56 < (~> 2**3)*(2**53)
THEN
(~> 2**55) <= 2**56 < (~> 2**56)
2**56 // 10 = 7205759403792793 (quotient)
2**56 % 10 = 6 (remainder)
7205759403792794 / (2**56) = 3602879701896397 / (2**55)
J < (2**56) / 10 < J + epsilonTHENJ / (2**56) < 1/10
(3602879701896397 * (10**55)) // (2**55)
1000000000000000055511151231257827021181583404541015625

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store