asteroid icon indicating copy to clipboard operation
asteroid copied to clipboard

issue warning when user performs an exact comparison on reals

Open olwmc opened this issue 3 years ago • 3 comments

Because python's underlying floating point arithmetic has precision errors, the following program prints out false:

load system io.
io @println ( (.1 + .1 + .1) == .3 ).

All we would really need to do is to have a check in our comparators for real and use the math.isclose() if we wanted a more natural model for floating point arithmetic. Alternatively, we could implement our own isclose to mirror the underlying implementation.

olwmc avatar Apr 07 '22 17:04 olwmc

That's real bad. I thought, may be this is due to the way we might be interacting with Python but no,

lutz$ python3
Python 3.8.9 (default, Oct 26 2021, 07:25:53) 
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 0.1 + 0.1 + 0.1 == 0.3
False
>>> 

Ouch!

lutzhamel avatar Apr 07 '22 17:04 lutzhamel

By extension this is true with standard C under both gcc and clang (The only two I've tested so far)!!

What do you all think the best solution here is? Have some standard epsilon we work with for tolerances or just give users something akin to isclose?

olwmc avatar Apr 07 '22 17:04 olwmc

The function isclose already exists in our math module. Perhaps we should issue a warning when folks try to do an exact comparison on reals...

lutzhamel avatar Feb 17 '23 00:02 lutzhamel