0

In Python, if we code print(55 % 11) we get 0. If we code print(float(55) % float(11)) the answer will be 0.0. However, with both print(5.5 % 1.1) and print(float(5.5) % float(1.1)) the output (on my machine) is 1.0999999999999996. On paper, if we do the division, with 5.5 as the dividend, 1.1 as the divisor, 5.0 will be the quotient and 0.0 will be the remainder. My question is why Python doesn't return 0.0 but returns 1.0999999999999996 (on my device) as the remainder of 5.5 % 1.1 or float(5.5) % float(1.1)?

plpm
  • 539
  • 3
  • 12
  • 1
    You'll want to read [Is floating point math broken?](https://stackoverflow.com/q/588004/13843268) though I don't think this should be closed as a duplicate. – sj95126 Dec 02 '22 at 20:04
  • 1
    `1.1` is not, as you might expect, the rational number 11/10. It's a fraction (whose denominator is a power of 2) that's close to, but not exactly, 11/10. – chepner Dec 02 '22 at 20:07

0 Answers0