How does Micropython avoid double precision issues?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Zezombye
Posts: 34
Joined: Mon Jul 30, 2018 8:29 pm

How does Micropython avoid double precision issues?

Post by Zezombye » Sun Nov 18, 2018 5:25 pm

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?

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: How does Micropython avoid double precision issues?

Post by jickster » Sun Nov 18, 2018 5:42 pm

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

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

Re: How does Micropython avoid double precision issues?

Post by dhylands » Sun Nov 18, 2018 6:01 pm

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.

Zezombye
Posts: 34
Joined: Mon Jul 30, 2018 8:29 pm

Re: How does Micropython avoid double precision issues?

Post by Zezombye » Sun Nov 18, 2018 7:03 pm

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

Post Reply