Round-Off Errors in Ruby
by admin on Apr.27, 2009, under Programming
# At first, we store the value “1″ in a variable and start a loop
x = 1
for i in (1..200)
# inside the loop, we assign random numbers to 2 variables
a = rand
b = rand
# the numbers are then added to the variable x and substracted again.
x = x + a
x = x + b
x = x - a
x = x - b
# Finally print the results
print x
print ”\n”
end
You’d think, when you add and substract the same number from 1, you would get 1 again. However, this is not the case due to Round-Off Errors in floating point calculations. This problem is not just limited to Ruby, it can basically happen in any programming language.

For more info have a look at: http://mathworld.wolfram.com/RoundoffError.html