Comparing two Floats is a general-purpose feature that could be added to Facets. I've implemented it in two projects that I'm working on, and submit it for consideration.
Here is my code:
class ::Float
def Float.close?(a, b, epsilon=0.0000001)
a, b = a.to_f, b.to_f
if a.zero? or b.zero?
# There's no scale, so we can only go on difference.
(a - b).abs < @epsilon
else
# We go by ratio. The ratio of two equal numbers is one, so the ratio
# of two practically-equal floats will be very nearly one.
(a/b - 1).abs < epsilon
end
end
end
I don't know any best practices about this kind of thing. My implementation was preceded by some research on Google, but that's it. I have unit tests, but they're for a different implementation and in my own testing library which hasn't seen public release (
http://github.org/gsinclair/attest).
Given the go-ahead, I'm happy to fork, implement (with tests) and send pull request. I'm equally happy for someone to take the code, or even just the idea, and make it happen. Also, of course, the specifics of the implementation should be discussed if necessary.
Gavin