From f05d7b4d4f051db1a268269b2de063be140dcd03 Mon Sep 17 00:00:00 2001 From: fdai7848 Date: Fri, 2 Feb 2024 19:21:36 +0100 Subject: [PATCH] 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