Skip to content

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

Closed
Ecco opened this issue Sep 13, 2018 · 8 comments
Closed

Floating-point behavior is not consistent with IEEE754 #4132

Ecco opened this issue Sep 13, 2018 · 8 comments

Comments

@Ecco
Copy link
Contributor

Ecco commented Sep 13, 2018

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 returns True, while it is False 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 yield 0.4000244 in uPy but 0.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 return False.

This bug is reproducible on the uPy live web page.

@EmilieNumworks
Copy link
Contributor

The bug seems to be in mp_parse_num_decimal which parses 1000.4 as 1000.4000000000001.

@pfalcon
Copy link
Contributor

pfalcon commented Sep 13, 2018

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

@EmilieNumworks
Copy link
Contributor

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: 1000.4 is improperly parsed as 0x408f433333333334 instead of 0x408F433333333333.

@pfalcon
Copy link
Contributor

pfalcon commented Sep 13, 2018

To get ahead with this, you would need to provide binary (i.e. hexadecimal)

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.

@pfalcon
Copy link
Contributor

pfalcon commented Sep 13, 2018

1000.4 is improperly parsed as 0x408f433333333334 instead of 0x408F433333333333.

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

@Ecco
Copy link
Contributor Author

Ecco commented Sep 13, 2018

At least the line dec_val *= MICROPY_FLOAT_C_FUN(pow)(10, exp_val); is buggy.

For example, when parsing 1000.4, it tries to do dec_val = 10004.0 * 0.1 which yields an incorrect result because of a rounding error. Indeed, in IEEE754 world, 1000.4 != (10004.0*0.1).

@Ecco
Copy link
Contributor Author

Ecco commented Sep 13, 2018

See #4133 for a possible fix. Uses the same logic as some implementations of strtod(3).

@dpgeorge
Copy link
Member

dpgeorge commented Dec 4, 2018

The behaviour of float parsing was improved in b768cc6

@dpgeorge dpgeorge closed this as completed Dec 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants