int() Anomaly

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: int() Anomaly

Post by scruss » Sun Jul 24, 2022 4:49 pm

This seemed to work for someone on the Raspberry Pi forum: Re: MicroPython double precision

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

Re: int() Anomaly

Post by TheSilverBullet » Mon Jul 25, 2022 7:50 am

pythoncoder wrote:
Sun Jul 24, 2022 10:39 am
I believe this is correct, but I haven't actually tried it.
Thanks Peter,
Will give it a try as soon as I'm back. (Traveling right now)

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

Re: int() Anomaly

Post by Phisatho » Mon Jul 25, 2022 11:51 am

I built with micropython after applying #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) as thesilverbullet and can confirm that the "anomaly" has disappeared.

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

Re: int() Anomaly

Post by TheSilverBullet » Mon Jul 25, 2022 12:16 pm

Phisatho wrote:
Mon Jul 25, 2022 11:51 am
I built with micropython after applying #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) as thesilverbullet and can confirm that the "anomaly" has disappeared.
Nice. But rest assured, the ›anomaly‹ hasn't disappeared, it has been shift from the seventh to the seventeenth digit. 8-) Not as visible as before.
print(f'{1.0/9.0:.30f}')
0.111111111111111104943205418749

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

Re: int() Anomaly

Post by karfas » Mon Jul 25, 2022 3:01 pm

TheSilverBullet wrote:
Mon Jul 25, 2022 12:16 pm
Nice. But rest assured, the ›anomaly‹ hasn't disappeared, it has been shift from the seventh to the seventeenth digit
This isn't an "anomaly". It is quite normal that the precision of floats is limited.
For more precision, you can use the integers (in micropython with arbitrary size).
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

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

Re: int() Anomaly

Post by TheSilverBullet » Mon Jul 25, 2022 3:12 pm

karfas wrote:
Mon Jul 25, 2022 3:01 pm
TheSilverBullet wrote:
Mon Jul 25, 2022 12:16 pm
Nice. But rest assured, the ›anomaly‹ hasn't disappeared, it has been shift from the seventh to the seventeenth digit
This isn't an "anomaly". It is quite normal that the precision of floats is limited.
For more precision, you can use the integers (in micropython with arbitrary size).
Trust me, I know precisely what it is. :D IEEE knows it as well. They have the 754…

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: int() Anomaly

Post by Roberthh » Mon Jul 25, 2022 4:18 pm

So we have 16 significant digits at least in this little test:

With CPython:

print(f'{1.0/9.0:.30f}')
0.111111111111111104943205418749

With Micropython at Teensy 4.x, PyBoard D SF6 and rp2, compiled with double precision:

print(f'{1.0/9.0:.30f}')
0.11111111111111111604543566500

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

Re: int() Anomaly

Post by TheSilverBullet » Tue Jul 26, 2022 1:30 pm

Roberthh wrote:
Mon Jul 25, 2022 4:18 pm
So we have 16 significant digits at least in this little test:
Exactly!
16 significant decimal digits which translates to a binary mantissa of size:
ln(10) / ln(2) * 16 = 53,…
And those 53 binary digits/bits, combined with 11 binary digits/bits as exponent are what's defines as double precision floats.
The important part is:
It is absolutely impossible to represent certain fractions in binary notation.
Pretty much the same way as it's impossible to represent certain decimal fractions.

But a ›double‹ resolution variable is 2^19 times better/more precise than a regular ›float‹ variable.
And that's a significant improvement, if really needed. Because at the same time it might be a bit slower.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: int() Anomaly

Post by Roberthh » Tue Jul 26, 2022 1:49 pm

Let me add that the binary representation of that number is the same with CPython and MicroPython. Only the printed results differ in the part, which is anyhow not significant.

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

Re: int() Anomaly

Post by Phisatho » Wed Jul 27, 2022 2:21 am

Between single precision and double precision, the underlying representation itself differs - not just the displaying format. In my case, it was significant. I was trying to subtract a fractional offset from NTP_OFFSET. The conversion error caused 88 seconds difference.

Post Reply