Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would Ruby fail equality on 2 floats that appear the same?

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?

like image 602
btelles Avatar asked Apr 07 '10 20:04

btelles


2 Answers

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.9 will return false. 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:

def float_equal(a, b)
  if a + 0.00001 > b and a - 0.00001 < b
    true
  else
    false
  end
end

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:

def test_some_object_total_is_calculated_correctly
  assert_in_delta 22.23, some_object.total, 0.01
end
like image 76
Chuck Avatar answered Nov 15 '22 05:11

Chuck


Float#to_s and Float#inspect round. Try "%.30f" % some_object.total and you will see that it's not quite 22.23.

like image 27
sepp2k Avatar answered Nov 15 '22 06:11

sepp2k