From 8e8b79ac14859007b7ae09ffa3cc1d9d8fea7e36 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Mon, 29 Jan 2024 09:59:18 +0100 Subject: [PATCH 01/24] Create exp files --- src/exponent.c | 5 +++++ src/exponent.h | 6 ++++++ test/test_exponent.c | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 src/exponent.c create mode 100644 src/exponent.h create mode 100644 test/test_exponent.c diff --git a/src/exponent.c b/src/exponent.c new file mode 100644 index 0000000..413f97c --- /dev/null +++ b/src/exponent.c @@ -0,0 +1,5 @@ +#include "exponent.h" + +int expI(unsigned int e, int num){ + return 0; +} \ No newline at end of file diff --git a/src/exponent.h b/src/exponent.h new file mode 100644 index 0000000..1fea7aa --- /dev/null +++ b/src/exponent.h @@ -0,0 +1,6 @@ +#ifndef EXPONENT_H +#define EXPONENT_H + +int expI(unsigned int e, int num); + +#endif // exponent.h \ No newline at end of file diff --git a/test/test_exponent.c b/test/test_exponent.c new file mode 100644 index 0000000..b0edf7f --- /dev/null +++ b/test/test_exponent.c @@ -0,0 +1,21 @@ +#ifdef TEST + +#include "unity.h" + +#include "exponent.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_IntegerExponent_NeedToImplement(void) +{ + TEST_IGNORE_MESSAGE("Integer Exponent needs to be implemented"); +} + + +#endif // TEST \ No newline at end of file From fdded408b1354efd2b62a2392dcb64180a71d5b5 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Mon, 29 Jan 2024 10:06:49 +0100 Subject: [PATCH 02/24] Add functionality to Exponent Function --- src/exponent.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/exponent.c b/src/exponent.c index 413f97c..ca2438f 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -1,5 +1,13 @@ #include "exponent.h" int expI(unsigned int e, int num){ - return 0; + int prod = 1; + if(e==0){ + return 1; + }else{ + for(int i = 1; i <=e; i++){ + prod *= num; + } + return prod; + } } \ No newline at end of file From e7902efcd3af770d32bfd135dada2d59979ee873 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Mon, 29 Jan 2024 10:27:38 +0100 Subject: [PATCH 03/24] Add Test for positive integer exponent function --- test/test_exponent.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/test_exponent.c b/test/test_exponent.c index b0edf7f..02503f4 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -12,9 +12,16 @@ void tearDown(void) { } -void test_IntegerExponent_NeedToImplement(void) -{ - TEST_IGNORE_MESSAGE("Integer Exponent needs to be implemented"); +void test_IntegerExponent_ForPositiveExponent(void){ + int r1, r2, r3; + + r1 = expI(2, -2); + r2 = expI(3, -3); + r3 = expI(4, 5); + + TEST_ASSERT_EQUAL_INT(4, r1); + TEST_ASSERT_EQUAL_INT(-27, r2); + TEST_ASSERT_EQUAL_INT(625, r3); } From f157389255373ae0067afb097aa3c56a1d98a50b Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Mon, 29 Jan 2024 10:39:26 +0100 Subject: [PATCH 04/24] add support for negative exponents --- src/exponent.c | 8 ++++++++ src/exponent.h | 1 + 2 files changed, 9 insertions(+) diff --git a/src/exponent.c b/src/exponent.c index ca2438f..00a5e39 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -10,4 +10,12 @@ int expI(unsigned int e, int num){ } return prod; } +} + +float expIN(int e, int num){ + if(e>0){ + return (float) expI(e, num); + }else{ + return 1.0 / (float) expI(e, num); + } } \ No newline at end of file diff --git a/src/exponent.h b/src/exponent.h index 1fea7aa..6963b46 100644 --- a/src/exponent.h +++ b/src/exponent.h @@ -2,5 +2,6 @@ #define EXPONENT_H int expI(unsigned int e, int num); +float expIN(int e, int num); #endif // exponent.h \ No newline at end of file From 807d1b7b6b23713bf46429065ca66d5fa1f57df9 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Mon, 29 Jan 2024 10:59:42 +0100 Subject: [PATCH 05/24] Add Test for Negative Exponents --- src/exponent.c | 6 +++++- test/test_exponent.c | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/exponent.c b/src/exponent.c index 00a5e39..6b82961 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -1,3 +1,5 @@ +#include + #include "exponent.h" int expI(unsigned int e, int num){ @@ -16,6 +18,8 @@ float expIN(int e, int num){ if(e>0){ return (float) expI(e, num); }else{ - return 1.0 / (float) expI(e, num); + printf("%d, %d\n", e, num); + printf("%f\n", 1.0 / (float) expI((e*-1), num)); + return (1.0 / (float) expI((e*-1), num)); } } \ No newline at end of file diff --git a/test/test_exponent.c b/test/test_exponent.c index 02503f4..acfe8f1 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -24,5 +24,21 @@ void test_IntegerExponent_ForPositiveExponent(void){ TEST_ASSERT_EQUAL_INT(625, r3); } +void test_IntegerExponent_ForNegativeExponent(void){ + float r1, r2, r3; + + r1 = expIN(-1, 2); + r2 = expIN(-3, -3); + r3 = expIN(-2, -2); + + printf("Result 1: %d\n", r1); + printf("Result 2: %d\n", r2); + printf("Result 3: %d\n", r3); + + TEST_ASSERT_EQUAL_FLOAT(0.5, r1); + TEST_ASSERT_EQUAL_FLOAT(-0.0370370373, r2); + TEST_ASSERT_EQUAL_FLOAT(0.25, r3); +} + #endif // TEST \ No newline at end of file From 4b9c7d8e14d8d7b758a844be88f572c59a915bdd Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Mon, 29 Jan 2024 11:07:56 +0100 Subject: [PATCH 06/24] add support for float basis --- src/exponent.c | 12 ++++++++++++ src/exponent.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/exponent.c b/src/exponent.c index 6b82961..37642e6 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -22,4 +22,16 @@ float expIN(int e, int num){ printf("%f\n", 1.0 / (float) expI((e*-1), num)); return (1.0 / (float) expI((e*-1), num)); } +} + +float expF(int e, float num){ + float prod = 1; + if(e==0){ + return 1; + }else{ + for(int i = 1; i <=e; i++){ + prod *= num; + } + return prod; + } } \ No newline at end of file diff --git a/src/exponent.h b/src/exponent.h index 6963b46..f7a030c 100644 --- a/src/exponent.h +++ b/src/exponent.h @@ -3,5 +3,6 @@ int expI(unsigned int e, int num); float expIN(int e, int num); +float expF(int e, float num); #endif // exponent.h \ No newline at end of file From 0db2ed8c28b640dbd3b254630fe9c4f5ff05e0bd Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Mon, 29 Jan 2024 17:23:58 +0100 Subject: [PATCH 07/24] refactoring: changing functions for double types --- src/exponent.c | 28 +++++++--------------------- src/exponent.h | 5 ++--- test/test_exponent.c | 22 +++++++++++----------- 3 files changed, 20 insertions(+), 35 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index 37642e6..d9c7223 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -2,36 +2,22 @@ #include "exponent.h" -int expI(unsigned int e, int num){ - int prod = 1; - if(e==0){ +double expI(double e, double num){ + double prod = 1.0; + if(e==0.0){ return 1; }else{ - for(int i = 1; i <=e; i++){ + for(int i = 1; i <= (int) e; i++){ prod *= num; } return prod; } } -float expIN(int e, int num){ +double expIN(double e, double num){ if(e>0){ - return (float) expI(e, num); + return expI(e, num); }else{ - printf("%d, %d\n", e, num); - printf("%f\n", 1.0 / (float) expI((e*-1), num)); - return (1.0 / (float) expI((e*-1), num)); - } -} - -float expF(int e, float num){ - float prod = 1; - if(e==0){ - return 1; - }else{ - for(int i = 1; i <=e; i++){ - prod *= num; - } - return prod; + return (1.0 / expI((e*-1), num)); } } \ No newline at end of file diff --git a/src/exponent.h b/src/exponent.h index f7a030c..8f77ae2 100644 --- a/src/exponent.h +++ b/src/exponent.h @@ -1,8 +1,7 @@ #ifndef EXPONENT_H #define EXPONENT_H -int expI(unsigned int e, int num); -float expIN(int e, int num); -float expF(int e, float num); +double expI(double e, double num); +double expIN(double e, double num); #endif // exponent.h \ No newline at end of file diff --git a/test/test_exponent.c b/test/test_exponent.c index acfe8f1..72bc9fc 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -12,24 +12,24 @@ void tearDown(void) { } -void test_IntegerExponent_ForPositiveExponent(void){ +void test_DoubleBase_ForPositiveExponent(void){ int r1, r2, r3; - r1 = expI(2, -2); - r2 = expI(3, -3); - r3 = expI(4, 5); + r1 = expI(2.0, -2.0); + r2 = expI(3.0, -3.0); + r3 = expI(4.0, 5.0); - TEST_ASSERT_EQUAL_INT(4, r1); - TEST_ASSERT_EQUAL_INT(-27, r2); - TEST_ASSERT_EQUAL_INT(625, r3); + TEST_ASSERT_EQUAL_INT(4.0, r1); + TEST_ASSERT_EQUAL_INT(-27.0, r2); + TEST_ASSERT_EQUAL_INT(625.0, r3); } -void test_IntegerExponent_ForNegativeExponent(void){ +void test_DoubleBase_ForNegativeExponent(void){ float r1, r2, r3; - r1 = expIN(-1, 2); - r2 = expIN(-3, -3); - r3 = expIN(-2, -2); + r1 = expIN(-1.0, 2.0); + r2 = expIN(-3.0, -3.0); + r3 = expIN(-2.0, -2.0); printf("Result 1: %d\n", r1); printf("Result 2: %d\n", r2); From 8e416217472864cc1cc9abbbedf095fe29597e79 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Mon, 29 Jan 2024 17:43:37 +0100 Subject: [PATCH 08/24] Add check for integer exponent --- src/exponent.c | 12 ++++++++---- test/test_exponent.c | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index d9c7223..3f4895a 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -15,9 +15,13 @@ double expI(double e, double num){ } double expIN(double e, double num){ - if(e>0){ - return expI(e, num); - }else{ - return (1.0 / expI((e*-1), num)); + if(e - ((int) e) < 0.0001 || e - ((int) e) > -0.0001){ + if(e>0){ + return expI(e, num); + } + else{ + return (1.0 / expI((e*-1), num)); + } } + return 0; } \ No newline at end of file diff --git a/test/test_exponent.c b/test/test_exponent.c index 72bc9fc..c9262dc 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -31,9 +31,9 @@ void test_DoubleBase_ForNegativeExponent(void){ r2 = expIN(-3.0, -3.0); r3 = expIN(-2.0, -2.0); - printf("Result 1: %d\n", r1); - printf("Result 2: %d\n", r2); - printf("Result 3: %d\n", r3); + printf("Result 1: %f\n", r1); + printf("Result 2: %f\n", r2); + printf("Result 3: %f\n", r3); TEST_ASSERT_EQUAL_FLOAT(0.5, r1); TEST_ASSERT_EQUAL_FLOAT(-0.0370370373, r2); From 1b96219fd5e6cff2ebd8737f99aa3d21a826cf26 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Tue, 30 Jan 2024 09:15:31 +0100 Subject: [PATCH 09/24] add a temporary faculty function --- src/exponent.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/exponent.c b/src/exponent.c index 3f4895a..e82de4e 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -1,4 +1,5 @@ #include +#include #include "exponent.h" @@ -24,4 +25,17 @@ double expIN(double e, double num){ } } return 0; -} \ No newline at end of file +} + +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; +} + From 1a5cd4f32eb3d103310b61cbeaba44558790f19e Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Tue, 30 Jan 2024 11:39:35 +0100 Subject: [PATCH 10/24] add exponential function --- src/exponent.c | 8 ++++++++ src/exponent.h | 1 + 2 files changed, 9 insertions(+) diff --git a/src/exponent.c b/src/exponent.c index e82de4e..b1135fd 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -39,3 +39,11 @@ unsigned long long fac(int x){ return prod; } + +double exponential(double x){ + double sum = 0.0; + for(int i = 1; i<=21; i++){ + sum += (1/fac(i)*(expIN(i,x))); + } + return sum; +} \ No newline at end of file diff --git a/src/exponent.h b/src/exponent.h index 8f77ae2..8a09fec 100644 --- a/src/exponent.h +++ b/src/exponent.h @@ -3,5 +3,6 @@ double expI(double e, double num); double expIN(double e, double num); +double exponential(double x); #endif // exponent.h \ No newline at end of file From 0b4772466eeb0c77e5c746c9149cd0ee4eec4304 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Tue, 30 Jan 2024 16:34:41 +0100 Subject: [PATCH 11/24] add tests for exponential function with positive numbers --- src/exponent.c | 4 ++-- test/test_exponent.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index b1135fd..2cfecf5 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -42,8 +42,8 @@ unsigned long long fac(int x){ double exponential(double x){ double sum = 0.0; - for(int i = 1; i<=21; i++){ - sum += (1/fac(i)*(expIN(i,x))); + for(int i = 0; i<=21; i++){ + sum += (1.0/fac(i)*(expIN(i,x))); } return sum; } \ No newline at end of file diff --git a/test/test_exponent.c b/test/test_exponent.c index c9262dc..6d53ef1 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -40,5 +40,22 @@ void test_DoubleBase_ForNegativeExponent(void){ TEST_ASSERT_EQUAL_FLOAT(0.25, r3); } +void test_Exponent_with_positive_number(void){ + double r1, r2, r3; + + r1 = exponential(0.0); + r2 = exponential(1.0); + r3 = exponential(2.0); + + printf("Result 1: %8.lf\n", r1); + printf("Result 2: %lf\n", r2); + printf("Result 3: %lf\n", r3); + + 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); +} + + #endif // TEST \ No newline at end of file From 22e4cbf852bcb39f8fcfada3f796bd485d70e141 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Wed, 31 Jan 2024 09:57:18 +0100 Subject: [PATCH 12/24] add test for exponential function with negative numbers --- test/test_exponent.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test_exponent.c b/test/test_exponent.c index 6d53ef1..e517280 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -56,6 +56,19 @@ void test_Exponent_with_positive_number(void){ 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); + + printf("Result 1: %lf\n", r1); + printf("Result 2: %lf\n", r2); + + 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 79c38646875acdb2146bde63c9f16fbcc023cc8c Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Wed, 31 Jan 2024 10:35:43 +0100 Subject: [PATCH 13/24] implement first revision for ln with halley-newton-approx --- src/exponent.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/exponent.c b/src/exponent.c index 2cfecf5..b37deab 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -46,4 +46,24 @@ double exponential(double x){ sum += (1.0/fac(i)*(expIN(i,x))); } return sum; +} + +double absD(double x){ + if(x<0){ + return -1 * x; + } + return x; +} + +double logN(double x, double eps){ + double yn = x - 1.0; + double yn1 = yn; + + do + { + yn = yn1; + yn1 = yn + 2 * (x - exponential(yn)) / (x + exponential(yn)); + } while (absD(yn - yn1) > eps); + + return yn1; } \ No newline at end of file From f43c8409fe94af90962bbb3a8005ed180a887616 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Wed, 31 Jan 2024 10:56:49 +0100 Subject: [PATCH 14/24] Add test for ln function for positive numbers --- src/exponent.h | 1 + test/test_exponent.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/exponent.h b/src/exponent.h index 8a09fec..c869156 100644 --- a/src/exponent.h +++ b/src/exponent.h @@ -4,5 +4,6 @@ double expI(double e, double num); double expIN(double e, double num); double exponential(double x); +double logN(double x, double eps); #endif // exponent.h \ No newline at end of file diff --git a/test/test_exponent.c b/test/test_exponent.c index e517280..8545529 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -69,6 +69,22 @@ void test_Exponent_with_negative_number(void){ TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.135335, r2); } +void test_ln_with_positive_number(void){ + double r1, r2, r3; + + r1 = logN(1.0, 0.000001); + r2 = logN(10.0, 0.000001); + r3 = logN(2.5, 0.000001); + + printf("Result 1: %lf\n", r1); + printf("Result 2: %lf\n", r2); + printf("Result 3: %lf\n", r3); + + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0, r1); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.302585, r2); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.916290, r3); +} + #endif // TEST \ No newline at end of file From fef22fedff07936979fb3cb8b58f39f1b8c7863b Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Wed, 31 Jan 2024 19:31:59 +0100 Subject: [PATCH 15/24] test ln functions for values smaller than 0 and 0 --- src/exponent.c | 5 ++++- test/test_exponent.c | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/exponent.c b/src/exponent.c index b37deab..03f4607 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -16,7 +16,7 @@ double expI(double e, double num){ } double expIN(double e, double num){ - if(e - ((int) e) < 0.0001 || e - ((int) e) > -0.0001){ + if(e - ((int) e) < 0.000001 || e - ((int) e) > -0.000001){ if(e>0){ return expI(e, num); } @@ -56,6 +56,9 @@ double absD(double x){ } double logN(double x, double eps){ + if(x <= 0){ + return -1.0; + } double yn = x - 1.0; double yn1 = yn; diff --git a/test/test_exponent.c b/test/test_exponent.c index 8545529..8bd76c9 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -85,6 +85,21 @@ void test_ln_with_positive_number(void){ TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.916290, r3); } +void test_ln_with_zero_and_negative_numbers(void){ + double r1, r2, r3; + + r1 = logN(0.0, 0.000001); + r2 = logN(-1.0, 0.000001); + r3 = logN(-2.5, 0.000001); + + printf("Result 1: %lf\n", r1); + printf("Result 2: %lf\n", r2); + printf("Result 3: %lf\n", r3); + + TEST_ASSERT_EQUAL_DOUBLE(-1.0, r1); + TEST_ASSERT_EQUAL_DOUBLE(-1.0, r2); + TEST_ASSERT_EQUAL_DOUBLE(-1.0, r3); +} #endif // TEST \ No newline at end of file From b8230bab231b4a50e18865e93a2c53d6ac3f09dc Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Wed, 31 Jan 2024 21:09:44 +0100 Subject: [PATCH 16/24] add functionality for decimal Exponents --- src/exponent.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/exponent.c b/src/exponent.c index 03f4607..2227027 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -23,6 +23,10 @@ double expIN(double e, double num){ else{ return (1.0 / expI((e*-1), num)); } + }else{ + if(e>0){ + return exponential(e*logN(num, 0.000001)); + } } return 0; } From 650e1f439da5252bed383000b3bf42a196d6e2bc Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Wed, 31 Jan 2024 22:07:49 +0100 Subject: [PATCH 17/24] Add test for decimal exponents --- src/exponent.c | 22 ++++++++++++---------- test/test_exponent.c | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index 2227027..bef7597 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -3,6 +3,13 @@ #include "exponent.h" +double absD(double x){ + if(x<0){ + return -1 * x; + } + return x; +} + double expI(double e, double num){ double prod = 1.0; if(e==0.0){ @@ -16,16 +23,16 @@ double expI(double e, double num){ } double expIN(double e, double num){ - if(e - ((int) e) < 0.000001 || e - ((int) e) > -0.000001){ - if(e>0){ + if(absD(e) - ((int) absD(e)) < 0.000001){ + if(e>0.0){ return expI(e, num); } else{ return (1.0 / expI((e*-1), num)); } }else{ - if(e>0){ - return exponential(e*logN(num, 0.000001)); + if(e>0.0){ + return exponential(e*logN(num, 0.00000001)); } } return 0; @@ -52,12 +59,7 @@ double exponential(double x){ return sum; } -double absD(double x){ - if(x<0){ - return -1 * x; - } - return x; -} + double logN(double x, double eps){ if(x <= 0){ diff --git a/test/test_exponent.c b/test/test_exponent.c index 8bd76c9..04d8725 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -40,6 +40,22 @@ void test_DoubleBase_ForNegativeExponent(void){ TEST_ASSERT_EQUAL_FLOAT(0.25, r3); } +void test_DoubleBase_for_positive_decimal_exponent(void){ + float r1, r2, r3; + + r1 = expIN(0.45, 3.0); + r2 = expIN(0.5, 9.0); + r3 = expIN(0.5, 2.0); + + printf("Result 1: %f\n", r1); + printf("Result 2: %f\n", r2); + printf("\nResult 3: %lf\n\n", r3); + + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.639474, r1); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 3.0, r2); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.414213, r3); +} + void test_Exponent_with_positive_number(void){ double r1, r2, r3; From dde66d2b33e0b6daf8f43ea9be3da53c66d869bd Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Thu, 1 Feb 2024 08:09:08 +0100 Subject: [PATCH 18/24] Add tests for decimal exponent with decimal base --- test/test_exponent.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test_exponent.c b/test/test_exponent.c index 04d8725..b05bd28 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -56,6 +56,22 @@ void test_DoubleBase_for_positive_decimal_exponent(void){ TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.414213, r3); } +void test_DoubleDecimalBase_for_positive_decimal_exponent(void){ + float r1, r2, r3; + + r1 = expIN(0.31, 0.413); + r2 = expIN(0.5, 3.13432); + r3 = expIN(0.124, 10.1231); + + printf("Result 1: %f\n", r1); + printf("Result 2: %f\n", r2); + printf("\nResult 3: %lf\n\n", r3); + + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.760229, r1); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.770401, r2); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.332474, r3); +} + void test_Exponent_with_positive_number(void){ double r1, r2, r3; From f82b58b61a15cd37dcbe0ee4895888284550b1d0 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Thu, 1 Feb 2024 08:11:23 +0100 Subject: [PATCH 19/24] refactoring: delete print statements from tests --- test/test_exponent.c | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/test/test_exponent.c b/test/test_exponent.c index b05bd28..bbc61e3 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -31,10 +31,6 @@ void test_DoubleBase_ForNegativeExponent(void){ r2 = expIN(-3.0, -3.0); r3 = expIN(-2.0, -2.0); - printf("Result 1: %f\n", r1); - printf("Result 2: %f\n", r2); - printf("Result 3: %f\n", r3); - TEST_ASSERT_EQUAL_FLOAT(0.5, r1); TEST_ASSERT_EQUAL_FLOAT(-0.0370370373, r2); TEST_ASSERT_EQUAL_FLOAT(0.25, r3); @@ -47,10 +43,6 @@ void test_DoubleBase_for_positive_decimal_exponent(void){ r2 = expIN(0.5, 9.0); r3 = expIN(0.5, 2.0); - printf("Result 1: %f\n", r1); - printf("Result 2: %f\n", r2); - printf("\nResult 3: %lf\n\n", r3); - TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.639474, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 3.0, r2); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.414213, r3); @@ -63,10 +55,6 @@ void test_DoubleDecimalBase_for_positive_decimal_exponent(void){ r2 = expIN(0.5, 3.13432); r3 = expIN(0.124, 10.1231); - printf("Result 1: %f\n", r1); - printf("Result 2: %f\n", r2); - printf("\nResult 3: %lf\n\n", r3); - TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.760229, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.770401, r2); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.332474, r3); @@ -79,10 +67,6 @@ void test_Exponent_with_positive_number(void){ r2 = exponential(1.0); r3 = exponential(2.0); - printf("Result 1: %8.lf\n", r1); - printf("Result 2: %lf\n", r2); - printf("Result 3: %lf\n", r3); - 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); @@ -94,9 +78,6 @@ void test_Exponent_with_negative_number(void){ r1 = exponential(-1.0); r2 = exponential(-2.0); - printf("Result 1: %lf\n", r1); - printf("Result 2: %lf\n", r2); - TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.367879, r1); TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.135335, r2); } @@ -108,10 +89,6 @@ void test_ln_with_positive_number(void){ r2 = logN(10.0, 0.000001); r3 = logN(2.5, 0.000001); - printf("Result 1: %lf\n", r1); - printf("Result 2: %lf\n", r2); - printf("Result 3: %lf\n", r3); - TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0, r1); TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.302585, r2); TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.916290, r3); @@ -124,10 +101,6 @@ void test_ln_with_zero_and_negative_numbers(void){ r2 = logN(-1.0, 0.000001); r3 = logN(-2.5, 0.000001); - printf("Result 1: %lf\n", r1); - printf("Result 2: %lf\n", r2); - printf("Result 3: %lf\n", r3); - TEST_ASSERT_EQUAL_DOUBLE(-1.0, r1); TEST_ASSERT_EQUAL_DOUBLE(-1.0, r2); TEST_ASSERT_EQUAL_DOUBLE(-1.0, r3); From deb72cdbb9c4323d4d5ad3a685230113ab638336 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Thu, 1 Feb 2024 08:21:34 +0100 Subject: [PATCH 20/24] add support for negative decimal exponents --- src/exponent.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index bef7597..feea7d7 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -30,12 +30,15 @@ double expIN(double e, double num){ else{ return (1.0 / expI((e*-1), num)); } - }else{ + }else if(num > 0.0){ if(e>0.0){ return exponential(e*logN(num, 0.00000001)); } + else{ + return (1.0 / exponential((-1*e)*logN(num, 0.00000001))); + } } - return 0; + return -1; } unsigned long long fac(int x){ From b9a1ad99da34cd80f4f432cd2f6eb2e4bdd9759e Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Thu, 1 Feb 2024 08:30:25 +0100 Subject: [PATCH 21/24] add test case for negative exponent with integer base --- test/test_exponent.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test_exponent.c b/test/test_exponent.c index bbc61e3..0439cac 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -60,6 +60,22 @@ void test_DoubleDecimalBase_for_positive_decimal_exponent(void){ TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.332474, r3); } +void test_DoubleBase_for_negative_decimal_exponent(void){ + float r1, r2, r3; + + r1 = expIN(-0.125, 3.0); + r2 = expIN(-0.5, 2.0); + r3 = expIN(-0.12923, 10.0); + + printf("%lf\n",r1); + printf("%lf\n",r2); + printf("%lf\n",r3); + + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.871685, r1); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.707106, r2); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.742625, r3); +} + void test_Exponent_with_positive_number(void){ double r1, r2, r3; From 80461d1273c41002d1fba01bf8c2d9f42a49fb2a Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Thu, 1 Feb 2024 08:36:50 +0100 Subject: [PATCH 22/24] add tests for decimal base and decimal exponent --- test/test_exponent.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/test/test_exponent.c b/test/test_exponent.c index 0439cac..f0a09d0 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -67,15 +67,23 @@ void test_DoubleBase_for_negative_decimal_exponent(void){ r2 = expIN(-0.5, 2.0); r3 = expIN(-0.12923, 10.0); - printf("%lf\n",r1); - printf("%lf\n",r2); - printf("%lf\n",r3); - TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.871685, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.707106, r2); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.742625, r3); } +void test_DoubleDecimalBase_for_negative_decimal_exponent(void){ + float r1, r2, r3; + + r1 = expIN(-0.5, 6.2315); + r2 = expIN(-0.1937, 9.2309); + r3 = expIN(-2.2145, 22.9983); + + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.400593, r1); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.650177, r2); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.000964, r3); +} + void test_Exponent_with_positive_number(void){ double r1, r2, r3; From 18200c9252026024d5bd261389475d0e9cea5bd7 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Thu, 1 Feb 2024 08:46:41 +0100 Subject: [PATCH 23/24] add tests for edge cases --- src/exponent.c | 12 +++++++++--- test/test_exponent.c | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index feea7d7..e74dbf3 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -24,21 +24,27 @@ double expI(double e, double num){ double expIN(double e, double num){ if(absD(e) - ((int) absD(e)) < 0.000001){ - if(e>0.0){ + if(e>=0.0){ return expI(e, num); } else{ + if (num == 0.0){ + return -1.0; + } return (1.0 / expI((e*-1), num)); } }else if(num > 0.0){ - if(e>0.0){ + if(e>=0.0){ return exponential(e*logN(num, 0.00000001)); } else{ + if (num == 0.0){ + return -1.0; + } return (1.0 / exponential((-1*e)*logN(num, 0.00000001))); } } - return -1; + return -1.0; } unsigned long long fac(int x){ diff --git a/test/test_exponent.c b/test/test_exponent.c index f0a09d0..8f371cd 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -84,6 +84,22 @@ void test_DoubleDecimalBase_for_negative_decimal_exponent(void){ TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.000964, r3); } +void test_exponent_edge_case(void){ + float r1, r2, r3, r4, r5; + + r1 = expIN(0, 2); + r2 = expIN(0, 0); + r3 = expIN(2, 0); + r4 = expIN(-3, 0); + r5 = expIN(0.5, -3); + + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.0, r1); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.0, r2); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.0, r3); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, -1.0, r4); + TEST_ASSERT_DOUBLE_WITHIN(0.0001, -1.0, r5); +} + void test_Exponent_with_positive_number(void){ double r1, r2, r3; From 3089590084d6298895e901e055667cb90f6dbd9e Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Thu, 1 Feb 2024 08:53:09 +0100 Subject: [PATCH 24/24] refactoring: change variable names and function names --- src/exponent.c | 35 ++++++++++++++++----------------- src/exponent.h | 4 ++-- test/test_exponent.c | 46 ++++++++++++++++++++++---------------------- 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index e74dbf3..a195240 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -10,38 +10,38 @@ double absD(double x){ return x; } -double expI(double e, double num){ +double p(double exp, double base){ double prod = 1.0; - if(e==0.0){ + if(exp==0.0){ return 1; }else{ - for(int i = 1; i <= (int) e; i++){ - prod *= num; + for(int i = 1; i <= (int) exp; i++){ + prod *= base; } return prod; } } -double expIN(double e, double num){ - if(absD(e) - ((int) absD(e)) < 0.000001){ - if(e>=0.0){ - return expI(e, num); +double powerD(double exp, double base){ + if(absD(exp) - ((int) absD(exp)) < 0.000001){ + if(exp>=0.0){ + return p(exp, base); } else{ - if (num == 0.0){ + if (base == 0.0){ return -1.0; } - return (1.0 / expI((e*-1), num)); + return (1.0 / p((exp*-1), base)); } - }else if(num > 0.0){ - if(e>=0.0){ - return exponential(e*logN(num, 0.00000001)); + }else if(base > 0.0){ + if(exp>=0.0){ + return exponential(exp*logN(base, 0.00000001)); } else{ - if (num == 0.0){ + if (base == 0.0){ return -1.0; } - return (1.0 / exponential((-1*e)*logN(num, 0.00000001))); + return (1.0 / exponential((-1*exp)*logN(base, 0.00000001))); } } return -1.0; @@ -63,7 +63,7 @@ unsigned long long fac(int x){ double exponential(double x){ double sum = 0.0; for(int i = 0; i<=21; i++){ - sum += (1.0/fac(i)*(expIN(i,x))); + sum += (1.0/fac(i)*(powerD(i,x))); } return sum; } @@ -77,8 +77,7 @@ double logN(double x, double eps){ double yn = x - 1.0; double yn1 = yn; - do - { + do{ yn = yn1; yn1 = yn + 2 * (x - exponential(yn)) / (x + exponential(yn)); } while (absD(yn - yn1) > eps); diff --git a/src/exponent.h b/src/exponent.h index c869156..14714d9 100644 --- a/src/exponent.h +++ b/src/exponent.h @@ -1,8 +1,8 @@ #ifndef EXPONENT_H #define EXPONENT_H -double expI(double e, double num); -double expIN(double e, double num); +double p(double exp, double base); +double powerD(double exp, double base); double exponential(double x); double logN(double x, double eps); diff --git a/test/test_exponent.c b/test/test_exponent.c index 8f371cd..ec19222 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -15,9 +15,9 @@ void tearDown(void) void test_DoubleBase_ForPositiveExponent(void){ int r1, r2, r3; - r1 = expI(2.0, -2.0); - r2 = expI(3.0, -3.0); - r3 = expI(4.0, 5.0); + r1 = p(2.0, -2.0); + r2 = p(3.0, -3.0); + r3 = p(4.0, 5.0); TEST_ASSERT_EQUAL_INT(4.0, r1); TEST_ASSERT_EQUAL_INT(-27.0, r2); @@ -27,9 +27,9 @@ void test_DoubleBase_ForPositiveExponent(void){ void test_DoubleBase_ForNegativeExponent(void){ float r1, r2, r3; - r1 = expIN(-1.0, 2.0); - r2 = expIN(-3.0, -3.0); - r3 = expIN(-2.0, -2.0); + r1 = powerD(-1.0, 2.0); + r2 = powerD(-3.0, -3.0); + r3 = powerD(-2.0, -2.0); TEST_ASSERT_EQUAL_FLOAT(0.5, r1); TEST_ASSERT_EQUAL_FLOAT(-0.0370370373, r2); @@ -39,9 +39,9 @@ void test_DoubleBase_ForNegativeExponent(void){ void test_DoubleBase_for_positive_decimal_exponent(void){ float r1, r2, r3; - r1 = expIN(0.45, 3.0); - r2 = expIN(0.5, 9.0); - r3 = expIN(0.5, 2.0); + r1 = powerD(0.45, 3.0); + r2 = powerD(0.5, 9.0); + r3 = powerD(0.5, 2.0); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.639474, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 3.0, r2); @@ -51,9 +51,9 @@ void test_DoubleBase_for_positive_decimal_exponent(void){ void test_DoubleDecimalBase_for_positive_decimal_exponent(void){ float r1, r2, r3; - r1 = expIN(0.31, 0.413); - r2 = expIN(0.5, 3.13432); - r3 = expIN(0.124, 10.1231); + r1 = powerD(0.31, 0.413); + r2 = powerD(0.5, 3.13432); + r3 = powerD(0.124, 10.1231); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.760229, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.770401, r2); @@ -63,9 +63,9 @@ void test_DoubleDecimalBase_for_positive_decimal_exponent(void){ void test_DoubleBase_for_negative_decimal_exponent(void){ float r1, r2, r3; - r1 = expIN(-0.125, 3.0); - r2 = expIN(-0.5, 2.0); - r3 = expIN(-0.12923, 10.0); + r1 = powerD(-0.125, 3.0); + r2 = powerD(-0.5, 2.0); + r3 = powerD(-0.12923, 10.0); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.871685, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.707106, r2); @@ -75,9 +75,9 @@ void test_DoubleBase_for_negative_decimal_exponent(void){ void test_DoubleDecimalBase_for_negative_decimal_exponent(void){ float r1, r2, r3; - r1 = expIN(-0.5, 6.2315); - r2 = expIN(-0.1937, 9.2309); - r3 = expIN(-2.2145, 22.9983); + r1 = powerD(-0.5, 6.2315); + r2 = powerD(-0.1937, 9.2309); + r3 = powerD(-2.2145, 22.9983); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.400593, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.650177, r2); @@ -87,11 +87,11 @@ void test_DoubleDecimalBase_for_negative_decimal_exponent(void){ void test_exponent_edge_case(void){ float r1, r2, r3, r4, r5; - r1 = expIN(0, 2); - r2 = expIN(0, 0); - r3 = expIN(2, 0); - r4 = expIN(-3, 0); - r5 = expIN(0.5, -3); + r1 = powerD(0, 2); + r2 = powerD(0, 0); + r3 = powerD(2, 0); + r4 = powerD(-3, 0); + r5 = powerD(0.5, -3); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.0, r1); TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.0, r2);