Very low int rounding error on Raspberry Pico

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Filip
Posts: 4
Joined: Sat Nov 20, 2021 9:50 pm

Very low int rounding error on Raspberry Pico

Post by Filip » Mon Jan 17, 2022 5:53 pm

Hi there.

First congrats for 1.18. I'm very happy and gratefull but very fresh member of MicroPython coders community.

I have some rounding errors in my outputs so I dig into it. I know floats are nasty with very limited resources but this feels too soon.

Code: Select all

print("This should not print 66.30001 on Pico but it does:", round(66.3, 1))
BTW: result is the same on 1.17.

Any known workaround? Should I report a bug or this is well known noob error?

Thanks for any comment or suggestion.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Very low int rounding error on Raspberry Pico

Post by davef » Mon Jan 17, 2022 6:37 pm

Known problem. If you are not too concern about any benefit of rounding up or down then just use string formatting, ie:

Code: Select all

Battery_voltage = f'{Battery_voltage:.2f}'

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Very low int rounding error on Raspberry Pico

Post by scruss » Mon Jan 17, 2022 6:44 pm

If you want formatted numbers of a particular length, use % or format:

Code: Select all

"%.1f" % 66.3
'66.3'
You've found one of those difficult numbers that can't be represented exactly in a float:

Code: Select all

float("%.1f" % 66.3)
66.30001
There's a bit more to it than that (CircuitPython 7.0 returns 66.3) but using formatted strings for output is reliable.

Filip
Posts: 4
Joined: Sat Nov 20, 2021 9:50 pm

Re: Very low int rounding error on Raspberry Pico

Post by Filip » Wed Jan 19, 2022 2:32 pm

It's not round it's print ;).

Code: Select all

if 66.3 == round(66.30001, 1):
    print("Rounding is fine")
print("This should not print 66.30001 on Pico but it does:", 66.3)

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Very low int rounding error on Raspberry Pico

Post by scruss » Thu Jan 20, 2022 5:20 am

No, as I said in the other thread: it's a floating point thing.

Your code

Code: Select all

if 66.3 == round(66.30001, 1):
is the same as saying:

Code: Select all

if 66.30001 == 66.30001:
which is of course true. The single-precision binary floating point library on the Pico can't represent 66.3 exactly.

Consider this:

Code: Select all

n = 0.0
i = 0.1

for j in range(663):
    n = n+i

print("n =         ", n)
print("n == 66.3 ? ", (n == 66.3))
print("66.3 - n =  ", 66.3-n)
You'd expect that adding 0.1 to 0 663 times would give you 66.3, or at least the same nearest representation. But if you run it:

Code: Select all

n =          66.29956
n == 66.3 ?  False
66.3 - n =   0.0004425049
You don't get the same result at all.

Post Reply