From 9cf7587746fbdcc2e2815346a641b73a4a32900f Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Thu, 1 Feb 2024 09:14:40 +0100 Subject: [PATCH] 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