/* * arcsinh(x) == log [ x + sqrt(x^2 + 1) ] * * also, use arcsinh(-x) == -arcsinh(x) */ void m_apm_arcsinh(M_APM rr, int places, M_APM aa) { M_APM tmp0, tmp1, tmp2; /* result is 0 if input is 0 */ if (aa->m_apm_sign == 0) { M_set_to_zero(rr); return; } tmp0 = M_get_stack_var(); tmp1 = M_get_stack_var(); tmp2 = M_get_stack_var(); m_apm_absolute_value(tmp0, aa); m_apm_multiply(tmp1, tmp0, tmp0); m_apm_add(tmp2, tmp1, MM_One); m_apm_sqrt(tmp1, (places + 6), tmp2); m_apm_add(tmp2, tmp0, tmp1); m_apm_log(rr, places, tmp2); rr->m_apm_sign = aa->m_apm_sign; /* fix final sign */ 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); }
/* Calculate arctan using the identity : x arctan (x) == arcsin [ --------------- ] sqrt(1 + x^2) */ void m_apm_arctan(M_APM rr, int places, M_APM xx) { M_APM tmp8, tmp9; if (xx->m_apm_sign == 0) /* input == 0 ?? */ { M_set_to_zero(rr); return; } if (xx->m_apm_exponent <= -4) /* input close to 0 ?? */ { M_arctan_near_0(rr, places, xx); return; } if (xx->m_apm_exponent >= 4) /* large input */ { M_arctan_large_input(rr, places, xx); return; } tmp8 = M_get_stack_var(); tmp9 = M_get_stack_var(); m_apm_multiply(tmp9, xx, xx); m_apm_add(tmp8, tmp9, MM_One); m_apm_sqrt(tmp9, (places + 6), tmp8); m_apm_divide(tmp8, (places + 6), xx, tmp9); m_apm_arcsin(rr, places, tmp8); M_restore_stack(2); }
/* * compute r = sqrt(1 - a ^ 2). */ void M_cos_to_sin(M_APM r, int places, M_APM a) { M_APM tmp1, tmp2; tmp1 = M_get_stack_var(); tmp2 = M_get_stack_var(); m_apm_multiply(tmp1, a, a); m_apm_subtract(tmp2, MM_One, tmp1); m_apm_sqrt(r, places, tmp2); M_restore_stack(2); }
/* * 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_log_AGM_R_func(M_APM rr, int places, M_APM aa, M_APM bb) { M_APM tmp1, tmp2, tmp3, tmp4, tmpC2, sum, pow_2, tmpA0, tmpB0; int tolerance, dplaces; tmpA0 = M_get_stack_var(); tmpB0 = M_get_stack_var(); tmpC2 = M_get_stack_var(); tmp1 = M_get_stack_var(); tmp2 = M_get_stack_var(); tmp3 = M_get_stack_var(); tmp4 = M_get_stack_var(); sum = M_get_stack_var(); pow_2 = M_get_stack_var(); tolerance = places + 8; dplaces = places + 16; m_apm_copy(tmpA0, aa); m_apm_copy(tmpB0, bb); m_apm_copy(pow_2, MM_0_5); m_apm_multiply(tmp1, aa, aa); /* 0.5 * [ a ^ 2 - b ^ 2 ] */ m_apm_multiply(tmp2, bb, bb); m_apm_subtract(tmp3, tmp1, tmp2); m_apm_multiply(sum, MM_0_5, tmp3); while (TRUE) { m_apm_subtract(tmp1, tmpA0, tmpB0); /* C n+1 = 0.5 * [ An - Bn ] */ m_apm_multiply(tmp4, MM_0_5, tmp1); /* C n+1 */ m_apm_multiply(tmpC2, tmp4, tmp4); /* C n+1 ^ 2 */ /* do the AGM */ m_apm_add(tmp1, tmpA0, tmpB0); m_apm_multiply(tmp3, MM_0_5, tmp1); m_apm_multiply(tmp2, tmpA0, tmpB0); m_apm_sqrt(tmpB0, dplaces, tmp2); m_apm_round(tmpA0, dplaces, tmp3); /* end AGM */ m_apm_multiply(tmp2, MM_Two, pow_2); m_apm_copy(pow_2, tmp2); m_apm_multiply(tmp1, tmpC2, pow_2); m_apm_add(tmp3, sum, tmp1); if ((tmp1->m_apm_sign == 0) || ((-2 * tmp1->m_apm_exponent) > tolerance)) break; m_apm_round(sum, dplaces, tmp3); } m_apm_subtract(tmp4, MM_One, tmp3); m_apm_reciprocal(rr, places, tmp4); M_restore_stack(9); }
void m_apm_root4(M_APM x, int dec_places, M_APM y) { m_apm_sqrt(x, dec_places, y); m_apm_copy(y, x); m_apm_sqrt(x, dec_places, y); }
void m_apm_sqrt_mt(M_APM rr, int places, M_APM aa) { m_apm_enter(); m_apm_sqrt(rr,places,aa); m_apm_leave(); }
/* * Calculate PI using the AGM (Arithmetic-Geometric Mean) * * Init : A0 = 1 * B0 = 1 / sqrt(2) * Sum = 1 * * Iterate: n = 1... * * * A = 0.5 * [ A + B ] * n n-1 n-1 * * * B = sqrt [ A * B ] * n n-1 n-1 * * * * C = 0.5 * [ A - B ] * n n-1 n-1 * * * 2 n+1 * Sum = Sum - C * 2 * n * * * At the end when C is 'small enough' : * n * * 2 * PI = 4 * A / Sum * n+1 * * -OR- * * 2 * PI = ( A + B ) / Sum * n n * */ void M_calculate_PI_AGM(M_APM outv, int places) { M_APM tmp1, tmp2, a0, b0, c0, a1, b1, sum, pow_2; int dplaces, nn; tmp1 = M_get_stack_var(); tmp2 = M_get_stack_var(); a0 = M_get_stack_var(); b0 = M_get_stack_var(); c0 = M_get_stack_var(); a1 = M_get_stack_var(); b1 = M_get_stack_var(); sum = M_get_stack_var(); pow_2 = M_get_stack_var(); dplaces = places + 16; m_apm_copy(a0, MM_One); m_apm_copy(sum, MM_One); m_apm_copy(pow_2, MM_Four); m_apm_sqrt(b0, dplaces, MM_0_5); /* sqrt(0.5) */ while (TRUE) { m_apm_add(tmp1, a0, b0); m_apm_multiply(a1, MM_0_5, tmp1); m_apm_multiply(tmp1, a0, b0); m_apm_sqrt(b1, dplaces, tmp1); m_apm_subtract(tmp1, a0, b0); m_apm_multiply(c0, MM_0_5, tmp1); /* * the net 'PI' calculated from this iteration will * be accurate to ~4 X the value of (c0)'s exponent. * this was determined experimentally. */ nn = -4 * c0->m_apm_exponent; m_apm_multiply(tmp1, c0, c0); m_apm_multiply(tmp2, tmp1, pow_2); m_apm_subtract(tmp1, sum, tmp2); m_apm_round(sum, dplaces, tmp1); if (nn >= dplaces) break; m_apm_copy(a0, a1); m_apm_copy(b0, b1); m_apm_multiply(tmp1, pow_2, MM_Two); m_apm_copy(pow_2, tmp1); } m_apm_add(tmp1, a1, b1); m_apm_multiply(tmp2, tmp1, tmp1); m_apm_divide(tmp1, dplaces, tmp2, sum); m_apm_round(outv, places, tmp1); M_restore_stack(9); }