Page 1 of 1

How does Micropython avoid double precision issues?

Posted: Sun Nov 18, 2018 5:25 pm
by Zezombye
After implementing doubles, I wanted to test double equality. But to my surprise, 0.1+0.2==0.3 returns True instead of False, although on my PC, Python3 does return False.

I also tried 0.1+0.2-0.3, which returns 5.551115123125783e-17 on my PC, but 0.0 on Micropython.

I thought avoiding double precision issues was only possible with BCD, but from what I've seen, Micropython floats are plain old IEEE754 floats.

- How does Micropython avoid these kinds of double precision issues?
- Is there an operation where a BCD system (used by TI/casio calculators) would return a different answer from Micropython?

Re: How does Micropython avoid double precision issues?

Posted: Sun Nov 18, 2018 5:42 pm
by jickster
There’s a few threads on here about float issues.

Search for “float” or “double” in title of posts.


Sent from my iPhone using Tapatalk Pro

Re: How does Micropython avoid double precision issues?

Posted: Sun Nov 18, 2018 6:01 pm
by dhylands
Micropython doesn't do anything specific to avoid the issues. It has a different sized float so it will have issues with different sets of numbers, but it still has all of the same issues.

For example, on my pyboard:

Code: Select all

>>> 0.5 - 0.3 - 0.2
-1.490116e-08
You also need to be careful to differentiate what a floating point number looks like when its printed versus what the value of the float actually is.

Re: How does Micropython avoid double precision issues?

Posted: Sun Nov 18, 2018 7:03 pm
by Zezombye
Thanks, 0.5-0.3-0.2 indeed prints 5.551115123125783e-17 instead of 0. So there are double precision issues (as expected, but at least I have an example).