Browse Source

add approximation for pi by gauss-legendre

master
fdai7848 11 months ago
parent
commit
73e6889e5e
  1. 15
      src/reihen.c
  2. 1
      src/reihen.h
  3. 8
      test/test_reihen.c

15
src/reihen.c

@ -28,4 +28,19 @@ double exponential(double x){
double euler(){
return exponential(1);
}
double pi(double eps){
double a = 1, b = 1/powerD(0.5, 2), t = 0.25, p = 1;
double aNew, piold, pi = 1;
do{
aNew = .5 * (a + b);
b = powerD(0.5 , a * b);
t -= p * (a - aNew) * (a - aNew);
a = aNew;
p *= 2;
piold = pi;
pi = (a + b) * (a + b) / (4 * t);
} while (absD(pi - piold) > eps);
return pi;
}

1
src/reihen.h

@ -3,5 +3,6 @@
double exponential(double x);
double euler();
double pi();
#endif // reihen.h

8
test/test_reihen.c

@ -36,4 +36,12 @@ void test_Exponent_with_negative_number(void){
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.135335, r2);
}
void test_pi(void){
double r1;
r1 = pi(1e-8);
TEST_ASSERT_DOUBLE_WITHIN(0.0000001, 3.14159265, r1);
}
#endif // TEST
Loading…
Cancel
Save