From ce42e30513750e5fad89e73888fc836409260d4e Mon Sep 17 00:00:00 2001 From: Jeff Wang Date: Fri, 4 Oct 2019 23:49:42 -0400 Subject: [PATCH] log10() fixes to prevent calculator from crashing --- src/decn/decn.c | 10 +++++----- src/decn/decn.h | 8 +++++++- src/decn/decn_test.c | 21 +++++++++++++++++++++ src/decn/proto/ln_mfp.cpp | 11 ++++------- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/decn/decn.c b/src/decn/decn.c index 068808d..a511237 100644 --- a/src/decn/decn.c +++ b/src/decn/decn.c @@ -1015,11 +1015,11 @@ void ln_decn(void){ #undef NUM_A_ARR } -void log10_decn(void){ - ln_decn(); - copy_decn(&BDecn, &DECN_LN_10); - div_decn(); -} +//inline void log10_decn(void){ +// ln_decn(); +// copy_decn(&BDecn, &DECN_LN_10); +// div_decn(); +//} static void set_str_error(void){ Buf[0] = 'E'; diff --git a/src/decn/decn.h b/src/decn/decn.h index fa45ec5..c7367b2 100644 --- a/src/decn/decn.h +++ b/src/decn/decn.h @@ -69,7 +69,13 @@ void mult_decn(void); void div_decn(void); void ln_decn(void); -void log10_decn(void); +//inline void log10_decn(void); +#define log10_decn() do { \ + ln_decn(); \ + copy_decn(&BDecn, &DECN_LN_10); \ + div_decn(); \ + } while(0); + //Buf should hold at least 18 + 4 + 5 + 1 = 28 #define DECN_BUF_SIZE 28 diff --git a/src/decn/decn_test.c b/src/decn/decn_test.c index 772eb92..48a1ff2 100644 --- a/src/decn/decn_test.c +++ b/src/decn/decn_test.c @@ -62,6 +62,22 @@ static void log_test( printf(" : %s\n\n", Buf); } +static void log10_test( + const char* x_str, int x_exp, + const char* res_str, int res_exp) +{ + build_dec80(x_str, x_exp); + decn_to_str_complete(&AccDecn); + printf(" a : %s\n", Buf); + log10_decn(); + decn_to_str_complete(&AccDecn); + printf("ln(a): %s\n", Buf); + build_decn_at(&diff, res_str, res_exp); + take_diff(); + decn_to_str_complete(&diff); + printf(" : %s\n\n", Buf); +} + int main(void){ // dec80 acc, b; @@ -226,5 +242,10 @@ int main(void){ "-22.3227534185273434", 0 ); + //new acc for log test + log10_test( + "1.5", 0, + "0.176091259", 0 + ); return 0; } diff --git a/src/decn/proto/ln_mfp.cpp b/src/decn/proto/ln_mfp.cpp index 033ebb1..6d63b58 100644 --- a/src/decn/proto/ln_mfp.cpp +++ b/src/decn/proto/ln_mfp.cpp @@ -10,10 +10,6 @@ using namespace boost::multiprecision; using std::cout; using std::endl; -static const int BSIZE = 200; -char buf[BSIZE]; -char pbuf[BSIZE]; - static const int NUM_A_ARR = 9; mpfr_float a_arr[NUM_A_ARR]; mpfr_float ln_a_arr[NUM_A_ARR]; @@ -48,7 +44,7 @@ int main(void){ mpfr_float accum(b_j, CALC_PRECISION); mpfr_float_1000 actual; actual = log(A); - + int num_times[NUM_A_ARR]; //track number of times multiplied by a_arr[j] for (int j = 0; j < NUM_A_ARR; j++){ @@ -69,7 +65,7 @@ int main(void){ // printf(" %d: num_times: %d, ", j, num_times[j]); // cout << b_j << endl; } - + //build final value accum = b_j; //remainder accum *= pow(10.0, -(NUM_A_ARR - 1)); //scale remainder @@ -81,10 +77,11 @@ int main(void){ } accum = -accum; + //calculate relative error mpfr_float_1000 calc, diff; calc = accum; diff = (actual - calc)/actual; - + if (diff > 5e-17){ cout << x << ": "; cout << std::setprecision(18) << accum << ", ";