Is there in Ruby some functionality/syntax to compare two floats with delta? Something similar to assert_in_delta(expected_float, actual_float, delta) from test/unit but returning Boolean?
(expected_float - actual_float).abs <= delta
Depending on your application and what your floating point values are actually representing, it might make sense to convert them to Rationals with a given precision. Then direct comparisons will always behave correctly since Rationals are just two integers.
For instance, if you know you're dealing with US currency amounts, storing them as amount.to_r.round(2)
allows for exact comparisons for equality.
Just a thought... not all non-integer values need to be inexactly-represented floats
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With