I have a calculation that generates what appears to be the Float 22.23, and a literal 22.23 like so:
some_object.total => 22.23
some_object.total.class => Float
22.23 => 22.23
22.23.class => Float
But for some reason, the following is false:
some_object.total == 22.23 ? true : false
Wacky, right?
Is there some kind of precision mechanism being used that maybe isn’t completely transparent through the some_object.total call?
Floating-point numbers cannot precisely represent all decimal numbers within their range. For example, 0.9 is not exactly 0.9, it’s a number really close to 0.9 that winds up being printed as it in most cases. As you do floating-point calculations, these errors can accumulate and you wind up with something very close to the right number but not exactly equal to it. For example,
0.3 * 3 == 0.9will returnfalse. This is the case in every computer language you will ever use — it’s just how binary floating-point math works. See, for example, this question about Haskell.To test for floating point equality, you generally want to test whether the number is within some tiny range of the target. So, for example:
You can also use the BigDecimal class in Ruby to represent arbitrary decimal numbers.
If this is a test case, you can use
assert_in_delta: