From f3024b4387b4e1e11f253287aba6ae954d72339b Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Thu, 1 Feb 2024 09:05:38 +0100 Subject: [PATCH 01/13] add files for logarithmus --- src/logarithmus.c | 4 ++++ src/logarithmus.h | 4 ++++ test/test_logarithmus.c | 15 +++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 src/logarithmus.c create mode 100644 src/logarithmus.h create mode 100644 test/test_logarithmus.c diff --git a/src/logarithmus.c b/src/logarithmus.c new file mode 100644 index 0000000..a99e0d9 --- /dev/null +++ b/src/logarithmus.c @@ -0,0 +1,4 @@ +#include +#include + +#include "logarithmus.h" \ No newline at end of file diff --git a/src/logarithmus.h b/src/logarithmus.h new file mode 100644 index 0000000..2c70dc1 --- /dev/null +++ b/src/logarithmus.h @@ -0,0 +1,4 @@ +#ifndef LOGARITHMUS_H_H +#define LOGARITHMUS_H + +#endif // exponent.h \ No newline at end of file diff --git a/test/test_logarithmus.c b/test/test_logarithmus.c new file mode 100644 index 0000000..2228e3d --- /dev/null +++ b/test/test_logarithmus.c @@ -0,0 +1,15 @@ +#ifdef TEST + +#include "unity.h" + +#include "logarithmus.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +#endif // TEST \ No newline at end of file From 9cf7587746fbdcc2e2815346a641b73a4a32900f Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Thu, 1 Feb 2024 09:14:40 +0100 Subject: [PATCH 02/13] refactoring: move ln function to logarithmus --- src/exponent.c | 17 +---------------- src/exponent.h | 2 +- src/logarithmus.c | 18 +++++++++++++++++- src/logarithmus.h | 6 ++++-- test/test_exponent.c | 26 +------------------------- test/test_logarithmus.c | 26 ++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 45 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index a195240..9873ad3 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -2,6 +2,7 @@ #include #include "exponent.h" +#include "logarithmus.h" double absD(double x){ if(x<0){ @@ -68,19 +69,3 @@ double exponential(double x){ return sum; } - - -double logN(double x, double eps){ - if(x <= 0){ - return -1.0; - } - 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 diff --git a/src/exponent.h b/src/exponent.h index 14714d9..9b53200 100644 --- a/src/exponent.h +++ b/src/exponent.h @@ -4,6 +4,6 @@ double p(double exp, double base); double powerD(double exp, double base); double exponential(double x); -double logN(double x, double eps); +double absD(double x); #endif // exponent.h \ No newline at end of file diff --git a/src/logarithmus.c b/src/logarithmus.c index a99e0d9..b5bc971 100644 --- a/src/logarithmus.c +++ b/src/logarithmus.c @@ -1,4 +1,20 @@ #include #include -#include "logarithmus.h" \ No newline at end of file +#include "logarithmus.h" +#include "exponent.h" + +double logN(double x, double eps){ + if(x <= 0){ + return -1.0; + } + 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 diff --git a/src/logarithmus.h b/src/logarithmus.h index 2c70dc1..9447319 100644 --- a/src/logarithmus.h +++ b/src/logarithmus.h @@ -1,4 +1,6 @@ -#ifndef LOGARITHMUS_H_H +#ifndef LOGARITHMUS_H #define LOGARITHMUS_H -#endif // exponent.h \ No newline at end of file +double logN(double x, double eps); + +#endif // logarithmus.h \ No newline at end of file diff --git a/test/test_exponent.c b/test/test_exponent.c index ec19222..ae49c01 100644 --- a/test/test_exponent.c +++ b/test/test_exponent.c @@ -3,6 +3,7 @@ #include "unity.h" #include "exponent.h" +#include "logarithmus.h" void setUp(void) { @@ -122,29 +123,4 @@ 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); - - 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); -} - -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); - - 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 diff --git a/test/test_logarithmus.c b/test/test_logarithmus.c index 2228e3d..6c056d8 100644 --- a/test/test_logarithmus.c +++ b/test/test_logarithmus.c @@ -3,6 +3,7 @@ #include "unity.h" #include "logarithmus.h" +#include "exponent.h" void setUp(void) { @@ -12,4 +13,29 @@ void tearDown(void) { } +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); + + 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); +} + +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); + + 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 c83f6e9b4be35007cdd91536e004484ccec3f1d1 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Thu, 1 Feb 2024 23:59:32 +0100 Subject: [PATCH 03/13] add logarithmic function for all bases --- src/logarithmus.c | 7 +++++++ src/logarithmus.h | 1 + 2 files changed, 8 insertions(+) diff --git a/src/logarithmus.c b/src/logarithmus.c index b5bc971..248622c 100644 --- a/src/logarithmus.c +++ b/src/logarithmus.c @@ -4,6 +4,13 @@ #include "logarithmus.h" #include "exponent.h" +double logX(double b, double a){ + if(b == 1.0 || b < 0.0){ + return -1.0; + } + return(logN(a, 0.000001)/logN(b, 0.000001)); +} + double logN(double x, double eps){ if(x <= 0){ return -1.0; diff --git a/src/logarithmus.h b/src/logarithmus.h index 9447319..4294276 100644 --- a/src/logarithmus.h +++ b/src/logarithmus.h @@ -2,5 +2,6 @@ #define LOGARITHMUS_H double logN(double x, double eps); +double logX(double b, double a); #endif // logarithmus.h \ No newline at end of file From 6060b30bc33cccb2f5aa55e2c3909554281fa0c4 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Fri, 2 Feb 2024 00:16:58 +0100 Subject: [PATCH 04/13] add function for logarithmus to base 10 for convenience --- src/logarithmus.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/logarithmus.c b/src/logarithmus.c index 248622c..f212821 100644 --- a/src/logarithmus.c +++ b/src/logarithmus.c @@ -11,6 +11,10 @@ double logX(double b, double a){ return(logN(a, 0.000001)/logN(b, 0.000001)); } +double logDec(double a){ + return logX(10.0, a); +} + double logN(double x, double eps){ if(x <= 0){ return -1.0; From bafd5844f32ff85dd25458157518d2b650a08c13 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Fri, 2 Feb 2024 00:20:34 +0100 Subject: [PATCH 05/13] add function for logarithmus to base 2 for convenience --- src/logarithmus.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/logarithmus.c b/src/logarithmus.c index f212821..a7abbbb 100644 --- a/src/logarithmus.c +++ b/src/logarithmus.c @@ -15,6 +15,10 @@ double logDec(double a){ return logX(10.0, a); } +double logBin(double a){ + return logX(2.0, a); +} + double logN(double x, double eps){ if(x <= 0){ return -1.0; From fa9fb28309e241890cf5f1f0cfc07b2ad4dfbe99 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Fri, 2 Feb 2024 15:57:58 +0100 Subject: [PATCH 06/13] add tests for edge cases and add fix for log function --- src/logarithmus.c | 2 +- src/logarithmus.h | 2 ++ test/test_logarithmus.c | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/logarithmus.c b/src/logarithmus.c index a7abbbb..8dbfd7a 100644 --- a/src/logarithmus.c +++ b/src/logarithmus.c @@ -5,7 +5,7 @@ #include "exponent.h" double logX(double b, double a){ - if(b == 1.0 || b < 0.0){ + if(b == 1.0 || b <= 0.0 || a <= 0.0){ return -1.0; } return(logN(a, 0.000001)/logN(b, 0.000001)); diff --git a/src/logarithmus.h b/src/logarithmus.h index 4294276..68b011f 100644 --- a/src/logarithmus.h +++ b/src/logarithmus.h @@ -3,5 +3,7 @@ double logN(double x, double eps); double logX(double b, double a); +double logDec(double a); +double logBin(double a); #endif // logarithmus.h \ No newline at end of file diff --git a/test/test_logarithmus.c b/test/test_logarithmus.c index 6c056d8..e8fedfe 100644 --- a/test/test_logarithmus.c +++ b/test/test_logarithmus.c @@ -37,5 +37,24 @@ void test_ln_with_zero_and_negative_numbers(void){ TEST_ASSERT_EQUAL_DOUBLE(-1.0, r3); } +void test_logarithmic_function_with_edge_cases(void){ + double r1, r2, r3, r4; + + r1 = logX(0, 10); + r2 = logX(-1.0, 9); + r3 = logX(1, 18); + r4 = logX(3, 0); + + printf("%lf\n", r1); + printf("%lf\n", r2); + printf("%lf\n", r3); + printf("%lf\n", r4); + + TEST_ASSERT_EQUAL_DOUBLE(-1.0, r1); + TEST_ASSERT_EQUAL_DOUBLE(-1.0, r2); + TEST_ASSERT_EQUAL_DOUBLE(-1.0, r3); + TEST_ASSERT_EQUAL_DOUBLE(-1.0, r4); +} + #endif // TEST \ No newline at end of file From 109ea482008568a98d634a6007b7328efa8483ec Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Fri, 2 Feb 2024 17:06:25 +0100 Subject: [PATCH 07/13] add tests for log function with integers --- test/test_logarithmus.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/test/test_logarithmus.c b/test/test_logarithmus.c index e8fedfe..6461bba 100644 --- a/test/test_logarithmus.c +++ b/test/test_logarithmus.c @@ -40,15 +40,10 @@ void test_ln_with_zero_and_negative_numbers(void){ void test_logarithmic_function_with_edge_cases(void){ double r1, r2, r3, r4; - r1 = logX(0, 10); - r2 = logX(-1.0, 9); - r3 = logX(1, 18); - r4 = logX(3, 0); - - printf("%lf\n", r1); - printf("%lf\n", r2); - printf("%lf\n", r3); - printf("%lf\n", r4); + r1 = logX(0, 10.0); + r2 = logX(-1.0, 9.0); + r3 = logX(1.0, 18.0); + r4 = logX(3.0, 0.0); TEST_ASSERT_EQUAL_DOUBLE(-1.0, r1); TEST_ASSERT_EQUAL_DOUBLE(-1.0, r2); @@ -56,5 +51,19 @@ void test_logarithmic_function_with_edge_cases(void){ TEST_ASSERT_EQUAL_DOUBLE(-1.0, r4); } +void test_logarithmic_function_with_integers(void){ + double r1, r2, r3, r4; + + r1 = logX(0.5, 48.0); + r2 = logX(2.0, 8.0); + r3 = logX(3.2, 18.0); + r4 = logX(6.0, 36.0); + + TEST_ASSERT_DOUBLE_WITHIN(0.000001, -5.584962, r1); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 3.0, r2); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.484950, r3); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.0, r4); +} + #endif // TEST \ No newline at end of file From eb421bd257cd747c27656df3c14ae899e154f09f Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Fri, 2 Feb 2024 17:55:01 +0100 Subject: [PATCH 08/13] add test for logarithmus with decimals --- test/test_logarithmus.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/test_logarithmus.c b/test/test_logarithmus.c index 6461bba..e39c8c9 100644 --- a/test/test_logarithmus.c +++ b/test/test_logarithmus.c @@ -65,5 +65,19 @@ void test_logarithmic_function_with_integers(void){ TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.0, r4); } +void test_logarithmic_function_with_decimals(void){ + double r1, r2, r3, r4; + + r1 = logX(0.1289, 26.5); + r2 = logX(7.64, 8.314); + r3 = logX(3.0, 2.4587); + r4 = logX(9.0, 94.6584); + + TEST_ASSERT_DOUBLE_WITHIN(0.000001, -1.599607, r1); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 1.041577, r2); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.818881, r3); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.070919, r4); +} + #endif // TEST \ No newline at end of file From 18a4d3836797403bc189e8ba07374e25218dca74 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Fri, 2 Feb 2024 18:02:19 +0100 Subject: [PATCH 09/13] add test for logarithmic function to base 10 --- test/test_logarithmus.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_logarithmus.c b/test/test_logarithmus.c index e39c8c9..d606c97 100644 --- a/test/test_logarithmus.c +++ b/test/test_logarithmus.c @@ -79,5 +79,23 @@ void test_logarithmic_function_with_decimals(void){ TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.070919, r4); } +void test_logarithmic_function_to_base_10(void){ + double r1, r2, r3, r4, r5, r6; + + r1 = logDec(100.0); + r2 = logDec(100.6479); + r3 = logDec(10.587); + r4 = logDec(4.341); + r5 = logDec(0.34979); + r6 = logDec(1.0); + + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.0, r1); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.002804, r2); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 1.024772, r3); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.637589, r4); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, -0.456192, r5); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.0, r6); +} + #endif // TEST \ No newline at end of file From f05d7b4d4f051db1a268269b2de063be140dcd03 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Fri, 2 Feb 2024 19:21:36 +0100 Subject: [PATCH 10/13] add support to ln function for larger numbers --- src/exponent.c | 4 +++- src/logarithmus.c | 43 +++++++++++++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index 9873ad3..0904080 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -1,5 +1,6 @@ #include #include +#include #include "exponent.h" #include "logarithmus.h" @@ -63,9 +64,10 @@ unsigned long long fac(int x){ double exponential(double x){ double sum = 0.0; - for(int i = 0; i<=21; i++){ + for(int i = 0; i<=20; i++){ sum += (1.0/fac(i)*(powerD(i,x))); } + return sum; } diff --git a/src/logarithmus.c b/src/logarithmus.c index 8dbfd7a..9bd265d 100644 --- a/src/logarithmus.c +++ b/src/logarithmus.c @@ -8,7 +8,7 @@ double logX(double b, double a){ if(b == 1.0 || b <= 0.0 || a <= 0.0){ return -1.0; } - return(logN(a, 0.000001)/logN(b, 0.000001)); + return(logN(a, 0.00000001)/logN(b, 0.00000001)); } double logDec(double a){ @@ -22,14 +22,37 @@ double logBin(double a){ double logN(double x, double eps){ if(x <= 0){ return -1.0; - } - double yn = x - 1.0; - double yn1 = yn; + } + if(x<200){ + 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); - do{ - yn = yn1; - yn1 = yn + 2 * (x - exponential(yn)) / (x + exponential(yn)); - } while (absD(yn - yn1) > eps); + return yn1; + } + else{ + int power_adjust = 0; + while (x > 1.0) { + x /= exponential(1); + power_adjust++; + } + while (x < .25) { + x *= exponential(1); + power_adjust--; + } + + x -= 1.0; + double t = 0.0, s = 1.0, z = x; + for (int k=1; k<=100; k++) { + t += z * s / k; + z *= x; + s = -s; + } + return t + power_adjust; + } +} - return yn1; -} \ No newline at end of file From 1ce4f54dd0daa74f4e08931763f1dbef36d47ed1 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Fri, 2 Feb 2024 19:22:37 +0100 Subject: [PATCH 11/13] add test for logarithmic function to base 2 --- test/test_logarithmus.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_logarithmus.c b/test/test_logarithmus.c index d606c97..3faad09 100644 --- a/test/test_logarithmus.c +++ b/test/test_logarithmus.c @@ -97,5 +97,23 @@ void test_logarithmic_function_to_base_10(void){ TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.0, r6); } +void test_logarithmic_function_to_base_2(void){ + double r1, r2, r3, r4, r5, r6; + + r1 = logBin(1024.0); + r2 = logBin(1024.8742); + r3 = logBin(900.587); + r4 = logBin(78.341); + r5 = logBin(0.34979); + r6 = logBin(1.0); + + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 10.0, r1); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 10.001231, r2); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 9.814721, r3); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 6.291695, r4); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, -1.515439, r5); + TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.0, r6); +} + #endif // TEST \ No newline at end of file From 923b1fe04c9bb4422ce0c9332b733bc7a0bc6976 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Fri, 2 Feb 2024 19:28:50 +0100 Subject: [PATCH 12/13] refactoring: change function names and parameter names in logarithmus.c --- src/exponent.c | 3 +-- src/logarithmus.c | 14 +++++++------- src/logarithmus.h | 4 ++-- test/test_logarithmus.c | 24 ++++++++++++------------ 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index 0904080..7dd4455 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -64,10 +64,9 @@ unsigned long long fac(int x){ double exponential(double x){ double sum = 0.0; - for(int i = 0; i<=20; i++){ + for(int i = 0; i<=21; i++){ sum += (1.0/fac(i)*(powerD(i,x))); } - return sum; } diff --git a/src/logarithmus.c b/src/logarithmus.c index 9bd265d..b15c069 100644 --- a/src/logarithmus.c +++ b/src/logarithmus.c @@ -4,19 +4,19 @@ #include "logarithmus.h" #include "exponent.h" -double logX(double b, double a){ - if(b == 1.0 || b <= 0.0 || a <= 0.0){ +double logX(double base, double value){ + if(base == 1.0 || base <= 0.0 || value <= 0.0){ return -1.0; } - return(logN(a, 0.00000001)/logN(b, 0.00000001)); + return(logN(value, 0.00000001)/logN(base, 0.00000001)); } -double logDec(double a){ - return logX(10.0, a); +double log10(double value){ + return logX(10.0, value); } -double logBin(double a){ - return logX(2.0, a); +double log2(double value){ + return logX(2.0, value); } double logN(double x, double eps){ diff --git a/src/logarithmus.h b/src/logarithmus.h index 68b011f..e94f407 100644 --- a/src/logarithmus.h +++ b/src/logarithmus.h @@ -3,7 +3,7 @@ double logN(double x, double eps); double logX(double b, double a); -double logDec(double a); -double logBin(double a); +double log10(double a); +double log2(double a); #endif // logarithmus.h \ No newline at end of file diff --git a/test/test_logarithmus.c b/test/test_logarithmus.c index 3faad09..d31283b 100644 --- a/test/test_logarithmus.c +++ b/test/test_logarithmus.c @@ -82,12 +82,12 @@ void test_logarithmic_function_with_decimals(void){ void test_logarithmic_function_to_base_10(void){ double r1, r2, r3, r4, r5, r6; - r1 = logDec(100.0); - r2 = logDec(100.6479); - r3 = logDec(10.587); - r4 = logDec(4.341); - r5 = logDec(0.34979); - r6 = logDec(1.0); + r1 = log10(100.0); + r2 = log10(100.6479); + r3 = log10(10.587); + r4 = log10(4.341); + r5 = log10(0.34979); + r6 = log10(1.0); TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.0, r1); TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.002804, r2); @@ -100,12 +100,12 @@ void test_logarithmic_function_to_base_10(void){ void test_logarithmic_function_to_base_2(void){ double r1, r2, r3, r4, r5, r6; - r1 = logBin(1024.0); - r2 = logBin(1024.8742); - r3 = logBin(900.587); - r4 = logBin(78.341); - r5 = logBin(0.34979); - r6 = logBin(1.0); + r1 = log2(1024.0); + r2 = log2(1024.8742); + r3 = log2(900.587); + r4 = log2(78.341); + r5 = log2(0.34979); + r6 = log2(1.0); TEST_ASSERT_DOUBLE_WITHIN(0.000001, 10.0, r1); TEST_ASSERT_DOUBLE_WITHIN(0.000001, 10.001231, r2); From 083b545c750ed01ddb1f8efab7a4cb033c5bc554 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Sat, 3 Feb 2024 15:45:09 +0100 Subject: [PATCH 13/13] refactoring: split function logN in smaller subfunctions --- src/exponent.c | 4 +-- src/logarithmus.c | 65 +++++++++++++++++++++++------------------ src/logarithmus.h | 2 +- test/test_logarithmus.c | 12 ++++---- 4 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/exponent.c b/src/exponent.c index 7dd4455..bbe2221 100644 --- a/src/exponent.c +++ b/src/exponent.c @@ -37,13 +37,13 @@ double powerD(double exp, double base){ } }else if(base > 0.0){ if(exp>=0.0){ - return exponential(exp*logN(base, 0.00000001)); + return exponential(exp*logN(base)); } else{ if (base == 0.0){ return -1.0; } - return (1.0 / exponential((-1*exp)*logN(base, 0.00000001))); + return (1.0 / exponential((-1*exp)*logN(base))); } } return -1.0; diff --git a/src/logarithmus.c b/src/logarithmus.c index b15c069..1c613e5 100644 --- a/src/logarithmus.c +++ b/src/logarithmus.c @@ -8,7 +8,7 @@ double logX(double base, double value){ if(base == 1.0 || base <= 0.0 || value <= 0.0){ return -1.0; } - return(logN(value, 0.00000001)/logN(base, 0.00000001)); + return(logN(value)/logN(base)); } double log10(double value){ @@ -18,41 +18,48 @@ double log10(double value){ double log2(double value){ return logX(2.0, value); } +double lns(double x, double eps){ + double yn = x - 1.0; + double yn1 = yn; -double logN(double x, double eps){ + do{ + yn = yn1; + yn1 = yn + 2 * (x - exponential(yn)) / (x + exponential(yn)); + } while (absD(yn - yn1) > eps); + + return yn1; +} + +double lnb(double x){ + int power_adjust = 0; + while (x > 1.0) { + x /= exponential(1); + power_adjust++; + } + while (x < .25) { + x *= exponential(1); + power_adjust--; + } + + x -= 1.0; + double t = 0.0, s = 1.0, z = x; + for (int k=1; k<=100; k++) { + t += z * s / k; + z *= x; + s = -s; + } + return t + power_adjust; +} + +double logN(double x){ if(x <= 0){ return -1.0; } if(x<200){ - 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; + return lns(x, 0.00000001); } else{ - int power_adjust = 0; - while (x > 1.0) { - x /= exponential(1); - power_adjust++; - } - while (x < .25) { - x *= exponential(1); - power_adjust--; - } - - x -= 1.0; - double t = 0.0, s = 1.0, z = x; - for (int k=1; k<=100; k++) { - t += z * s / k; - z *= x; - s = -s; - } - return t + power_adjust; + return lnb(x); } } diff --git a/src/logarithmus.h b/src/logarithmus.h index e94f407..113c48b 100644 --- a/src/logarithmus.h +++ b/src/logarithmus.h @@ -1,7 +1,7 @@ #ifndef LOGARITHMUS_H #define LOGARITHMUS_H -double logN(double x, double eps); +double logN(double x); double logX(double b, double a); double log10(double a); double log2(double a); diff --git a/test/test_logarithmus.c b/test/test_logarithmus.c index d31283b..0f7aba1 100644 --- a/test/test_logarithmus.c +++ b/test/test_logarithmus.c @@ -16,9 +16,9 @@ void tearDown(void) 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); + r1 = logN(1.0); + r2 = logN(10.0); + r3 = logN(2.5); TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0, r1); TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.302585, r2); @@ -28,9 +28,9 @@ void test_ln_with_positive_number(void){ 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); + r1 = logN(0.0); + r2 = logN(-1.0); + r3 = logN(-2.5); TEST_ASSERT_EQUAL_DOUBLE(-1.0, r1); TEST_ASSERT_EQUAL_DOUBLE(-1.0, r2);