/* * arctanh(x) == 0.5 * log [ (1 + x) / (1 - x) ] * * |x| < 1.0 */ void m_apm_arctanh(M_APM rr, int places, M_APM aa) { M_APM tmp1, tmp2, tmp3; int ii, local_precision; tmp1 = M_get_stack_var(); m_apm_absolute_value(tmp1, aa); ii = m_apm_compare(tmp1, MM_One); if (ii >= 0) /* |x| >= 1.0 */ { M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_arctanh\', |Argument| >= 1"); M_set_to_zero(rr); M_restore_stack(1); return; } tmp2 = M_get_stack_var(); tmp3 = M_get_stack_var(); local_precision = places + 8; m_apm_add(tmp1, MM_One, aa); m_apm_subtract(tmp2, MM_One, aa); m_apm_divide(tmp3, local_precision, tmp1, tmp2); m_apm_log(tmp2, local_precision, tmp3); m_apm_multiply(tmp1, tmp2, MM_0_5); m_apm_round(rr, places, tmp1); M_restore_stack(3); }
/* * arccosh(x) == log [ x + sqrt(x^2 - 1) ] * * x >= 1.0 */ void m_apm_arccosh(M_APM rr, int places, M_APM aa) { M_APM tmp1, tmp2; int ii; ii = m_apm_compare(aa, MM_One); if (ii == -1) /* x < 1 */ { M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_arccosh\', Argument < 1"); M_set_to_zero(rr); return; } tmp1 = M_get_stack_var(); tmp2 = M_get_stack_var(); m_apm_multiply(tmp1, aa, aa); m_apm_subtract(tmp2, tmp1, MM_One); m_apm_sqrt(tmp1, (places + 6), tmp2); m_apm_add(tmp2, aa, tmp1); m_apm_log(rr, places, tmp2); M_restore_stack(2); }
static int Blt(lua_State *L) { M_APM a=Bget(L,1); M_APM b=Bget(L,2); lua_pushboolean(L,m_apm_compare(a,b)<0); return 1; }
static int Bcompare(lua_State *L) /** compare(x,y) */ { M_APM a=Bget(L,1); M_APM b=Bget(L,2); lua_pushinteger(L,m_apm_compare(a,b)); return 1; }
/* compute int *n = round_to_nearest_int(a / log(2)) M_APM b = MAPM version of *n returns 0: OK -1, 1: failure */ int M_exp_compute_nn(int *n, M_APM b, M_APM a) { M_APM tmp0, tmp1; void *vp; char *cp, sbuf[48]; int kk; *n = 0; vp = NULL; cp = sbuf; tmp0 = M_get_stack_var(); tmp1 = M_get_stack_var(); /* find 'n' and convert it to a normal C int */ /* we just need an approx 1/log(2) for this calculation */ m_apm_multiply(tmp1, a, MM_exp_log2R); /* round to the nearest int */ if (tmp1->m_apm_sign >= 0) { m_apm_add(tmp0, tmp1, MM_0_5); m_apm_floor(tmp1, tmp0); } else { m_apm_subtract(tmp0, tmp1, MM_0_5); m_apm_ceil(tmp1, tmp0); } kk = tmp1->m_apm_exponent; if (kk >= 42) { if ((vp = (void *)MAPM_MALLOC((kk + 16) * sizeof(char))) == NULL) { /* fatal, this does not return */ M_apm_log_error_msg(M_APM_FATAL, "\'M_exp_compute_nn\', Out of memory"); } cp = (char *)vp; } m_apm_to_integer_string(cp, tmp1); *n = atoi(cp); m_apm_set_long(b, (long)(*n)); kk = m_apm_compare(b, tmp1); if (vp != NULL) MAPM_FREE(vp); M_restore_stack(2); return(kk); }
int m_apm_compare_mt(M_APM ltmp, M_APM rtmp) { int ret; m_apm_enter(); ret=m_apm_compare(ltmp,rtmp); m_apm_leave(); return(ret); }
void M_limit_angle_to_pi(M_APM rr, int places, M_APM aa) { M_APM tmp7, tmp8, tmp9; M_check_PI_places(places); tmp9 = M_get_stack_var(); m_apm_copy(tmp9, MM_lc_PI); if (m_apm_compare(aa, tmp9) == 1) /* > PI */ { tmp7 = M_get_stack_var(); tmp8 = M_get_stack_var(); m_apm_add(tmp7, aa, tmp9); m_apm_integer_divide(tmp9, tmp7, MM_lc_2_PI); m_apm_multiply(tmp8, tmp9, MM_lc_2_PI); m_apm_subtract(tmp9, aa, tmp8); m_apm_round(rr, places, tmp9); M_restore_stack(3); return; } tmp9->m_apm_sign = -1; if (m_apm_compare(aa, tmp9) == -1) /* < -PI */ { tmp7 = M_get_stack_var(); tmp8 = M_get_stack_var(); m_apm_add(tmp7, aa, tmp9); m_apm_integer_divide(tmp9, tmp7, MM_lc_2_PI); m_apm_multiply(tmp8, tmp9, MM_lc_2_PI); m_apm_subtract(tmp9, aa, tmp8); m_apm_round(rr, places, tmp9); M_restore_stack(3); return; } m_apm_copy(rr, aa); M_restore_stack(1); }
void m_apm_arccos(M_APM r, int places, M_APM x) { M_APM tmp0, tmp1, tmp2, tmp3, current_x; int ii, maxiter, maxp, tolerance, local_precision; current_x = M_get_stack_var(); tmp0 = M_get_stack_var(); tmp1 = M_get_stack_var(); tmp2 = M_get_stack_var(); tmp3 = M_get_stack_var(); m_apm_absolute_value(tmp0, x); ii = m_apm_compare(tmp0, MM_One); if (ii == 1) /* |x| > 1 */ { M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_arccos\', |Argument| > 1"); M_set_to_zero(r); M_restore_stack(5); return; } if (ii == 0) /* |x| == 1, arccos = 0, PI */ { if (x->m_apm_sign == 1) { M_set_to_zero(r); } else { M_check_PI_places(places); m_apm_round(r, places, MM_lc_PI); } M_restore_stack(5); return; } if (m_apm_compare(tmp0, MM_0_85) == 1) /* check if > 0.85 */ { M_cos_to_sin(tmp2, (places + 4), x); if (x->m_apm_sign == 1) { m_apm_arcsin(r, places, tmp2); } else { M_check_PI_places(places); m_apm_arcsin(tmp3, (places + 4), tmp2); m_apm_subtract(tmp1, MM_lc_PI, tmp3); m_apm_round(r, places, tmp1); } M_restore_stack(5); return; } if (x->m_apm_sign == 0) /* input == 0 ?? */ { M_check_PI_places(places); m_apm_round(r, places, MM_lc_HALF_PI); M_restore_stack(5); return; } if (x->m_apm_exponent <= -4) /* input close to 0 ?? */ { M_arccos_near_0(r, places, x); M_restore_stack(5); return; } tolerance = -(places + 4); maxp = places + 8; local_precision = 18; /* * compute the maximum number of iterations * that should be needed to calculate to * the desired accuracy. [ constant below ~= 1 / log(2) ] */ maxiter = (int)(log((double)(places + 2)) * 1.442695) + 3; if (maxiter < 5) maxiter = 5; M_get_acos_guess(current_x, x); /* Use the following iteration to solve for arc-cos : cos(X) - N X = X + ------------ n+1 sin(X) */ ii = 0; while (TRUE) { M_4x_cos(tmp1, local_precision, current_x); M_cos_to_sin(tmp2, local_precision, tmp1); if (tmp2->m_apm_sign != 0) tmp2->m_apm_sign = current_x->m_apm_sign; m_apm_subtract(tmp3, tmp1, x); m_apm_divide(tmp0, local_precision, tmp3, tmp2); m_apm_add(tmp2, current_x, tmp0); m_apm_copy(current_x, tmp2); if (ii != 0) { if (((2 * tmp0->m_apm_exponent) < tolerance) || (tmp0->m_apm_sign == 0)) break; } if (++ii == maxiter) { M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_arccos\', max iteration count reached"); break; } local_precision *= 2; if (local_precision > maxp) local_precision = maxp; } m_apm_round(r, places, current_x); M_restore_stack(5); }
int main(int argc, char *argv[]) { char version_info[80]; int ct; /* declare the M_APM variables ... */ M_APM aa_mapm; M_APM bb_mapm; M_APM cc_mapm; M_APM dd_mapm; if (argc < 2) { m_apm_lib_short_version(version_info); fprintf(stdout, "Usage: primenum number\t\t\t[Version 1.3, MAPM Version %s]\n", version_info); fprintf(stdout, " find the first 10 prime numbers starting with \'number\'\n"); exit(4); } /* now initialize the M_APM variables ... */ aa_mapm = m_apm_init(); bb_mapm = m_apm_init(); cc_mapm = m_apm_init(); dd_mapm = m_apm_init(); init_working_mapm(); m_apm_set_string(dd_mapm, argv[1]); /* * if input < 3, set start point = 3 */ if (m_apm_compare(dd_mapm, MM_Three) == -1) { m_apm_copy(dd_mapm, MM_Three); } /* * make sure we start with an odd integer */ m_apm_integer_divide(aa_mapm, dd_mapm, MM_Two); m_apm_multiply(bb_mapm, MM_Two, aa_mapm); m_apm_add(aa_mapm, MM_One, bb_mapm); ct = 0; while (TRUE) { if (is_number_prime(aa_mapm)) { m_apm_to_integer_string(buffer, aa_mapm); fprintf(stdout,"%s\n",buffer); if (++ct == 10) break; } m_apm_add(cc_mapm, MM_Two, aa_mapm); m_apm_copy(aa_mapm, cc_mapm); } free_working_mapm(); m_apm_free(aa_mapm); m_apm_free(bb_mapm); m_apm_free(cc_mapm); m_apm_free(dd_mapm); m_apm_free_all_mem(); exit(0); }
/* * functions returns TRUE if the M_APM input number is prime * FALSE if it is not */ int is_number_prime(M_APM input) { int ii, ret, index; char sbuf[32]; /* * for reference: * * table size of 2 to filter multiples of 2 and 3 * table size of 8 to filter multiples of 2, 3 and 5 * table size of 480 to filter multiples of 2,3,5,7, and 11 * * this increment table will filter out all numbers * that are multiples of 2,3,5 and 7. */ static char incr_table[48] = { 2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 4, 2, 6, 4, 6, 8, 4, 2, 4, 2, 4, 8, 6, 4, 6, 2, 4, 6, 2, 6, 6, 4, 2, 4, 6, 2, 6, 4, 2, 4, 2, 10, 2, 10 }; /* * since the real algorithm starts at 11 (to syncronize * with the increment table), we will cheat for numbers < 10. */ if (m_apm_compare(input, MM_Ten) <= 0) { m_apm_to_integer_string(sbuf, input); ii = atoi(sbuf); if (ii == 2 || ii == 3 || ii == 5 || ii == 7) return(TRUE); else return(FALSE); } ret = FALSE; index = 0; /* * see if the input number is a * multiple of 3, 5, or 7. */ m_apm_integer_div_rem(M_quot, M_rem, input, MM_Three); if (m_apm_sign(M_rem) == 0) /* remainder == 0 */ return(ret); m_apm_integer_div_rem(M_quot, M_rem, input, MM_Five); if (m_apm_sign(M_rem) == 0) return(ret); m_apm_set_long(M_digit, 7L); m_apm_integer_div_rem(M_quot, M_rem, input, M_digit); if (m_apm_sign(M_rem) == 0) return(ret); ii = m_apm_exponent(input) + 16; m_apm_sqrt(M_tmp1, ii, input); m_apm_add(M_limit, MM_Two, M_tmp1); m_apm_set_long(M_digit, 11L); /* now start at '11' to check */ while (TRUE) { if (m_apm_compare(M_digit, M_limit) >= 0) { ret = TRUE; break; } m_apm_integer_div_rem(M_quot, M_rem, input, M_digit); if (m_apm_sign(M_rem) == 0) /* remainder == 0 */ break; m_apm_set_long(M_tmp1, (long)incr_table[index]); m_apm_add(M_tmp0, M_digit, M_tmp1); m_apm_copy(M_digit, M_tmp0); if (++index == 48) index = 0; } return(ret); }
void m_apm_factorial(M_APM moutput, M_APM minput) { int ii, nmul, ndigits, nd, jj, kk, mm, ct; M_APM array[NDIM]; M_APM iprod1, iprod2, tmp1, tmp2; /* return 1 for any input <= 1 */ if (m_apm_compare(minput, MM_One) <= 0) { m_apm_copy(moutput, MM_One); return; } ct = 0; mm = NDIM - 2; ndigits = 256; nd = ndigits - 20; tmp1 = m_apm_init(); tmp2 = m_apm_init(); iprod1 = m_apm_init(); iprod2 = m_apm_init(); array[0] = m_apm_init(); m_apm_copy(tmp2, minput); /* loop until multiply count-down has reached '2' */ while (TRUE) { m_apm_copy(iprod1, MM_One); /* * loop until the number of significant digits in this * partial result is slightly less than 256 */ while (TRUE) { m_apm_multiply(iprod2, iprod1, tmp2); m_apm_subtract(tmp1, tmp2, MM_One); m_apm_multiply(iprod1, iprod2, tmp1); /* * I know, I know. There just isn't a *clean* way * to break out of 2 nested loops. */ if (m_apm_compare(tmp1, MM_Two) <= 0) goto PHASE2; m_apm_subtract(tmp2, tmp1, MM_One); if (iprod1->m_apm_datalength > nd) break; } if (ct == (NDIM - 1)) { /* * if the array has filled up, start multiplying * some of the partial products now. */ m_apm_copy(tmp1, array[mm]); m_apm_multiply(array[mm], iprod1, tmp1); if (mm == 0) { mm = NDIM - 2; ndigits = ndigits << 1; nd = ndigits - 20; } else mm--; } else { /* * store this partial product in the array * and allocate the next array element */ m_apm_copy(array[ct], iprod1); array[++ct] = m_apm_init(); } } PHASE2: m_apm_copy(array[ct], iprod1); kk = ct; while (kk != 0) { ii = 0; jj = 0; nmul = (kk + 1) >> 1; while (TRUE) { /* must use tmp var when ii,jj point to same element */ if (ii == 0) { m_apm_copy(tmp1, array[ii]); m_apm_multiply(array[jj], tmp1, array[ii+1]); } else m_apm_multiply(array[jj], array[ii], array[ii+1]); if (++jj == nmul) break; ii += 2; } if ((kk & 1) == 0) { jj = kk >> 1; m_apm_copy(array[jj], array[kk]); } kk = kk >> 1; }
int MAPM::compare(const MAPM &m) const { return m_apm_compare(cval(),m.cval()); }
bool MAPM::operator>=(const MAPM &m) const { return m_apm_compare(cval(),m.cval())>=0; }
void M_apm_sdivide(M_APM r, int places, M_APM a, M_APM b) { int j, k, m, b0, sign, nexp, indexr, icompare, iterations; long trial_numer; void *vp; if (M_div_firsttime) { M_div_firsttime = FALSE; M_div_worka = m_apm_init(); M_div_workb = m_apm_init(); M_div_tmp7 = m_apm_init(); M_div_tmp8 = m_apm_init(); M_div_tmp9 = m_apm_init(); } sign = a->m_apm_sign * b->m_apm_sign; if (sign == 0) /* one number is zero, result is zero */ { if (b->m_apm_sign == 0) { M_apm_log_error_msg(M_APM_RETURN, "\'M_apm_sdivide\', Divide by 0"); } M_set_to_zero(r); return; } /* * Knuth step D1. Since base = 100, base / 2 = 50. * (also make the working copies positive) */ if (b->m_apm_data[0] >= 50) { m_apm_absolute_value(M_div_worka, a); m_apm_absolute_value(M_div_workb, b); } else /* 'normal' step D1 */ { k = 100 / (b->m_apm_data[0] + 1); m_apm_set_long(M_div_tmp9, (long)k); m_apm_multiply(M_div_worka, M_div_tmp9, a); m_apm_multiply(M_div_workb, M_div_tmp9, b); M_div_worka->m_apm_sign = 1; M_div_workb->m_apm_sign = 1; } /* setup trial denominator for step D3 */ b0 = 100 * (int)M_div_workb->m_apm_data[0]; if (M_div_workb->m_apm_datalength >= 3) b0 += M_div_workb->m_apm_data[1]; nexp = M_div_worka->m_apm_exponent - M_div_workb->m_apm_exponent; if (nexp > 0) iterations = nexp + places + 1; else iterations = places + 1; k = (iterations + 1) >> 1; /* required size of result, in bytes */ if (k > r->m_apm_malloclength) { if ((vp = MAPM_REALLOC(r->m_apm_data, (k + 32))) == NULL) { /* fatal, this does not return */ M_apm_log_error_msg(M_APM_FATAL, "\'M_apm_sdivide\', Out of memory"); } r->m_apm_malloclength = k + 28; r->m_apm_data = (UCHAR *)vp; } /* clear the exponent in the working copies */ M_div_worka->m_apm_exponent = 0; M_div_workb->m_apm_exponent = 0; /* if numbers are equal, ratio == 1.00000... */ if ((icompare = m_apm_compare(M_div_worka, M_div_workb)) == 0) { iterations = 1; r->m_apm_data[0] = 10; nexp++; } else /* ratio not 1, do the real division */ { if (icompare == 1) /* numerator > denominator */ { nexp++; /* to adjust the final exponent */ M_div_worka->m_apm_exponent += 1; /* multiply numerator by 10 */ } else /* numerator < denominator */ { M_div_worka->m_apm_exponent += 2; /* multiply numerator by 100 */ } indexr = 0; m = 0; while (TRUE) { /* * Knuth step D3. Only use the 3rd -> 6th digits if the number * actually has that many digits. */ trial_numer = 10000L * (long)M_div_worka->m_apm_data[0]; if (M_div_worka->m_apm_datalength >= 5) { trial_numer += 100 * M_div_worka->m_apm_data[1] + M_div_worka->m_apm_data[2]; } else { if (M_div_worka->m_apm_datalength >= 3) trial_numer += 100 * M_div_worka->m_apm_data[1]; } j = (int)(trial_numer / b0); /* * Since the library 'normalizes' all the results, we need * to look at the exponent of the number to decide if we * have a lead in 0n or 00. */ if ((k = 2 - M_div_worka->m_apm_exponent) > 0) { while (TRUE) { j /= 10; if (--k == 0) break; } } if (j == 100) /* qhat == base ?? */ j = 99; /* if so, decrease by 1 */ m_apm_set_long(M_div_tmp8, (long)j); m_apm_multiply(M_div_tmp7, M_div_tmp8, M_div_workb); /* * Compare our q-hat (j) against the desired number. * j is either correct, 1 too large, or 2 too large * per Theorem B on pg 272 of Art of Compter Programming, * Volume 2, 3rd Edition. * * The above statement is only true if using the 2 leading * digits of the numerator and the leading digit of the * denominator. Since we are using the (3) leading digits * of the numerator and the (2) leading digits of the * denominator, we eliminate the case where our q-hat is * 2 too large, (and q-hat being 1 too large is quite remote). */ if (m_apm_compare(M_div_tmp7, M_div_worka) == 1) { j--; m_apm_subtract(M_div_tmp8, M_div_tmp7, M_div_workb); m_apm_copy(M_div_tmp7, M_div_tmp8); } /* * Since we know q-hat is correct, step D6 is unnecessary. * * Store q-hat, step D5. Since D6 is unnecessary, we can * do D5 before D4 and decide if we are done. */ r->m_apm_data[indexr++] = (UCHAR)j; /* j == 'qhat' */ m += 2; if (m >= iterations) break; /* step D4 */ m_apm_subtract(M_div_tmp9, M_div_worka, M_div_tmp7); /* * if the subtraction yields zero, the division is exact * and we are done early. */ if (M_div_tmp9->m_apm_sign == 0) { iterations = m; break; } /* multiply by 100 and re-save */ M_div_tmp9->m_apm_exponent += 2; m_apm_copy(M_div_worka, M_div_tmp9); } } r->m_apm_sign = sign; r->m_apm_exponent = nexp; r->m_apm_datalength = iterations; M_apm_normalize(r); }
/* Calculate the POW function by calling EXP : Y A X = e where A = Y * log(X) */ void m_apm_pow(M_APM rr, int places, M_APM xx, M_APM yy) { int iflag, pflag; char sbuf[64]; M_APM tmp8, tmp9; /* if yy == 0, return 1 */ if (yy->m_apm_sign == 0) { m_apm_copy(rr, MM_One); return; } /* if xx == 0, return 0 */ if (xx->m_apm_sign == 0) { M_set_to_zero(rr); return; } if (M_size_flag == 0) /* init locals on first call */ { M_size_flag = M_get_sizeof_int(); M_last_log_digits = 0; M_last_xx_input = m_apm_init(); M_last_xx_log = m_apm_init(); } /* * if 'yy' is a small enough integer, call the more * efficient _integer_pow function. */ if (m_apm_is_integer(yy)) { iflag = FALSE; if (M_size_flag == 2) /* 16 bit compilers */ { if (yy->m_apm_exponent <= 4) iflag = TRUE; } else /* >= 32 bit compilers */ { if (yy->m_apm_exponent <= 7) iflag = TRUE; } if (iflag) { m_apm_to_integer_string(sbuf, yy); m_apm_integer_pow(rr, places, xx, atoi(sbuf)); return; } } tmp8 = M_get_stack_var(); tmp9 = M_get_stack_var(); /* * If parameter 'X' is the same this call as it * was the previous call, re-use the saved log * calculation from last time. */ pflag = FALSE; if (M_last_log_digits >= places) { if (m_apm_compare(xx, M_last_xx_input) == 0) pflag = TRUE; } if (pflag) { m_apm_round(tmp9, (places + 8), M_last_xx_log); } else { m_apm_log(tmp9, (places + 8), xx); M_last_log_digits = places + 2; /* save the 'X' input value and the log calculation */ m_apm_copy(M_last_xx_input, xx); m_apm_copy(M_last_xx_log, tmp9); } m_apm_multiply(tmp8, tmp9, yy); m_apm_exp(rr, places, tmp8); M_restore_stack(2); /* restore the 2 locals we used here */ }