0
ceil((14.84 - 14.04)/0.08)

output - 11

I was expecting the output to be 10 when manually calculated but when running it in python, it is giving output as 11

1 Answers1

1

Is floating point math broken?

The float value from your equation is actually 10.000000000000009 because of how floats are handled (see link for more information). So, even though it is such a small amount above 10 the ceiling function will still place it at 11.

You can try rounding the number to a decimal point that you trust to get the value you want:

from math import ceil

ceil(round((14.84 - 14.04)/0.08, 2))

Output: 10

Michael S.
  • 3,050
  • 4
  • 19
  • 34