From 9feab8998d4028ce66bb2ecc8bc11373285099fc Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sun, 4 Feb 2024 15:53:35 +0100 Subject: [PATCH] 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);