/* calculate arctan (x) with the following series: x^3 x^5 x^7 x^9 arctan (x) == x - --- + --- - --- + --- ... 3 5 7 9 */ void M_arctan_near_0(M_APM rr, int places, M_APM aa) { M_APM tmp0, tmpR, tmp2, tmpS, digit, term; int tolerance, local_precision; long m1; tmp0 = M_get_stack_var(); tmp2 = M_get_stack_var(); tmpR = M_get_stack_var(); tmpS = M_get_stack_var(); term = M_get_stack_var(); digit = M_get_stack_var(); tolerance = aa->m_apm_exponent - places - 4; local_precision = places + 8 - aa->m_apm_exponent; m_apm_copy(term, aa); m_apm_copy(tmpS, aa); m_apm_multiply(tmp0, aa, aa); m_apm_round(tmp2, (local_precision + 8), tmp0); m1 = 1; while (TRUE) { m1 += 2; m_apm_set_long(digit, m1); m_apm_multiply(tmp0, term, tmp2); m_apm_round(term, local_precision, tmp0); m_apm_divide(tmp0, local_precision, term, digit); m_apm_subtract(tmpR, tmpS, tmp0); if ((tmp0->m_apm_exponent < tolerance) || (tmp0->m_apm_sign == 0)) { m_apm_round(rr, places, tmpR); break; } m1 += 2; m_apm_set_long(digit, m1); m_apm_multiply(tmp0, term, tmp2); m_apm_round(term, local_precision, tmp0); m_apm_divide(tmp0, local_precision, term, digit); m_apm_add(tmpS, tmpR, tmp0); if ((tmp0->m_apm_exponent < tolerance) || (tmp0->m_apm_sign == 0)) { m_apm_round(rr, places, tmpS); break; } } M_restore_stack(6); /* restore the 6 locals we used here */ }
/* calculate log (1 + x) with the following series: x y = ----- ( |y| < 1 ) x + 2 [ 1 + y ] y^3 y^5 y^7 log [-------] = 2 * [ y + --- + --- + --- ... ] [ 1 - y ] 3 5 7 */ void M_log_near_1(M_APM rr, int places, M_APM xx) { M_APM tmp0, tmp1, tmp2, tmpS, term; int tolerance, dplaces, local_precision; long m1; tmp0 = M_get_stack_var(); tmp1 = M_get_stack_var(); tmp2 = M_get_stack_var(); tmpS = M_get_stack_var(); term = M_get_stack_var(); tolerance = xx->m_apm_exponent - (places + 6); dplaces = (places + 12) - xx->m_apm_exponent; m_apm_add(tmp0, xx, MM_Two); m_apm_divide(tmpS, (dplaces + 6), xx, tmp0); m_apm_copy(term, tmpS); m_apm_multiply(tmp0, tmpS, tmpS); m_apm_round(tmp2, (dplaces + 6), tmp0); m1 = 3L; while (TRUE) { m_apm_multiply(tmp0, term, tmp2); if ((tmp0->m_apm_exponent < tolerance) || (tmp0->m_apm_sign == 0)) break; local_precision = dplaces + tmp0->m_apm_exponent; if (local_precision < 20) local_precision = 20; m_apm_set_long(tmp1, m1); m_apm_round(term, local_precision, tmp0); m_apm_divide(tmp0, local_precision, term, tmp1); m_apm_add(tmp1, tmpS, tmp0); m_apm_copy(tmpS, tmp1); m1 += 2; } m_apm_multiply(tmp0, MM_Two, tmpS); m_apm_round(rr, places, tmp0); M_restore_stack(5); /* restore the 5 locals we used here */ }
/* * 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); }
/* 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); }
void M_log_solve_cubic(M_APM rr, int places, M_APM nn) { M_APM tmp0, tmp1, tmp2, tmp3, guess; int ii, maxp, tolerance, local_precision; guess = 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_get_log_guess(guess, nn); tolerance = -(places + 4); maxp = places + 16; local_precision = 18; /* Use the following iteration to solve for log : exp(X) - N X = X - 2 * ------------ n+1 exp(X) + N this is a cubically convergent algorithm (each iteration yields 3X more digits) */ ii = 0; while (TRUE) { m_apm_exp(tmp1, local_precision, guess); m_apm_subtract(tmp3, tmp1, nn); m_apm_add(tmp2, tmp1, nn); m_apm_divide(tmp1, local_precision, tmp3, tmp2); m_apm_multiply(tmp0, MM_Two, tmp1); m_apm_subtract(tmp3, guess, tmp0); if (ii != 0) { if (((3 * tmp0->m_apm_exponent) < tolerance) || (tmp0->m_apm_sign == 0)) break; } m_apm_round(guess, local_precision, tmp3); local_precision *= 3; if (local_precision > maxp) local_precision = maxp; ii = 1; } m_apm_round(rr, places, tmp3); M_restore_stack(5); }
/* Calculate arcsin using the identity : x arcsin (x) == arctan [ --------------- ] sqrt(1 - x^2) */ void M_arcsin_near_0(M_APM rr, int places, M_APM aa) { M_APM tmp5, tmp6; tmp5 = M_get_stack_var(); tmp6 = M_get_stack_var(); M_cos_to_sin(tmp5, (places + 8), aa); m_apm_divide(tmp6, (places + 8), aa, tmp5); M_arctan_near_0(rr, places, tmp6); M_restore_stack(2); }
void m_apm_tan(M_APM r, int places, M_APM a) { M_APM tmps, tmpc, tmp0; tmps = M_get_stack_var(); tmpc = M_get_stack_var(); tmp0 = M_get_stack_var(); m_apm_sin_cos(tmps, tmpc, (places + 4), a); /* tan(x) = sin(x) / cos(x) */ m_apm_divide(tmp0, (places + 4), tmps, tmpc); m_apm_round(r, places, tmp0); M_restore_stack(3); }
/* calculate the exponential function using the following series : x^2 x^3 x^4 x^5 exp(x) == 1 + x + --- + --- + --- + --- ... 2! 3! 4! 5! */ void M_raw_exp(M_APM rr, int places, M_APM xx) { M_APM tmp0, digit, term; int tolerance, local_precision, prev_exp; long m1; tmp0 = M_get_stack_var(); term = M_get_stack_var(); digit = M_get_stack_var(); local_precision = places + 8; tolerance = -(places + 4); prev_exp = 0; m_apm_add(rr, MM_One, xx); m_apm_copy(term, xx); m1 = 2L; while (TRUE) { m_apm_set_long(digit, m1); m_apm_multiply(tmp0, term, xx); m_apm_divide(term, local_precision, tmp0, digit); m_apm_add(tmp0, rr, term); m_apm_copy(rr, tmp0); if ((term->m_apm_exponent < tolerance) || (term->m_apm_sign == 0)) break; if (m1 != 2L) { local_precision = local_precision + term->m_apm_exponent - prev_exp; if (local_precision < 20) local_precision = 20; } prev_exp = term->m_apm_exponent; m1++; } M_restore_stack(3); /* restore the 3 locals we used here */ }
/* for large input values use : arctan(x) = (PI / 2) - arctan(1 / |x|) and sign of result = sign of original input */ void M_arctan_large_input(M_APM rr, int places, M_APM xx) { M_APM tmp1, tmp2; tmp1 = M_get_stack_var(); tmp2 = M_get_stack_var(); M_check_PI_places(places); m_apm_divide(tmp1, (places + 6), MM_One, xx); /* 1 / xx */ tmp1->m_apm_sign = 1; /* make positive */ m_apm_arctan(tmp2, (places + 6), tmp1); m_apm_subtract(tmp1, MM_lc_HALF_PI, tmp2); m_apm_round(rr, places, tmp1); rr->m_apm_sign = xx->m_apm_sign; /* fix final sign */ M_restore_stack(2); }
/* * tanh(x) == [ exp(x) - exp(-x) ] / [ exp(x) + exp(-x) ] */ void m_apm_tanh(M_APM rr, int places, M_APM aa) { M_APM tmp1, tmp2, tmp3, tmp4; int local_precision; tmp1 = M_get_stack_var(); tmp2 = M_get_stack_var(); tmp3 = M_get_stack_var(); tmp4 = M_get_stack_var(); local_precision = places + 4; m_apm_exp(tmp1, local_precision, aa); m_apm_reciprocal(tmp2, local_precision, tmp1); m_apm_subtract(tmp3, tmp1, tmp2); m_apm_add(tmp4, tmp1, tmp2); m_apm_divide(tmp1, local_precision, tmp3, tmp4); m_apm_round(rr, places, tmp1); M_restore_stack(4); }
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); }
void m_apm_arctan2(M_APM rr, int places, M_APM yy, M_APM xx) { M_APM tmp5, tmp6, tmp7; int ix, iy; iy = yy->m_apm_sign; ix = xx->m_apm_sign; if (ix == 0) /* x == 0 */ { if (iy == 0) /* y == 0 */ { M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_arctan2\', Both Inputs = 0"); M_set_to_zero(rr); return; } M_check_PI_places(places); m_apm_round(rr, places, MM_lc_HALF_PI); rr->m_apm_sign = iy; return; } if (iy == 0) { if (ix == 1) { M_set_to_zero(rr); } else { M_check_PI_places(places); m_apm_round(rr, places, MM_lc_PI); } return; } /* * the special cases have been handled, now do the real work */ tmp5 = M_get_stack_var(); tmp6 = M_get_stack_var(); tmp7 = M_get_stack_var(); m_apm_divide(tmp6, (places + 6), yy, xx); m_apm_arctan(tmp5, (places + 6), tmp6); if (ix == 1) /* 'x' is positive */ { m_apm_round(rr, places, tmp5); } else /* 'x' is negative */ { M_check_PI_places(places); if (iy == 1) /* 'y' is positive */ { m_apm_add(tmp7, tmp5, MM_lc_PI); m_apm_round(rr, places, tmp7); } else /* 'y' is negative */ { m_apm_subtract(tmp7, tmp5, MM_lc_PI); m_apm_round(rr, places, tmp7); } } M_restore_stack(3); }
void m_apm_divide_mt(M_APM rr, int places, M_APM aa, M_APM bb) { m_apm_enter(); m_apm_divide(rr,places,aa,bb); 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); }