Incorrect floating point calculations

Found erroneous behavior in floating point calculations in openmv firmware when doing a performance calculation test.

A very simple procedure for calculating the number pi using the Leibniz formula, 10_000_000 iterations.

def calculate_pi(n):
    t_sum = 0
    for i in range(n):
        term = (-1) ** i /(2*i+1)
        t_sum = t_sum + term
    return t_sum * 4

def performanceTest8():
    iterations = 10000000
    print("Calculate %i iterations PI Leibniz Formula" % iterations)
    timet1 = utime.ticks_ms()
    value = calculate_pi(iterations)
    timet2 = utime.ticks_ms()
    print(value)
    print("Calculations time %u ms" %(timet2-timet1))

performanceTest8()

Got the following results:

OpenMV Cam H7 Plus (omv):
0.950764 !? !? !? !!!
Calculations time 48205 ms

Portenta H7 (omv)
0.950764 !? !? !? !!!
Calculations time 57519 ms

The same test on the version from Micropython.org (1.19.1):

Maix Amigo:
3.141597
Calculations time 121479 ms

PiPico:
3.141597
Calculations time 405259 ms

PyCom LoPy4(esp32):
3.141597
Calculations time 355002 ms

Nucleo-STM32F429ZI
3.141597
Calculations time 210003 ms

Nucleo-STM32H743ZI
3.141592553589793
Calculations time 86835 ms

PC Notebook:
3.1415925535897915
Calculations time 1990.820 ms

platforms

(tried different platforms Teensy 4.1, stm32, arduino etc - everything is fine)
All platform versions from OpenMV give an error, except for Arduino BLE33Sense (omv):

3.141597 !!!
Calculations time 517730 ms

Please fix it, otherwise there is an uncertain impression of the calculations.

Yeah! Woah! Thanks for finding this. The code is totally part of MicroPython….

Mmm, we don’t touch any of that. It could be a bug in the version of floating point numbers we use. We’re using the 4 byte version of a MicroPython float versus the object version which is much larger. They offer a compile flag to change out which floating point scheme to use.

There might be a limit on the decimal places in our version of the floats to save bits.

Can you create a bug tracker on this for our GitHub. But, also, I can give a link to the implementation we use from MicroPython. What you are trying to do might not be possible via the Python API if the decimal places are truncated with out float implementation.

Hi, I tested your code with a Portenta-H7 running MicroPython (Arduino boards are supported upstream now) and I got the same results, so this is not a specific issue with our firmware, and you should report it to MicroPython here: Issues · micropython/micropython · GitHub