# Ruby weird maths?

Not an actual problem here, i passed the lesson and my code worked.
But the weird thing is when I ran:
"i = 0
while i <= 5
puts i
i = i + 0.2
end

the results i got where incorrect.

What you are seeing is an artifact of floating point arithmetic. As we can see, the amount of error is in the magnitude of 10 to the minus 15, and not negligible enough to have any effect on our calculations.

The way to get around this is to round to a single decimal placeâ€¦

``puts i.round(1)``

with that addition it only prints to 4.8 so I did a hacky fix in the end â€śwhile i <= 5.2â€ť
I want to find a better way.
I tried â€śi = i + 0.2 + 10**-15â€ť
but this just crashes.

nevermind just realised it should be â€śi = i + 0.2 - 10**-15â€ť
that worked great.

again, thank you

1 Like

You shouldnâ€™t rely on your error being some particular value, you should instead either tolerate the error or use only exact operations so that there is no error (float is often not what you want)

You can for example count the number of tenths instead of whole numbers (like counting cents instead of whole dollars)

Another option is to pick a different value type such as BigDecimal which makes other tradeoffs but produce the results that â€śpeople expectâ€ť where possible

Floats are approximations. For most use-cases it is not appropriate to do things like testing for equality, two floats may be incredibly close but not equal. Some operations are still exact, but even then you may really be looking for something else.

hey thanks for your reply, it did strike me that there would have to be a better way still.

any chance you could provide some example code of how you would achieve the out put I wanted (printing 0.2, 0.4â€¦ 5.0)?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.