What you see is not what you get with floating point numbers. Have Ruby print out 20+ decimal places and you’ll see that there is in-fact a difference. When trying to compare floats, you need to use an epsilon and check that the two values are close enough to each other. e.g.
x = 5.1
y = 5.1
epsilon = 0.001
abs(x - y) < epsilon
Jason
Jason Roelofs wrote:
What you see is not what you get with floating point numbers. Have Ruby
print out 20+ decimal places and you'll see that there is in-fact a
difference. When trying to compare floats, you need to use an epsilon and
check that the two values are close enough to each other. e.g.
x = 5.1
y = 5.1
epsilon = 0.001
abs(x - y) < epsilon
Jason
Assuming you are using the Ruby Test::Unit framework or something derived from it, the right assertion to use when comparing floats for equality is assert_in_delta, documented here:
http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M002493
regards
Justin Forder