Tell me pls why this code does not work correctly:
p a # result - 157.2
p b # result - 307.9
p a+b # result 465.09999999999997
Why is rounding reset when summing?
A Floating Point number is an approximation of the value it is trying to represent. The IEEE-754 format which we use to store these numbers is an attempt to represent all numbers between negetive infinity and positive infinity in 64 bits of memory. As a result, for almost all numbers there is a very small error in the value that is stored.
For a hands on example try these links:"
This code prints the actual value of your variables a and b
a = 6.187595819331583.to_mm
a = a.round(1)
puts "The value of a is:"
puts "The actual value of a is:"
puts "%.36f" % a
b = 12.122847489661602.to_mm
b = b.round(1)
puts "The value of b is:"
puts "The actual value of b is:"
puts "%.36f" % b
puts "the sum is:"
puts a + b
Because the resultant value is a new object from the
+() method, which does not know that the
round() method was called on the two operands (receiver and argument.) And of course, it sees the slight error as explained by sWilliams above.
So, you will need to call
round() upon the result as well: