News for hackers. Stuff that matters.
Re:Cosine Implementation in C by new2yc on Friday March 31, 2023 @02:34AM (#35383449)
If you don't care about "performance":

```// Uses first 10 terms of Taylor Series double my_cos(double x) { double sum = 0; for (int n = 0; n < 10; n++) { sum += pow(-1, n) * pow(x, 2 * n) / factorial(2 * n); } return sum; }```

Re:Cosine Implementation in C by irchans on Friday March 31, 2023 @08:21AM (#35385976)
/* This code is silly, not super accurate, but fun /

double cos2(double x, int n) {

    double numerator = 1.0;

    double denominator = 1.0;

    double pi = 3.14159265358979323846;

    int i;

    for (i = 1; i <= n; ++i) {
        numerator   *= pow((x - n * pi / 2.0), 2);
        denominator *= pow((n * pi / 2.0), 2);
    }

    return numerator / denominator;
}

(Edit for format)

Re:Cosine Implementation in C by Sesse__ on Friday March 31, 2023 @01:44PM (#35390503)
Also if you don't care about accuracy at all. my_cos(2 * M_PI) = 0.9965. my_cos(4 * M_PI) = -2917.7144 (!).