Skip to content

Incorrect float values obtained from strings / program code with RP2 #13094

@rkompass

Description

@rkompass

The RP2 Pico (and ESP8266) transfer floats wrongly into the internal representation:

Demonstration code:

import array
import uctypes
from machine import mem32

def printfp32(s, x):
    a = array.array('f', (0,))
    a[0] = x
    a0 = mem32[uctypes.addressof(a)]
    sign = '-' if a0 >> 31 else ''
    exp = ((a0>>23) & 0xff) - 150
    frac = (0x800000 | (a0 & 0x7fffff))
    # print(f"{a[0]:12.6f} {a0:032b}")
    print(f'{s}   {x}: {sign} {frac} * 2**{exp}')
    
y = 360.98564736629
printfp32('y', y)

y 360.9857: 11828778 * 2**-15 on stm32 (Blackpill) <--- correct, closest value
but
y 360.9858: 11828782 * 2**-15 on RP2 Pico <--- incorrect
y 360.986: 11828776 * 2**-15 on ESP 8266 <--- incorrect

this is a deviation of 4 LSB on RP2 Pico.

This was found to be one contributor to inaccuracies in a calculation discussed here, where both Pico and esp8266 exhibited much poorer precision than the other platforms using 32 bit FP math.

I was trying to trace the function for string to float conversion in the RP2040 SDK but didn't find any.
On the other hand, if this function is in the MP code base, how could it be different then on these platforms?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions