Skip to content

Instantly share code, notes, and snippets.

@okutane
Created March 12, 2020 04:01
Show Gist options
  • Save okutane/7ce7f65cc18f9c2da48c17d8a70da2a3 to your computer and use it in GitHub Desktop.
Save okutane/7ce7f65cc18f9c2da48c17d8a70da2a3 to your computer and use it in GitHub Desktop.
Taylor series for cos
#include <stdio.h>
#include <math.h>
double my_cos(double x) {
int n = 2;
double result = 1;
double x2 = x * x;
double a = -x * x / 2;
while (fabs(a) > 0.0001) {
result += a;
a = -a * x * x / (n * (n + 1));
n += 2;
}
return result;
}
int main(int argc, char **argv) {
int i;
double x;
printf(" x my_cos cos\n");
for (i = 0; i <= 10; i++) {
x = 0.1 * i;
printf("%.1f %f %f\n", x, my_cos(x), cos(x));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment