grain
grain copied to clipboard
How Should Float Equality Work?
At the moment, for our structural equality check, we simply do a == comparison on floating-point numbers. This is awkward, though, if we are, say, comparing a float32 and a float64, since a user might expect them to be equal. What's the correct course of action here? Some options:
- Leave it as-is, documenting it as "floating point equality is unstable". I think this is the approach most languages take, so I'm happy to take it too.
- Build in some tolerance threshold for equality checks. This will inevitably bite someone when they are working with tiny values, though.
What about: .1 + .2 ~== .3? You could also allow programmers to efficiency specify the level of equality that they care about: .1 + .2 ==.prec(2) .3 .
My vote would be to have == check if the values are strictly equal, but implement Number.isClose, Float32.isClose, and Float64.isClose, similar to Python's math.isclose for checking if values are roughly similar.
Once we have that for a while and we're happy with it, I could see us adding some syntax similar to what @indolering posted.