From 7cf036b53a00b519355788b6e491f2b8744e78df Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sat, 3 Feb 2024 17:08:32 +0100 Subject: [PATCH 01/14] add files for reihen --- src/reihen.c | 7 +++++++ src/reihen.h | 6 ++++++ test/test_reihen.c | 17 +++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 src/reihen.c create mode 100644 src/reihen.h create mode 100644 test/test_reihen.c diff --git a/src/reihen.c b/src/reihen.c new file mode 100644 index 0000000..cd80bc3 --- /dev/null +++ b/src/reihen.c @@ -0,0 +1,7 @@ +#include +#include +#include + +#include "reihen.h" +#include "exponent.h" +#include "logarithmus.h" \ No newline at end of file diff --git a/src/reihen.h b/src/reihen.h new file mode 100644 index 0000000..bb17ea0 --- /dev/null +++ b/src/reihen.h @@ -0,0 +1,6 @@ +#ifndef REIHEN_H +#define REIHEN_H + + + +#endif // reihen.h \ No newline at end of file diff --git a/test/test_reihen.c b/test/test_reihen.c new file mode 100644 index 0000000..043fc95 --- /dev/null +++ b/test/test_reihen.c @@ -0,0 +1,17 @@ +#ifdef TEST + +#include "unity.h" + +#include "reihen.h" +#include "exponent.h" +#include "logarithmus.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +#endif // TEST \ No newline at end of file From 12393766b6be409dfc01183bca558d9f1f2456a8 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sat, 3 Feb 2024 17:21:29 +0100 Subject: [PATCH 02/14] refactoring: move exponential function to reihen.c --- src/exponent.c | 22 +--------------------- src/exponent.h | 1 - src/logarithmus.c | 1 + src/reihen.c | 22 +++++++++++++++++++++- src/reihen.h | 2 +- test/test_exponent.c | 23 +---------------------- test/test_logarithmus.c | 1 + test/test_reihen.c | 22 ++++++++++++++++++++++ 8 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index bbe2221..8909d56 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -4,6 +4,7 @@ #include "exponent.h" #include "logarithmus.h" +#include "reihen.h" double absD(double x){ if(x<0){ @@ -49,24 +50,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..1264177 100644 --- a/src/exponent.h +++ b/src/exponent.h @@ -3,7 +3,6 @@ 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..fa6ea62 100644 --- a/src/logarithmus.c +++ b/src/logarithmus.c @@ -3,6 +3,7 @@ #include "logarithmus.h" #include "exponent.h" +#include "reihen.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 index cd80bc3..01fd0a5 100644 --- a/src/reihen.c +++ b/src/reihen.c @@ -4,4 +4,24 @@ #include "reihen.h" #include "exponent.h" -#include "logarithmus.h" \ No newline at end of file +#include "logarithmus.h" + +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; +} \ No newline at end of file diff --git a/src/reihen.h b/src/reihen.h index bb17ea0..bd4e520 100644 --- a/src/reihen.h +++ b/src/reihen.h @@ -1,6 +1,6 @@ #ifndef REIHEN_H #define REIHEN_H - +double exponential(double x); #endif // reihen.h \ No newline at end of file diff --git a/test/test_exponent.c b/test/test_exponent.c index ae49c01..d137507 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -4,6 +4,7 @@ #include "exponent.h" #include "logarithmus.h" +#include "reihen.h" void setUp(void) { @@ -101,26 +102,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..7c24c13 100644 --- a/test/test_logarithmus.c +++ b/test/test_logarithmus.c @@ -4,6 +4,7 @@ #include "logarithmus.h" #include "exponent.h" +#include "reihen.h" void setUp(void) { diff --git a/test/test_reihen.c b/test/test_reihen.c index 043fc95..13d490c 100644 --- a/test/test_reihen.c +++ b/test/test_reihen.c @@ -14,4 +14,26 @@ 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); +} + #endif // TEST \ No newline at end of file From ea2679568fbaa8ed25d54a9199b5979d97c49960 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sat, 3 Feb 2024 17:33:40 +0100 Subject: [PATCH 03/14] add function to get euler number --- src/reihen.c | 4 ++++ src/reihen.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/reihen.c b/src/reihen.c index 01fd0a5..b9f15e9 100644 --- a/src/reihen.c +++ b/src/reihen.c @@ -24,4 +24,8 @@ double exponential(double x){ sum += (1.0/fac(i)*(powerD(i,x))); } return sum; +} + +double euler(){ + return exponential(1); } \ No newline at end of file diff --git a/src/reihen.h b/src/reihen.h index bd4e520..20f8a18 100644 --- a/src/reihen.h +++ b/src/reihen.h @@ -2,5 +2,6 @@ #define REIHEN_H double exponential(double x); +double euler(); #endif // reihen.h \ No newline at end of file From 73e6889e5e8dcb0989b39b8269a50f130c17b1cd Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sat, 3 Feb 2024 19:10:27 +0100 Subject: [PATCH 04/14] add approximation for pi by gauss-legendre --- src/reihen.c | 15 +++++++++++++++ src/reihen.h | 1 + test/test_reihen.c | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/src/reihen.c b/src/reihen.c index b9f15e9..4e3ebb0 100644 --- a/src/reihen.c +++ b/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; } \ No newline at end of file diff --git a/src/reihen.h b/src/reihen.h index 20f8a18..36d8468 100644 --- a/src/reihen.h +++ b/src/reihen.h @@ -3,5 +3,6 @@ double exponential(double x); double euler(); +double pi(); #endif // reihen.h \ No newline at end of file diff --git a/test/test_reihen.c b/test/test_reihen.c index 13d490c..1329b6f 100644 --- a/test/test_reihen.c +++ b/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 \ No newline at end of file From 26be4d744ab3848b2dd2e9d18e2a67ade185390b Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sat, 3 Feb 2024 19:21:41 +0100 Subject: [PATCH 05/14] add function that converts degrees to radians --- src/reihen.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/reihen.c b/src/reihen.c index 4e3ebb0..b31d063 100644 --- a/src/reihen.c +++ b/src/reihen.c @@ -6,6 +6,8 @@ #include "exponent.h" #include "logarithmus.h" +#define PI pi(1e-8) + unsigned long long fac(int x){ unsigned long long prod = 1; if(x==0) return 1; @@ -30,6 +32,15 @@ double euler(){ return exponential(1); } +double squashDegreesTo360(double x){ + int multiple = x / 360; + return x - (360*multiple); +} + +double radians(double d){ + return d / 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; From 4b9f352ea1bedd83a775a5623215a360d563186d Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sat, 3 Feb 2024 19:42:50 +0100 Subject: [PATCH 06/14] add tests for radians function --- src/reihen.c | 2 +- src/reihen.h | 1 + test/test_reihen.c | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/reihen.c b/src/reihen.c index b31d063..258638c 100644 --- a/src/reihen.c +++ b/src/reihen.c @@ -38,7 +38,7 @@ double squashDegreesTo360(double x){ } double radians(double d){ - return d / PI * 180; + return d * PI / 180; } double pi(double eps){ diff --git a/src/reihen.h b/src/reihen.h index 36d8468..19f4b35 100644 --- a/src/reihen.h +++ b/src/reihen.h @@ -4,5 +4,6 @@ double exponential(double x); double euler(); double pi(); +double radians(double d); #endif // reihen.h \ No newline at end of file diff --git a/test/test_reihen.c b/test/test_reihen.c index 1329b6f..2a30593 100644 --- a/test/test_reihen.c +++ b/test/test_reihen.c @@ -44,4 +44,23 @@ void test_pi(void){ 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); + + printf("%lf\n", r1); + printf("%lf\n", r2); + printf("%lf\n", r3); + printf("%lf\n", r4); + + 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); +} + #endif // TEST \ No newline at end of file From c5fd68472a11586d7b94f268bd1b9c6f25fe2fa2 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sat, 3 Feb 2024 20:26:59 +0100 Subject: [PATCH 07/14] add sin functions and tests --- src/reihen.c | 20 ++++++++++++++++++++ src/reihen.h | 1 + test/test_reihen.c | 19 ++++++++++++++----- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/reihen.c b/src/reihen.c index 258638c..5fe2d8c 100644 --- a/src/reihen.c +++ b/src/reihen.c @@ -54,4 +54,24 @@ double pi(double eps){ pi = (a + b) * (a + b) / (4 * t); } while (absD(pi - piold) > eps); return pi; +} + +double sinD(double x){ + if (x>=360.0){ + x = squashDegreesTo360(x); + } + double sum = 0.0; + double currvalue; + x = radians(x); + double power; + unsigned long long faculty; + for (int i = 0; i <= 9; i++) + { + currvalue = 1.0; + currvalue *= powerD(2*i+1,x); + currvalue /= (double) fac(2*i+1); + if(i%2==0) sum += currvalue; + else sum -= currvalue; + } + return sum; } \ No newline at end of file diff --git a/src/reihen.h b/src/reihen.h index 19f4b35..f056dfe 100644 --- a/src/reihen.h +++ b/src/reihen.h @@ -5,5 +5,6 @@ double exponential(double x); double euler(); double pi(); double radians(double d); +double sinD(double d); #endif // reihen.h \ No newline at end of file diff --git a/test/test_reihen.c b/test/test_reihen.c index 2a30593..a24ecb7 100644 --- a/test/test_reihen.c +++ b/test/test_reihen.c @@ -52,15 +52,24 @@ void test_degrees_to_radians(){ r3 = radians(51.6); r4 = radians(36.97); - printf("%lf\n", r1); - printf("%lf\n", r2); - printf("%lf\n", r3); - printf("%lf\n", r4); - 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); +} + #endif // TEST \ No newline at end of file From 32b27094cf1565c777dd2b28318fdb16502fa86c Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sat, 3 Feb 2024 20:30:44 +0100 Subject: [PATCH 08/14] refactoring: change parameter and variable names --- src/reihen.c | 25 +++++++++++++------------ src/reihen.h | 6 +++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/reihen.c b/src/reihen.c index 5fe2d8c..649814a 100644 --- a/src/reihen.c +++ b/src/reihen.c @@ -20,10 +20,10 @@ unsigned long long fac(int x){ return prod; } -double exponential(double x){ +double exponential(double exp){ double sum = 0.0; for(int i = 0; i<=21; i++){ - sum += (1.0/fac(i)*(powerD(i,x))); + sum += (1.0/fac(i)*(powerD(i,exp))); } return sum; } @@ -32,13 +32,13 @@ double euler(){ return exponential(1); } -double squashDegreesTo360(double x){ - int multiple = x / 360; - return x - (360*multiple); +double squashDegreesTo360(double degrees){ + int multiple = degrees / 360; + return degrees - (360*multiple); } -double radians(double d){ - return d * PI / 180; +double radians(double degrees){ + return degrees * PI / 180; } double pi(double eps){ @@ -56,19 +56,20 @@ double pi(double eps){ return pi; } -double sinD(double x){ - if (x>=360.0){ - x = squashDegreesTo360(x); +double sinD(double degrees){ + + if (degrees>=360.0){ + degrees = squashDegreesTo360(degrees); } double sum = 0.0; double currvalue; - x = radians(x); + double radian = radians(degrees); double power; unsigned long long faculty; for (int i = 0; i <= 9; i++) { currvalue = 1.0; - currvalue *= powerD(2*i+1,x); + currvalue *= powerD(2*i+1, radian); currvalue /= (double) fac(2*i+1); if(i%2==0) sum += currvalue; else sum -= currvalue; diff --git a/src/reihen.h b/src/reihen.h index f056dfe..d9cdf38 100644 --- a/src/reihen.h +++ b/src/reihen.h @@ -1,10 +1,10 @@ #ifndef REIHEN_H #define REIHEN_H -double exponential(double x); +double exponential(double exp); double euler(); double pi(); -double radians(double d); -double sinD(double d); +double radians(double degrees); +double sinD(double degrees); #endif // reihen.h \ No newline at end of file From 7bfc5a1fcef49185f84ae2694dd16e1f1d33cf47 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sat, 3 Feb 2024 20:41:46 +0100 Subject: [PATCH 09/14] refactoring: move helper functions to util.c --- src/exponent.c | 8 +------- src/logarithmus.c | 1 + src/reihen.c | 17 +---------------- src/util.c | 28 ++++++++++++++++++++++++++++ src/util.h | 8 ++++++++ test/test_exponent.c | 1 + test/test_logarithmus.c | 1 + test/test_reihen.c | 1 + 8 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 src/util.c create mode 100644 src/util.h diff --git a/src/exponent.c b/src/exponent.c index 8909d56..2e0d5b3 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -5,13 +5,7 @@ #include "exponent.h" #include "logarithmus.h" #include "reihen.h" - -double absD(double x){ - if(x<0){ - return -1 * x; - } - return x; -} +#include "util.h" double p(double exp, double base){ double prod = 1.0; diff --git a/src/logarithmus.c b/src/logarithmus.c index fa6ea62..2d440f9 100644 --- a/src/logarithmus.c +++ b/src/logarithmus.c @@ -4,6 +4,7 @@ #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 index 649814a..e021325 100644 --- a/src/reihen.c +++ b/src/reihen.c @@ -5,21 +5,10 @@ #include "reihen.h" #include "exponent.h" #include "logarithmus.h" +#include "util.h" #define PI pi(1e-8) -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 exp){ double sum = 0.0; for(int i = 0; i<=21; i++){ @@ -32,10 +21,6 @@ double euler(){ return exponential(1); } -double squashDegreesTo360(double degrees){ - int multiple = degrees / 360; - return degrees - (360*multiple); -} double radians(double degrees){ return degrees * PI / 180; 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/test/test_exponent.c b/test/test_exponent.c index d137507..bd2fdda 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -5,6 +5,7 @@ #include "exponent.h" #include "logarithmus.h" #include "reihen.h" +#include "util.h" void setUp(void) { diff --git a/test/test_logarithmus.c b/test/test_logarithmus.c index 7c24c13..e8f8da9 100644 --- a/test/test_logarithmus.c +++ b/test/test_logarithmus.c @@ -5,6 +5,7 @@ #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 index a24ecb7..f75c8fa 100644 --- a/test/test_reihen.c +++ b/test/test_reihen.c @@ -5,6 +5,7 @@ #include "reihen.h" #include "exponent.h" #include "logarithmus.h" +#include "util.h" void setUp(void) { From ae634ac3a2c467b76409738c9bb3012715302647 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sun, 4 Feb 2024 15:29:21 +0100 Subject: [PATCH 10/14] add cos function --- src/exponent.h | 1 - src/reihen.c | 21 ++++++++++++++++++++- src/reihen.h | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/exponent.h b/src/exponent.h index 1264177..e20413c 100644 --- a/src/exponent.h +++ b/src/exponent.h @@ -3,6 +3,5 @@ double p(double exp, double base); double powerD(double exp, double base); -double absD(double x); #endif // exponent.h \ No newline at end of file diff --git a/src/reihen.c b/src/reihen.c index e021325..2190881 100644 --- a/src/reihen.c +++ b/src/reihen.c @@ -42,7 +42,6 @@ double pi(double eps){ } double sinD(double degrees){ - if (degrees>=360.0){ degrees = squashDegreesTo360(degrees); } @@ -60,4 +59,24 @@ double sinD(double degrees){ else sum -= currvalue; } return sum; +} + +double cosD(double degrees){ + if (degrees>=360.0){ + degrees = squashDegreesTo360(degrees); + } + double sum = 0.0; + double currvalue; + double radian = radians(degrees); + double power; + unsigned long long faculty; + for (int i = 0; i <= 9; i++) + { + currvalue = 1.0; + currvalue *= powerD(2*i, radian); + currvalue /= fac(2*i); + if(i%2==0) sum += currvalue; + else sum -= currvalue; + } + return sum; } \ No newline at end of file diff --git a/src/reihen.h b/src/reihen.h index d9cdf38..cb15a78 100644 --- a/src/reihen.h +++ b/src/reihen.h @@ -6,5 +6,6 @@ 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 From 08d0ad16e75a8dd31432ba85346589325fa10b51 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sun, 4 Feb 2024 15:30:10 +0100 Subject: [PATCH 11/14] add tests for cos function --- test/test_reihen.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/test_reihen.c b/test/test_reihen.c index f75c8fa..a020dec 100644 --- a/test/test_reihen.c +++ b/test/test_reihen.c @@ -73,4 +73,23 @@ void test_sin(void){ 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); + + printf("%lf\n", r1); + printf("%lf\n", r2); + printf("%lf\n", r3); + printf("%lf\n", r4); + + 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 From b20392a5acf56766f42b7b874e44891ddb6436e3 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sun, 4 Feb 2024 15:45:23 +0100 Subject: [PATCH 12/14] add tangens function --- src/reihen.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/reihen.c b/src/reihen.c index 2190881..556bece 100644 --- a/src/reihen.c +++ b/src/reihen.c @@ -79,4 +79,15 @@ double cosD(double degrees){ else sum -= currvalue; } 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 From 9feab8998d4028ce66bb2ecc8bc11373285099fc Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sun, 4 Feb 2024 15:53:35 +0100 Subject: [PATCH 13/14] refactoring: reduce use of variables in sin and cos function and delete print functions in tests --- src/reihen.c | 30 ++++++++++-------------------- test/test_reihen.c | 5 ----- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/reihen.c b/src/reihen.c index 556bece..b58b27e 100644 --- a/src/reihen.c +++ b/src/reihen.c @@ -45,19 +45,14 @@ double sinD(double degrees){ if (degrees>=360.0){ degrees = squashDegreesTo360(degrees); } + double sum = 0.0; - double currvalue; double radian = radians(degrees); - double power; - unsigned long long faculty; - for (int i = 0; i <= 9; i++) - { - currvalue = 1.0; - currvalue *= powerD(2*i+1, radian); - currvalue /= (double) fac(2*i+1); - if(i%2==0) sum += currvalue; - else sum -= currvalue; + + 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; } @@ -65,19 +60,14 @@ double cosD(double degrees){ if (degrees>=360.0){ degrees = squashDegreesTo360(degrees); } + double sum = 0.0; - double currvalue; double radian = radians(degrees); - double power; - unsigned long long faculty; - for (int i = 0; i <= 9; i++) - { - currvalue = 1.0; - currvalue *= powerD(2*i, radian); - currvalue /= fac(2*i); - if(i%2==0) sum += currvalue; - else sum -= currvalue; + + for (int i = 0; i <= 9; i++){ + sum += powerD((double) i, -1.0) * (powerD(2*i, radian) / fac(2*i)); } + return sum; } diff --git a/test/test_reihen.c b/test/test_reihen.c index a020dec..5638120 100644 --- a/test/test_reihen.c +++ b/test/test_reihen.c @@ -81,11 +81,6 @@ void test_cos(void){ r3 = cosD(36.97); r4 = cosD(180.0); - printf("%lf\n", r1); - printf("%lf\n", r2); - printf("%lf\n", r3); - printf("%lf\n", r4); - 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); From 53203e6e873f0946cd3227b6aebbf46ae0fbc1d1 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Wed, 7 Feb 2024 14:46:52 +0100 Subject: [PATCH 14/14] =?UTF-8?q?=C3=A4nderung=20an=20team.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- team.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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