asteroid-lang / asteroid

Asteroid is a modern, multi-paradigm programming language that supports first-class patterns.
https://asteroid-lang.org
MIT License
40 stars 10 forks source link

issue warning when user performs an exact comparison on reals #158

Open olwmc opened 2 years ago

olwmc commented 2 years ago

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.

lutzhamel commented 2 years ago

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!

olwmc commented 2 years ago

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?

lutzhamel commented 1 year ago

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...