int() Anomaly

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Phisatho
Posts: 10
Joined: Mon Jul 18, 2022 9:18 am

int() Anomaly

Post by Phisatho » Sat Jul 23, 2022 6:24 am

>>> 5.5*3600
19800.0
>>> int(5.5*3600)
19800
>>> int(3155673600-19800)
3155653800
>>> int(3155673600-19800.0)
3155653888

Can somebody explain this anomaly?

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

Re: int() Anomaly

Post by davef » Sat Jul 23, 2022 6:51 am

I get the right answer on a 64bit machine running Ubuntu 20.04LTS, so I guess I'd be looking at how well float numbers are being represented in a Micropython port. There was some discussion about this recently, sorry can't find the specific thread. Or how a 10 digit number is represented?

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: int() Anomaly

Post by karfas » Sat Jul 23, 2022 6:56 am

Limited precision for float values.
For your last example, the int gets converted to a float, the subtraction returns a float, you convert it to int.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

Phisatho
Posts: 10
Joined: Mon Jul 18, 2022 9:18 am

Re: int() Anomaly

Post by Phisatho » Sat Jul 23, 2022 7:09 am

As I understand, int() is truncating the decimal part and converting the data type to int. I am not an expert, but logic tells me that this process should not cause a deviation of 88.

Phisatho
Posts: 10
Joined: Mon Jul 18, 2022 9:18 am

Re: int() Anomaly

Post by Phisatho » Sat Jul 23, 2022 7:12 am

@karfas: Looks like you are right.
3155673600-19800.0 gives me 3.155654e+09

TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: int() Anomaly

Post by TheSilverBullet » Sat Jul 23, 2022 7:46 am

If you work with float on MicroPython, that's the precision you can expect:

Code: Select all

print(f'{1.0/3.0:52.48f}')
# 0.33333334922790527343750000000
About seven digits.
More info: https://en.wikipedia.org/wiki/IEEE_754

The implicit int to float conversion:

Code: Select all

x = 3155673600.0; print(f'{x:.15g} {x:30.15f}')
or
x = 3155673600; x = float(x); print(f'{x:.15g} {x:30.15f}')
results in:
3155673742.29431 3155673742.294311523437500
which is the nearest representation in float.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: int() Anomaly

Post by pythoncoder » Sat Jul 23, 2022 8:31 am

TheSilverBullet wrote:
Sat Jul 23, 2022 7:46 am
If you work with float on MicroPython, that's the precision you can expect:
...
This is not strictly correct. Most MP targets support 32 bit floats, but some support 64 bits, notably the Pyboard D SF6W. Other targets can support 64 bits with a compile option.
Peter Hinch
Index to my micropython libraries.

TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: int() Anomaly

Post by TheSilverBullet » Sat Jul 23, 2022 9:23 am

pythoncoder wrote:
Sat Jul 23, 2022 8:31 am
TheSilverBullet wrote:
Sat Jul 23, 2022 7:46 am
If you work with float on MicroPython, that's the precision you can expect:
...
This is not strictly correct. Most MP targets support 32 bit floats, but some support 64 bits, notably the Pyboard D SF6W. Other targets can support 64 bits with a compile option.
So, would it'd be safe to assume that ›float‹ is the precision used on most boards while a very few selected ones provide ›double‹ resolution? That information might be a good candidate for the docs.
I'd imagine that most users just put the uf2 file de jour on the µC board and be done with it.

TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: int() Anomaly

Post by TheSilverBullet » Sat Jul 23, 2022 10:43 am

pythoncoder wrote:
Sat Jul 23, 2022 8:31 am
Other targets can support 64 bits with a compile option.
Peter, I took a closer look at the sourcecode for 1.19.1 (particularly the rp2 port)
Would a change in mpconfigport.h:
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
to
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)

be sufficient? Or did I overlook something important?
I'd be willing to sacrifice some speed in exchange for higher resolution.
Thanks,
Norbert

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: int() Anomaly

Post by pythoncoder » Sun Jul 24, 2022 10:39 am

I believe this is correct, but I haven't actually tried it.
Peter Hinch
Index to my micropython libraries.

Post Reply