Browse Source

refactoring: split function logN in smaller subfunctions

master
fdai7848 11 months ago
parent
commit
083b545c75
  1. 4
      src/exponent.c
  2. 23
      src/logarithmus.c
  3. 2
      src/logarithmus.h
  4. 12
      test/test_logarithmus.c

4
src/exponent.c

@ -37,13 +37,13 @@ double powerD(double exp, double base){
} }
}else if(base > 0.0){ }else if(base > 0.0){
if(exp>=0.0){ if(exp>=0.0){
return exponential(exp*logN(base, 0.00000001));
return exponential(exp*logN(base));
} }
else{ else{
if (base == 0.0){ if (base == 0.0){
return -1.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; return -1.0;

23
src/logarithmus.c

@ -8,7 +8,7 @@ double logX(double base, double value){
if(base == 1.0 || base <= 0.0 || value <= 0.0){ if(base == 1.0 || base <= 0.0 || value <= 0.0){
return -1.0; return -1.0;
} }
return(logN(value, 0.00000001)/logN(base, 0.00000001));
return(logN(value)/logN(base));
} }
double log10(double value){ double log10(double value){
@ -18,12 +18,7 @@ double log10(double value){
double log2(double value){ double log2(double value){
return logX(2.0, value); return logX(2.0, value);
} }
double logN(double x, double eps){
if(x <= 0){
return -1.0;
}
if(x<200){
double lns(double x, double eps){
double yn = x - 1.0; double yn = x - 1.0;
double yn1 = yn; double yn1 = yn;
@ -34,7 +29,8 @@ double logN(double x, double eps){
return yn1; return yn1;
} }
else{
double lnb(double x){
int power_adjust = 0; int power_adjust = 0;
while (x > 1.0) { while (x > 1.0) {
x /= exponential(1); x /= exponential(1);
@ -54,5 +50,16 @@ double logN(double x, double eps){
} }
return t + power_adjust; return t + power_adjust;
} }
double logN(double x){
if(x <= 0){
return -1.0;
}
if(x<200){
return lns(x, 0.00000001);
}
else{
return lnb(x);
}
} }

2
src/logarithmus.h

@ -1,7 +1,7 @@
#ifndef LOGARITHMUS_H #ifndef LOGARITHMUS_H
#define LOGARITHMUS_H #define LOGARITHMUS_H
double logN(double x, double eps);
double logN(double x);
double logX(double b, double a); double logX(double b, double a);
double log10(double a); double log10(double a);
double log2(double a); double log2(double a);

12
test/test_logarithmus.c

@ -16,9 +16,9 @@ void tearDown(void)
void test_ln_with_positive_number(void){ void test_ln_with_positive_number(void){
double r1, r2, r3; 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, 0, r1);
TEST_ASSERT_DOUBLE_WITHIN(0.000001, 2.302585, r2); 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){ void test_ln_with_zero_and_negative_numbers(void){
double r1, r2, r3; 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, r1);
TEST_ASSERT_EQUAL_DOUBLE(-1.0, r2); TEST_ASSERT_EQUAL_DOUBLE(-1.0, r2);

Loading…
Cancel
Save