Math.floor(10) = 9?

Hey everyone, I was doing some work with percentages and I noticed my code was outputting unexpected numbers.

image

As you can see when I use math.floor on A it says the result is 9, and when I use it on the number 10 (which is the same as A) the result then changes to the expected 10.

Is this a floating point error?

This is because math.floor always rounds down and the math operation you’re doing is creating a floating point error, which is a slight deviation from the expected number. math.floor won’t round up even in the slightest.

1 Like

A isn’t actually 10. Yes it’s a floating point error. Any math you do using fractions should always be rounded using the classic math.floor( answer + 0.5 ) if you want it to be a perfect integer.

It’s because 0.9 and 0.1 cannot be perfectly represented, but Roblox will display 9.999999 as 10 once it gets to a certain number of decimal places as it’s not practical to show all of them.

Edit: if you wish to prove this to yourself, try this,

print( math.floor( A * 10000000 ) )

And it should print 9999999.

5 Likes

Ahhhh I see. I didn’t get why it would show me 10 and not 9.9999999999 something.
But yeah the display behaviour does make sense.
Thanks!