-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Floating-point behavior is not consistent with IEEE754 #4132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The bug seems to be in |
What platform do you use? MicroPython leaves floating-point handling to the platform (which means your FPU if you have that, or somebody's software floating-point library if you don't). To get ahead with this, you would need to provide binary (i.e. hexadecimal) values of the FP numbers you get, that's the only way to assess correctness/incorrectness of it. (Well, approach being able to assess, the actual assessment is a work on its own.) |
We've tested that behavior on different platforms (ARM v7m, x86_64), so this bug is not platform-dependent. The bug really is early in the process, at the parsing level: |
To be more specific, we should implement float.hex() (guarded by config option, default off - which are the usual rules) to be able to debug there comfortably. |
@EmilieNumworks , good research, thanks. I'm sure @dpgeorge will look into this as his time permits. As you can see, mp_parse_num_decimal() does iterative float multiplications, a recipe for rounding error. I don't know how other folks do it, perhaps keeping to integer value for as long as possible? Worth research too. |
At least the line For example, when parsing |
The behaviour of float parsing was improved in b768cc6 |
For some reason, even the most simple floating-point operations yields results that don't match IEEE754. For instance, in uPy,
0.1+0.2 == 0.3
returnsTrue
, while it isFalse
in CPython and every other programming language that uses IEEE754.One might argue that uPy's behavior is better. In that case it might, but uPy also does some rounding errors, just not the same as everyone else… For example,
1000.4-1000
will yield0.4000244
in uPy but0.39999999999997726
in CPython, JavaScript (Chrome, Firefox), Ruby, etc…Also it looks like uPy's behavior is not matching the Python standard, since https://docs.python.org/3/tutorial/floatingpoint.html explicitly says that
0.1+0.2 == 0.3
should returnFalse
.This bug is reproducible on the uPy live web page.
The text was updated successfully, but these errors were encountered: