3

Can someone please explain in detail why in Python 2.7

10.3 % 2.5 => 0.3

and in Python 3.0 prints 0.3000000000000007 (probably due to representation issues)

And, moreover, 10.3 % 2.5 - 0.3 prints 7.216449660063518e-16 in both Pythons.

The error I can imagine that is due to float representation, but why the change in print behavior and what can be done to avoid it when converting programs from 2 to 3?

Xexeo
  • 302
  • 1
  • 12

1 Answers1

0

A solution for your problem is to use round so you will always be guaranteed pre-determined accuracy of your floats. So your equation would become

round(10.3 % 2.5, 1)

Which will in all versions return 0.3.

Tymoteusz Paul
  • 2,732
  • 17
  • 20