Float/Double

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
priis
Posts: 26
Joined: Tue Mar 31, 2015 9:52 pm

Float/Double

Post by priis » Wed Oct 05, 2022 7:27 pm

Here is a short dummy program:

a=1234.123456789
b=1234.123459876
c=b-a
print('a,b,c=',a,b,c)
print('a={:.9f}'.format(a))
print('b={:.9f}'.format(b))
print('c={:.9f}'.format(c))

Normal python gives the following output:
a,b,c= 1234.123456789 1234.123459876 3.0869998681737343e-06
a=1234.123456789
b=1234.123459876
c=0.000003087

while micropython on my esp32 gives:
a,b,c= 1234.123 1234.123 0.0
a=1234.123468399
b=1234.123468399
c=0.000000000

This is too bad!!!!!
Is there a way to do better in micropython? Double precision? How?
I tried to install the FixedPoint module but I only got error messages!? Is there a different solution?

Poul Riis
Denmark

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Float/Double

Post by jimmo » Wed Oct 05, 2022 9:45 pm

Please open new topics at GitHub Discussions

Micropython uses whatever floating point representation the hardware provides. In this case ESP32 has single-precision floats. Python on your PC is using your PC's hardware double-precision floats.

You can compile your own firmware for ESP32 with the MICROPY_FLOAT_IMPL set to MICROPY_FLOAT_IMPL_DOUBLE in which case the compiler will generate software-emulated double-precision floating point code instead (which will be slower and result in a larger firmware binary, which might be acceptable depending on your use case)

priis
Posts: 26
Joined: Tue Mar 31, 2015 9:52 pm

Re: Float/Double

Post by priis » Thu Oct 06, 2022 8:07 am

My problem was solved by installing a double precision firmware found here:
https://gitlab.com/rcolistete/micropyth ... -07-29.bin

But that introduces another problem - I had better start another thread...

Post Reply