pico-sdk icon indicating copy to clipboard operation
pico-sdk copied to clipboard

sin(x) and cos(x) have increasing error for |x|>128

Open dpgeorge opened this issue 2 years ago • 4 comments

When using single precision float, sin(x) and cos(x) (and possibly sincos and tan) are accurate for |x| < 128 (here, |x| means absolute value of x) but show increasing error as |x| grows beyond 128.

Here is a plot of the error of sin(x) as a function of x (compared to the single-precision float musl implementation of sin):

rp2040-sin-error

You can see that the error grows linearly beyond -128 / +128.

Looking at the code, there's a wrapper function that reduces the argument down to within +/- 128 and then calls the ROM code: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_float/float_aeabi.S#L637-L664 . That reduction is probably introducing a small floating point error for each loop of the reduction (that's a guess, I did not investigate further).

(Note: this was found running MicroPython's tests/float/cmath_fun.py test, which fails on a Pico board due to the above problem.)

dpgeorge avatar Feb 14 '23 03:02 dpgeorge

One should never call sin (x) or cos(x) with |x| greater than 128! And code for sin(x) and cos(x) should take advantage of trigonometric identities to reduce the range of the argument. See https://realtimecollisiondetection.net/blog/?p=9, for example. The graph tells me the code may have range reduction but it's broken.

znmeb avatar Feb 14 '23 04:02 znmeb

it is indeed broken ;-) oops.

kilograham avatar Feb 15 '23 20:02 kilograham

@kilograham did you intend to close this?

dpgeorge avatar Feb 15 '23 22:02 dpgeorge

nope (wrong button)

kilograham avatar Feb 15 '23 23:02 kilograham