Page 1 of 1

Float/Double

Posted: Wed Oct 05, 2022 7:27 pm
by priis
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

Re: Float/Double

Posted: Wed Oct 05, 2022 9:45 pm
by jimmo
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)

Re: Float/Double

Posted: Thu Oct 06, 2022 8:07 am
by priis
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...