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).
float() consistency with python3
Re: float() consistency with python3
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).I am encountering slight differences in float() output between micropython and python3 in an alpine linux container, is this expected behavior?
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.)
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/
Re: float() consistency with python3
Any insight on why ppc64le and x86_64 would return different results for float('.' + '9' * 70)? Are different results expected for different cpu types?
Re: float() consistency with python3
Well, you have ppc64le, you'd have insight into it.
The reality? You see it.
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?).Are different results expected for different cpu types?
The reality? You see it.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/
Re: float() consistency with python3
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.
Re: float() consistency with python3
On my Power8 box:
>>> float('.' + '9' * 70)
1.000000000000001
on my x86_64 laptop:
>>> float('.' + '9' * 70)
1.0
>>> float('.' + '9' * 70)
1.000000000000001
on my x86_64 laptop:
>>> float('.' + '9' * 70)
1.0