From c5fd68472a11586d7b94f268bd1b9c6f25fe2fa2 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sat, 3 Feb 2024 20:26:59 +0100 Subject: [PATCH] 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