This is the nature of limited-precision representations.
For example, consider six decimal digits of precision. This is not what double
uses, of course, but the concept is precisely the same. 1/3
is .333333
with six digits of decimal precision. So 30 * (1/3)
is 9.99999
. If you round down to an integer, you get 9
, not 10
. This is the problem you are seeing.
If you don't want this behavior, don't use floating point.
It may suffice to add a small "delta" to the value you pass to floor
.