2

I have the following piece of code which I run with python 2.7.9:

import math

print 0.6/0.05
print math.floor(0.6/0.05)
print int(0.6/0.05)

The output is:

12.0
11.0
11

Why is it turning my 12.0 to an 11.0 when I use floor or int? round() will work but does not suit my use case. I have this running with 0.55,0.60,0.65,0.70... and everything works fine except for 0.6. Any idea?

EitanT
  • 285
  • 6
  • 12
  • 2
    This is a typical floating point precision problem. When I tried `0.6/0.05`, it gives me `11.999999999999998` which is a floating point arithmetic issue. – Mp0int May 26 '15 at 07:16
  • You might fare better by multiplying by `20`, Python 2.7 gives `12.0`. Also, `0.6 * (1 / 0.05)` gives `12.0`. – Peter Wood May 26 '15 at 07:26

1 Answers1

0

If you know the required resolution in advance, you can multiply your input numbers accordingly and then make the required calculations safely.

Let's say that the resolution is 0.01. Then:

# original inputs
n = 0.6
d = 0.05

# intermediate values
n1 = int(n*100)
d1 = int(d*100)

# integer calculation
r = n1 // d1

# or

# floating point calculation
r = float(n1) / float(d1)
dlask
  • 8,776
  • 1
  • 26
  • 30