enter image description here  r1 and r2 both are assigned 0.05 initially. If r1 is incremented by 0.01 in this way "r1 = r1 +0.01" we have 0.060000000000000005 as answer not 0.06.


2 Answers 2


This is the foundation of computer arithmetic, when dealing with fractional values. (https://en.wikipedia.org/wiki/Floating-point_arithmetic.)

The key reason is that numbers are represented in the binary base rather than decimal. Though $0.06$ is a short number in your world, it cannot be represented exactly by a computer.

But there is nothing shocking: try and expand $1/3$ in the decimal notation, and tell us when you are done... :-)

The next topic to know about is "rounding": https://en.wikipedia.org/wiki/Rounding.


As @gnashed729 remarks, this is how floating-point arithmetic works, and it is not specific to Python.

It is all clearly explained in the Python manual:

  • Python (like most programming languages) interprets a number such as 0.3 as a floating-point number, using the computer's built-in support for floating-point arithmetic, if present;
  • computers represent floating-point number as binary fractions, not decimal fractions;
  • decimal fractions cannot be represented exactly as binary fractions; for instance, 0.1 (decimal) is 0.0001100110011001100110011001100110011001100110011... (binary) (this example is from the Python manual);
  • as a consequence, you already have a rounding error once you write the number 0.3 down; more rounding errors will occur once you start to do arithmetic with these numbers.

To calculate with exact decimal fractions, you may want to use the decimal module.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.