Browse Source

refactoring: move ln function to logarithmus

master
fdai7848 11 months ago
parent
commit
9cf7587746
  1. 17
      src/exponent.c
  2. 2
      src/exponent.h
  3. 16
      src/logarithmus.c
  4. 6
      src/logarithmus.h
  5. 26
      test/test_exponent.c
  6. 26
      test/test_logarithmus.c

17
src/exponent.c

@ -2,6 +2,7 @@
#include <limits.h> #include <limits.h>
#include "exponent.h" #include "exponent.h"
#include "logarithmus.h"
double absD(double x){ double absD(double x){
if(x<0){ if(x<0){
@ -68,19 +69,3 @@ double exponential(double x){
return sum; 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;
}

2
src/exponent.h

@ -4,6 +4,6 @@
double p(double exp, double base); double p(double exp, double base);
double powerD(double exp, double base); double powerD(double exp, double base);
double exponential(double x); double exponential(double x);
double logN(double x, double eps);
double absD(double x);
#endif // exponent.h #endif // exponent.h

16
src/logarithmus.c

@ -2,3 +2,19 @@
#include <limits.h> #include <limits.h>
#include "logarithmus.h" #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;
}

6
src/logarithmus.h

@ -1,4 +1,6 @@
#ifndef LOGARITHMUS_H_H
#ifndef LOGARITHMUS_H
#define LOGARITHMUS_H #define LOGARITHMUS_H
#endif // exponent.h
double logN(double x, double eps);
#endif // logarithmus.h

26
test/test_exponent.c

@ -3,6 +3,7 @@
#include "unity.h" #include "unity.h"
#include "exponent.h" #include "exponent.h"
#include "logarithmus.h"
void setUp(void) void setUp(void)
{ {
@ -122,29 +123,4 @@ void test_Exponent_with_negative_number(void){
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.135335, r2); 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 #endif // TEST

26
test/test_logarithmus.c

@ -3,6 +3,7 @@
#include "unity.h" #include "unity.h"
#include "logarithmus.h" #include "logarithmus.h"
#include "exponent.h"
void setUp(void) 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 #endif // TEST
Loading…
Cancel
Save