Search code examples
algorithmmathapproximation

No idea for limit the sin(x) formula loop


Making c/++ function to compute sin(x)

Referancing by the formula;
http://wiki.ubc.ca/images/math/4/4/7/447a79826774707026bbefcd76962d3a.png

But i don't know where to stop infinity sum, and i don't want libraries for quick-answer. I want to make it. I made factorial function well. (fact() function)

double sin(int x) {
    int tp=0;
    for(int k=1; ;k++)
    {
        tp+=pow(-1,k)*pow(x,2*k+1)/fact(2*k+1);
    }
    return tp;
}

Solution

  • I ran the loop by 8 times, it's approaching to true value if you improve loop timing. Thanks @Sreeram TP

    double sin(int x) {
        int tp=0;
        for(int k=1;k<8 ;k++)
        {
            tp+=pow(-1,k)*pow(x,2*k+1)/fact(2*k+1);
        }
        return tp;
    }