Ask Your Question

Revision history [back]

If you replace print(i,r,up) with print(i,r-up) you will get:

1 -0.400000000000000
2 -0.300000000000000
3 -0.200000000000000
4 -0.100000000000000
5 -1.66533453693773e-16
6 0.0999999999999998

So, as you can see, the two floating-point numbers are not equal and differ by something very small.

The reason is that 0.1 looks exact in base 10, but in base 2 this number takes infinitely many digits. As floating-point numbers have finite precision, the representation of 0.1 is base 2 is only approximate, see:

sage: (0.1).exact_rational()
3602879701896397/36028797018963968
sage: (0.1).sign_mantissa_exponent()
(1, 7205759403792794, -56)

To provide more hints (like using rational numbers, real balls, etc), we need to know more about your objective.