Skip to content

Instantly share code, notes, and snippets.

@limtbk
Last active January 23, 2018 05:30
Show Gist options
  • Save limtbk/9c36d231320283fdac73eb693c90b685 to your computer and use it in GitHub Desktop.
Save limtbk/9c36d231320283fdac73eb693c90b685 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define abs(x) ((x)<0 ? -(x) : (x))
double calc_prec(double x, double pr)
{
double s = 0;
double n = 1;
double k = 1;
double xx = x;
if ((x > -1) && (x <= 1)) {
double a = xx / n;
while (abs (a) > pr) {
//printf("%f\n", s);
s = s + k * a;
xx = xx * x;
n = n + 1;
k = -k;
a = xx / n;
}
}
return s;
}
int main()
{
printf("%f\n", calc_prec(0.5, 0.0001));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment