float() consistency with python3

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
mksully22
Posts: 9
Joined: Tue Sep 11, 2018 12:07 pm

float() consistency with python3

Post by mksully22 » Tue Sep 11, 2018 12:41 pm

I am encountering slight differences in float() output between micropython and python3 in an alpine linux container, is this expected behavior?
Micropython:
~/aports/testing/micropython/src/micropython-1.9.4/tests $ ../ports/unix/micropython
MicroPython v3.8.0-1909-g685fa426c5-dirty on 2018-09-11; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> float('.' + '9' * 4)
0.9999000000000001

Python3:
~/aports/testing/micropython/src/micropython-1.9.4/tests $ python3
Python 3.6.6 (default, Aug 23 2018, 09:02:39)
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> float('.' + '9' * 4)
0.9999

Additionally I see small differences using micropython on x86_64 vs ppc64le. Are these different results between cpu architectures expected behavior?
~/aports/testing/micropython/src/micropython-1.9.4/tests $ ../ports/unix/micropython
MicroPython v3.8.0-1909-g685fa426c5-dirty on 2018-09-11; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> float('.' + '9' * 70)
1.000000000000001

X86_64
Linux 1060f6cfbd6b
~/aports/testing/micropython/src/micropython-1.9.4/tests $ ../ports/unix/micropython
MicroPython v3.8.0-1909-g685fa426c5-dirty on 2018-09-11; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> float('.' + '9' * 70)
1.0

This last difference may explain why the float_parse.py test fails on ppc64le. Using python3 on ppc64le, float('.' + '9' * 70) returns 1.0. This causes float_parse.py test to fails because of the failed comparison to the micropython result (1.000000000000001).

pfalcon
Posts: 1064
Joined: Fri Feb 28, 2014 2:05 pm

Re: float() consistency with python3

Post by pfalcon » Tue Sep 11, 2018 2:09 pm

I am encountering slight differences in float() output between micropython and python3 in an alpine linux container, is this expected behavior?
Yes. CPython uses sly algorithm of rounding up towards "most human expected number", while MicroPython just tries to do exact representation (modulo binary to decimal conversion rounding errors).

It theoretically would be nice to employ a similar algo, but practically, that depends on what overhead it has.

Otherwise, the fact that floating points numbers aren't exact is well-known fact, just as there're well-known ways to deal with that (limiting number of significant digits in output, using epsilon for comparisons, etc.)
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
Oftentimes more up to date docs than mainline - http://pycopy.readthedocs.io/

mksully22
Posts: 9
Joined: Tue Sep 11, 2018 12:07 pm

Re: float() consistency with python3

Post by mksully22 » Tue Sep 11, 2018 3:38 pm

Any insight on why ppc64le and x86_64 would return different results for float('.' + '9' * 70)? Are different results expected for different cpu types?

pfalcon
Posts: 1064
Joined: Fri Feb 28, 2014 2:05 pm

Re: float() consistency with python3

Post by pfalcon » Tue Sep 11, 2018 3:52 pm

Well, you have ppc64le, you'd have insight into it.
Are different results expected for different cpu types?
No, all hardware/software should implement https://en.wikipedia.org/wiki/IEEE_754 , and thus produce exactly the same results (if using the same data types, does your ppc64le vs x86_64 do that?).

The reality? You see it.
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
Oftentimes more up to date docs than mainline - http://pycopy.readthedocs.io/

User avatar
dhylands
Posts: 2881
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: float() consistency with python3

Post by dhylands » Tue Sep 11, 2018 3:55 pm

You should probably read: https://floating-point-gui.de (simplified) and https://docs.oracle.com/cd/E19957-01/80 ... dberg.html (much more detailed) to understand why many of these types of differences exist and how you should deal with them.

mksully22
Posts: 9
Joined: Tue Sep 11, 2018 12:07 pm

Re: float() consistency with python3

Post by mksully22 » Tue Sep 11, 2018 3:57 pm

On my Power8 box:
>>> float('.' + '9' * 70)
1.000000000000001

on my x86_64 laptop:
>>> float('.' + '9' * 70)
1.0

Post Reply