Browse Source
Merge branch 'Reihen' into 'master'
Merge branch 'Reihen' into 'master'
Reihen See merge request fdai7848/Linkes-Twix!4master
fdai7848
11 months ago
11 changed files with 229 additions and 53 deletions
-
30src/exponent.c
-
2src/exponent.h
-
2src/logarithmus.c
-
83src/reihen.c
-
11src/reihen.h
-
28src/util.c
-
8src/util.h
-
2team.md
-
24test/test_exponent.c
-
2test/test_logarithmus.c
-
90test/test_reihen.c
@ -0,0 +1,83 @@ |
|||||
|
#include <stdio.h> |
||||
|
#include <limits.h> |
||||
|
#include <stdbool.h> |
||||
|
|
||||
|
#include "reihen.h" |
||||
|
#include "exponent.h" |
||||
|
#include "logarithmus.h" |
||||
|
#include "util.h" |
||||
|
|
||||
|
#define PI pi(1e-8) |
||||
|
|
||||
|
double exponential(double exp){ |
||||
|
double sum = 0.0; |
||||
|
for(int i = 0; i<=21; i++){ |
||||
|
sum += (1.0/fac(i)*(powerD(i,exp))); |
||||
|
} |
||||
|
return sum; |
||||
|
} |
||||
|
|
||||
|
double euler(){ |
||||
|
return exponential(1); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
double radians(double degrees){ |
||||
|
return degrees * PI / 180; |
||||
|
} |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
|
||||
|
double sinD(double degrees){ |
||||
|
if (degrees>=360.0){ |
||||
|
degrees = squashDegreesTo360(degrees); |
||||
|
} |
||||
|
|
||||
|
double sum = 0.0; |
||||
|
double radian = radians(degrees); |
||||
|
|
||||
|
for (int i = 0; i <= 9; i++){ |
||||
|
sum += powerD((double) i, -1.0) * (powerD(2.0*i+1, radian) / fac(2.0*i+1)); |
||||
|
} |
||||
|
|
||||
|
return sum; |
||||
|
} |
||||
|
|
||||
|
double cosD(double degrees){ |
||||
|
if (degrees>=360.0){ |
||||
|
degrees = squashDegreesTo360(degrees); |
||||
|
} |
||||
|
|
||||
|
double sum = 0.0; |
||||
|
double radian = radians(degrees); |
||||
|
|
||||
|
for (int i = 0; i <= 9; i++){ |
||||
|
sum += powerD((double) i, -1.0) * (powerD(2*i, radian) / fac(2*i)); |
||||
|
} |
||||
|
|
||||
|
return sum; |
||||
|
} |
||||
|
|
||||
|
double tanD(double degrees){ |
||||
|
if (degrees>=360.0){ |
||||
|
degrees = squashDegreesTo360(degrees); |
||||
|
} |
||||
|
if(degrees == 90.0 || degrees == 270.0){ |
||||
|
return -1.0; |
||||
|
} |
||||
|
return sinD(degrees)/cosD(degrees); |
||||
|
|
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
#ifndef REIHEN_H |
||||
|
#define REIHEN_H |
||||
|
|
||||
|
double exponential(double exp); |
||||
|
double euler(); |
||||
|
double pi(); |
||||
|
double radians(double degrees); |
||||
|
double sinD(double degrees); |
||||
|
double cosD(double degrees); |
||||
|
|
||||
|
#endif // reihen.h |
@ -0,0 +1,28 @@ |
|||||
|
#include <stdio.h> |
||||
|
#include <limits.h> |
||||
|
|
||||
|
#include "util.h" |
||||
|
|
||||
|
double absD(double x){ |
||||
|
if(x<0){ |
||||
|
return -1 * x; |
||||
|
} |
||||
|
return x; |
||||
|
} |
||||
|
|
||||
|
double squashDegreesTo360(double degrees){ |
||||
|
int multiple = degrees / 360; |
||||
|
return degrees - (360*multiple); |
||||
|
} |
||||
|
|
||||
|
unsigned long long fac(int x){ |
||||
|
unsigned long long prod = 1; |
||||
|
if(x==0) return 1; |
||||
|
for (int i = 1; i <= x; i++){ |
||||
|
if (prod > ULLONG_MAX / i){ |
||||
|
break; |
||||
|
} |
||||
|
prod*=i; |
||||
|
} |
||||
|
return prod; |
||||
|
} |
@ -0,0 +1,8 @@ |
|||||
|
#ifndef UTIL_H |
||||
|
#define UTIL_H |
||||
|
|
||||
|
double absD(double x); |
||||
|
double squashDegreesTo360(double degrees); |
||||
|
unsigned long long fac(int x); |
||||
|
|
||||
|
#endif // util.h |
@ -1,4 +1,4 @@ |
|||||
- Jan Heidekrüger, fdai7848 |
|
||||
|
- fdai7848, fdai7848 |
||||
- Inna Röhrig, fdai7851 |
- Inna Röhrig, fdai7851 |
||||
- Justin Trausch, fdai7720 |
- Justin Trausch, fdai7720 |
||||
- Sophia Weber, fdai7828 |
- Sophia Weber, fdai7828 |
||||
|
@ -0,0 +1,90 @@ |
|||||
|
#ifdef TEST |
||||
|
|
||||
|
#include "unity.h" |
||||
|
|
||||
|
#include "reihen.h" |
||||
|
#include "exponent.h" |
||||
|
#include "logarithmus.h" |
||||
|
#include "util.h" |
||||
|
|
||||
|
void setUp(void) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
void tearDown(void) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
void test_Exponent_with_positive_number(void){ |
||||
|
double r1, r2, r3; |
||||
|
|
||||
|
r1 = exponential(0.0); |
||||
|
r2 = exponential(1.0); |
||||
|
r3 = exponential(2.0); |
||||
|
|
||||
|
TEST_ASSERT_EQUAL_DOUBLE(1.0, r1); |
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 2.718282, r2); |
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.0001, 7.389056, r3); |
||||
|
} |
||||
|
|
||||
|
void test_Exponent_with_negative_number(void){ |
||||
|
double r1, r2, r3; |
||||
|
|
||||
|
r1 = exponential(-1.0); |
||||
|
r2 = exponential(-2.0); |
||||
|
|
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.367879, r1); |
||||
|
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); |
||||
|
} |
||||
|
|
||||
|
void test_degrees_to_radians(){ |
||||
|
double r1, r2, r3, r4; |
||||
|
|
||||
|
r1 = radians(90.0); |
||||
|
r2 = radians(180.0); |
||||
|
r3 = radians(51.6); |
||||
|
r4 = radians(36.97); |
||||
|
|
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 1.570796, r1); |
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, pi(1e-8), r2); |
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.900589, r3); |
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.645248, r4); |
||||
|
} |
||||
|
|
||||
|
void test_sin(void){ |
||||
|
double r1, r2, r3, r4; |
||||
|
|
||||
|
r1 = sinD(50.0); |
||||
|
r2 = sinD(90.0); |
||||
|
r3 = sinD(36.97); |
||||
|
r4 = sinD(180.0); |
||||
|
|
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.766044, r1); |
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 1.0, r2); |
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.601396, r3); |
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.0, r4); |
||||
|
} |
||||
|
|
||||
|
void test_cos(void){ |
||||
|
double r1, r2, r3, r4; |
||||
|
|
||||
|
r1 = cosD(50.0); |
||||
|
r2 = cosD(90.0); |
||||
|
r3 = cosD(36.97); |
||||
|
r4 = cosD(180.0); |
||||
|
|
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.642787, r1); |
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.0, r2); |
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.798950, r3); |
||||
|
TEST_ASSERT_DOUBLE_WITHIN(0.000001, -1.0, r4); |
||||
|
} |
||||
|
|
||||
|
#endif // TEST |
Write
Preview
Loading…
Cancel
Save
Reference in new issue