diff --git a/src/exponent.c b/src/exponent.c index bbe2221..2e0d5b3 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -4,13 +4,8 @@ #include "exponent.h" #include "logarithmus.h" - -double absD(double x){ - if(x<0){ - return -1 * x; - } - return x; -} +#include "reihen.h" +#include "util.h" double p(double exp, double base){ double prod = 1.0; @@ -49,24 +44,3 @@ double powerD(double exp, double base){ return -1.0; } -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; -} - - -double exponential(double x){ - double sum = 0.0; - for(int i = 0; i<=21; i++){ - sum += (1.0/fac(i)*(powerD(i,x))); - } - return sum; -} - diff --git a/src/exponent.h b/src/exponent.h index 9b53200..e20413c 100644 --- a/src/exponent.h +++ b/src/exponent.h @@ -3,7 +3,5 @@ double p(double exp, double base); double powerD(double exp, double base); -double exponential(double x); -double absD(double x); #endif // exponent.h \ No newline at end of file diff --git a/src/logarithmus.c b/src/logarithmus.c index 1c613e5..2d440f9 100644 --- a/src/logarithmus.c +++ b/src/logarithmus.c @@ -3,6 +3,8 @@ #include "logarithmus.h" #include "exponent.h" +#include "reihen.h" +#include "util.h" double logX(double base, double value){ if(base == 1.0 || base <= 0.0 || value <= 0.0){ diff --git a/src/reihen.c b/src/reihen.c new file mode 100644 index 0000000..b58b27e --- /dev/null +++ b/src/reihen.c @@ -0,0 +1,83 @@ +#include +#include +#include + +#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); + +} \ No newline at end of file diff --git a/src/reihen.h b/src/reihen.h new file mode 100644 index 0000000..cb15a78 --- /dev/null +++ b/src/reihen.h @@ -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 \ No newline at end of file diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..6930ec9 --- /dev/null +++ b/src/util.c @@ -0,0 +1,28 @@ +#include +#include + +#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; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..76eb4ba --- /dev/null +++ b/src/util.h @@ -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 \ No newline at end of file diff --git a/team.md b/team.md index 2fa5436..e3d7d03 100644 --- a/team.md +++ b/team.md @@ -1,4 +1,4 @@ -- Jan Heidekrüger, fdai7848 +- fdai7848, fdai7848 - Inna Röhrig, fdai7851 - Justin Trausch, fdai7720 - Sophia Weber, fdai7828 diff --git a/test/test_exponent.c b/test/test_exponent.c index ae49c01..bd2fdda 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -4,6 +4,8 @@ #include "exponent.h" #include "logarithmus.h" +#include "reihen.h" +#include "util.h" void setUp(void) { @@ -101,26 +103,4 @@ void test_exponent_edge_case(void){ TEST_ASSERT_DOUBLE_WITHIN(0.0001, -1.0, r5); } -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); -} - #endif // TEST \ No newline at end of file diff --git a/test/test_logarithmus.c b/test/test_logarithmus.c index 0f7aba1..e8f8da9 100644 --- a/test/test_logarithmus.c +++ b/test/test_logarithmus.c @@ -4,6 +4,8 @@ #include "logarithmus.h" #include "exponent.h" +#include "reihen.h" +#include "util.h" void setUp(void) { diff --git a/test/test_reihen.c b/test/test_reihen.c new file mode 100644 index 0000000..5638120 --- /dev/null +++ b/test/test_reihen.c @@ -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 \ No newline at end of file