grain icon indicating copy to clipboard operation
grain copied to clipboard

How Should Float Equality Work?

Open peblair opened this issue 5 years ago • 2 comments

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.

peblair avatar Sep 12 '20 13:09 peblair

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 .

indolering avatar Jan 04 '21 21:01 indolering

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.

ospencer avatar Aug 03 '22 17:08 ospencer