Ruby weird maths?


#1

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.


#2

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)

#3

hey thanks for your reply.
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.


#4

nevermind just realised it should be “i = i + 0.2 - 10**-15”
that worked great.

again, thank you :slight_smile:


#5

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.


#6

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)?


#7

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