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);