ruby-on-railsfloating-pointroundingminitestfloating-point-comparison

Minitest assert_equal failing when should be equal


I have a minitest test that is asserting if a decimal is the same. Wondering why this

My assert code:

assert_equal -9.04, elts[2].change.round(2)

Failure:

Failure: LeaveTypePolicyTest#test_case_12 [/usr/src/app/test/models/leave_type_policy_test.rb:1007] Minitest::Assertion: Expected: -9.04 Actual: -0.904e1

Anyone experienced this? and perhaps why its failing? I have many many tests doing similar assertions, but only this for some reason fails.


Solution

  • Use assert_in_delta, for example:

    assert_in_delta -9.04, elts[2].change, 0.01
    

    SEE ALSO: Test::Unit::Assertions#assert_in_delta: https://www.rubydoc.info/github/test-unit/test-unit/Test%2FUnit%2FAssertions:assert_in_delta

    For example, this passes:

    assert_in_delta 0.33, 1.0/3, 0.01
    

    It is possible that the initial test failure was caused by comparing float to BigDecimal or something similar to that. Apparently, Ruby needs the classes in such a comparison to match. Here is a simple example that reproduces a similar behavior:

    bar = -9.04                       # Float
    baz = BigDecimal.new("-0.904e1")  # BigDecimal (not Float)
    
    puts bar == baz                   # false
    puts bar == baz.round(2)          # false (even after rounding!)
    puts bar == baz.to_f              # true (converted to Float)
    puts bar == baz.to_f.round(2)     # true (same, plus rounded)