Skip to content

double float parsing broken #9034

@h-milz

Description

@h-milz

CircuitPython version

Adafruit CircuitPython 9.0.0-beta.2-25-g95f24318e4-dirty on 2024-03-11; Adafruit Feather M4 Express with samd51j19

I'm still trying to get double float to work. Linking against libm works fine. When MICROPY_FLOAT_IMPL_DOUBLE is 
off, I can do: 

>>> import math as m 
>>> m.sin(4)
-0.756802
>>> m.sin(4.0)
-0.756802
>>> m.sqrt(42)
6.48074

When I activate _DOUBLE and _CMATH, it looks like this: 

>>> import math as m
>>> import cmath as c
>>> m.sin(4)
1.839006457345601e-314
>>> m.sin(4.0)
0.0
>>> m.sqrt(42)
6.146221405944527e-315
>>> c.sin(4)
(-0.7568024953079282+-0j)
>>> c.sin(4.0)
0j
>>> c.sqrt(42)
(6.48074069840786+0j)
>>> c.sqrt(-42)
(3.968309176204584e-16+6.48074069840786j)

where the last real part appears to be a rounding issue. 

Apparently, double float parsing is broken. Integer parsing seems to work ok for cmath entry. Watch the 
difference between c.sin(4), where the real part of the result is correct, and where real mode sin from libm.a 
is invoked under the hood, and m.sin(4) which takes input from the command line, calls the same real 
mode sin, and obviously returns gibberish. 

When the argument is a float (4.0), it is apparently parsed wrongly - c.sin internally calls m.sin as you can tell. 

Similarly for sqrt - m.sqrt returns gibberish, but c.sqrt works fine for both signs. 

I dug through parsenum.c, objfloat.c, formatfloat.c and a couple of more but I can't find anything suspicious. 

(As a sidenote, c.sqrt when called with a complex argument returns nonsense as well: 

>>> c.sqrt(1.234+0.456j)
(0.4774934554525329+0.4774934554525329j)

The correct result should be something like 1.129+0.202j. No idea if this is related to num parsing, but when 
I do the same with _DOUBLE disabled, it works fine.)


### Code/REPL

```python
none

Behavior

see above

Description

see above

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions