Skip to content

Check if sin/cos approximation via integers has sufficient accuracy and speed #31

@Kuratius

Description

@Kuratius

It's possibly to compute a good approximation to the cosine of a number using a single float multiplication and 1 integer multiplication.

The implementation from this video for example
https://www.youtube.com/watch?v=hffgNRfL1XY
(see e.g. 8:02 )

can be modified to use only integers except for the very last step.
Example:

#define SECOND_ORDER_COEFFICIENT 0.0000000010911122665310369f
uint32 a=3667592205

float b=1.f/a ( can be evaluated at compile time)
sint32 c= (sint32) (SECOND_ORDER_COEFFICIENT*a) (also compile time)

uint32 x=16382 (0 is 0 angle and 16382 is pi/4 ), values lower than -pi/4 or greater than pi/4 need to be done with symmetry transforms or the squareroot operation.


 float result=b * (a-c*x*x)

the compiler optimizes this a bit to turn 2 integer multiplies +1 float into 1 integer multiplications and 1 float multiplication.
There are other possible values for a that can be chosen, potentially with less error than the one I used for this example, especially if the range of x is changed. The float multiply could also be replaced with a hardware divide if the angle doesnt have to be a float.

The current implementation seems to use a 8th order polynomial with doubles.

float cosf(float x)

I'm also a bit confused as to why p[0] seems to be unused here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions