log10() fixes to prevent calculator from crashing

This commit is contained in:
Jeff Wang 2019-10-04 23:49:42 -04:00
parent c2edc7871d
commit ce42e30513
4 changed files with 37 additions and 13 deletions

View File

@ -1015,11 +1015,11 @@ void ln_decn(void){
#undef NUM_A_ARR #undef NUM_A_ARR
} }
void log10_decn(void){ //inline void log10_decn(void){
ln_decn(); // ln_decn();
copy_decn(&BDecn, &DECN_LN_10); // copy_decn(&BDecn, &DECN_LN_10);
div_decn(); // div_decn();
} //}
static void set_str_error(void){ static void set_str_error(void){
Buf[0] = 'E'; Buf[0] = 'E';

View File

@ -69,7 +69,13 @@ void mult_decn(void);
void div_decn(void); void div_decn(void);
void ln_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 //Buf should hold at least 18 + 4 + 5 + 1 = 28
#define DECN_BUF_SIZE 28 #define DECN_BUF_SIZE 28

View File

@ -62,6 +62,22 @@ static void log_test(
printf(" : %s\n\n", Buf); 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){ int main(void){
// dec80 acc, b; // dec80 acc, b;
@ -226,5 +242,10 @@ int main(void){
"-22.3227534185273434", 0 "-22.3227534185273434", 0
); );
//new acc for log test
log10_test(
"1.5", 0,
"0.176091259", 0
);
return 0; return 0;
} }

View File

@ -10,10 +10,6 @@ using namespace boost::multiprecision;
using std::cout; using std::cout;
using std::endl; using std::endl;
static const int BSIZE = 200;
char buf[BSIZE];
char pbuf[BSIZE];
static const int NUM_A_ARR = 9; static const int NUM_A_ARR = 9;
mpfr_float a_arr[NUM_A_ARR]; mpfr_float a_arr[NUM_A_ARR];
mpfr_float ln_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 accum(b_j, CALC_PRECISION);
mpfr_float_1000 actual; mpfr_float_1000 actual;
actual = log(A); actual = log(A);
int num_times[NUM_A_ARR]; int num_times[NUM_A_ARR];
//track number of times multiplied by a_arr[j] //track number of times multiplied by a_arr[j]
for (int j = 0; j < NUM_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]); // printf(" %d: num_times: %d, ", j, num_times[j]);
// cout << b_j << endl; // cout << b_j << endl;
} }
//build final value //build final value
accum = b_j; //remainder accum = b_j; //remainder
accum *= pow(10.0, -(NUM_A_ARR - 1)); //scale remainder accum *= pow(10.0, -(NUM_A_ARR - 1)); //scale remainder
@ -81,10 +77,11 @@ int main(void){
} }
accum = -accum; accum = -accum;
//calculate relative error
mpfr_float_1000 calc, diff; mpfr_float_1000 calc, diff;
calc = accum; calc = accum;
diff = (actual - calc)/actual; diff = (actual - calc)/actual;
if (diff > 5e-17){ if (diff > 5e-17){
cout << x << ": "; cout << x << ": ";
cout << std::setprecision(18) << accum << ", "; cout << std::setprecision(18) << accum << ", ";