static void special_overflow (void) { mpfr_t x, y; mpfr_exp_t emin, emax; emin = mpfr_get_emin (); emax = mpfr_get_emax (); set_emin (-125); set_emax (128); mpfr_init2 (x, 24); mpfr_init2 (y, 48); mpfr_set_str_binary (x, "0.101100100000000000110100E0"); mpfr_acos (y, x, MPFR_RNDN); if (mpfr_cmp_str (y, "0.110011010100101111000100111010111011010000001001E0", 2, MPFR_RNDN)) { printf("Special Overflow error.\n"); mpfr_dump (y); exit (1); } mpfr_clear (y); mpfr_clear (x); set_emin (emin); set_emax (emax); }
decimal r_acos(const decimal& a,bool round) { #ifdef USE_CGAL CGAL::Gmpfr m; CGAL::Gmpfr n=to_gmpfr(a); mpfr_acos(m.fr(),n.fr(),MPFR_RNDN); return r_round_preference(decimal(m),round); #else return r_round_preference(acos(a),round); #endif }
static void special (void) { mpfr_t x, y; int inex1, inex2; mpfr_init2 (x, 32); mpfr_init2 (y, 32); mpfr_set_str_binary (x, "0.10001000001001011000100001E-6"); mpfr_acos (y, x, MPFR_RNDN); mpfr_set_str_binary (x, "1.10001111111111110001110110001"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_acos (1)\n"); exit (1); } mpfr_set_str_binary (x, "-0.01101011110111100111010011001011"); mpfr_acos (y, x, MPFR_RNDZ); mpfr_set_str_binary (x, "10.0000000101111000011101000101"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_acos (2)\n"); mpfr_print_binary (y); printf ("\n"); exit (1); } mpfr_set_prec (x, 2); mpfr_set_ui (x, 0, MPFR_RNDN); inex1 = mpfr_acos (x, x, MPFR_RNDN); /* Pi/2 */ inex2 = mpfr_const_pi (x, MPFR_RNDN); if (inex1 != inex2) { printf ("Error in mpfr_acos (3) for prec=2\n"); exit (1); } mpfr_clear (y); mpfr_clear (x); }
/// @brief acos keyword implementation /// void program::rpn_acos(void) { MIN_ARGUMENTS(1); if (_stack->get_type(0) == cmd_number) { floating_t* left = &((number*)_stack->get_obj(0))->_value; CHECK_MPFR(mpfr_acos(left->mpfr, left->mpfr, floating_t::s_mpfr_rnd)); } else if (_stack->get_type(0) == cmd_complex) { // acos(z)=pi/2-asin(z) rpn_asin(); rpn_pi(); number* num = (number*)_stack->allocate_back(number::calc_size(), cmd_number); CHECK_MPFR(mpfr_set_d(num->_value.mpfr, 2.0, floating_t::s_mpfr_rnd)); rpn_div(); rpn_minus(); rpn_neg(); } else ERR_CONTEXT(ret_bad_operand_type); }
static PyObject * _GMPy_MPFR_Acos(PyObject *x, CTXT_Object *context) { MPFR_Object *result; if (!mpfr_nan_p(MPFR(x)) && (mpfr_cmp_si(MPFR(x), 1) > 0 || mpfr_cmp_si(MPFR(x), -1) < 0) && context->ctx.allow_complex ) { return GMPy_Complex_Acos(x, context); } if (!(result = GMPy_MPFR_New(0, context))) { return NULL; } mpfr_clear_flags(); result->rc = mpfr_acos(result->f, MPFR(x), GET_MPFR_ROUND(context)); _GMPy_MPFR_Cleanup(&result, context); return (PyObject*)result; }
REAL _acos(REAL a, REAL, QByteArray &) { mpfr_t tmp1; mpfr_init2(tmp1, NUMBITS); mpfr_t result; mpfr_init2(result, NUMBITS); try { // mpfr_init_set_f(tmp1, a.get_mpf_t(), MPFR_RNDN); mpfr_set_str(tmp1, getString(a).data(), 10, MPFR_RNDN); mpfr_acos(result, tmp1, MPFR_RNDN); mpfr_get_f(a.get_mpf_t(), result, MPFR_RNDN); } catch(...) { mpfr_clear(tmp1); mpfr_clear(result); return ZERO; } mpfr_clear(tmp1); mpfr_clear(result); return a; }
//------------------------------------------------------------------------------ // Name: //------------------------------------------------------------------------------s knumber_base *knumber_float::acos() { if(mpf_cmp_d(mpf_, 1.0) > 0 || mpf_cmp_d(mpf_, -1.0) < 0) { delete this; return new knumber_error(knumber_error::ERROR_UNDEFINED); } #ifdef KNUMBER_USE_MPFR mpfr_t mpfr; mpfr_init_set_f(mpfr, mpf_, rounding_mode); mpfr_acos(mpfr, mpfr, rounding_mode); mpfr_get_f(mpf_, mpfr, rounding_mode); mpfr_clear(mpfr); return this; #else const double x = mpf_get_d(mpf_); if(isinf(x)) { delete this; return new knumber_error(knumber_error::ERROR_POS_INFINITY); } else { return execute_libc_func< ::acos>(x); } #endif }
int main() { long iter; flint_rand_t state; printf("acos...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000; iter++) { arb_t a, b; fmpq_t q; mpfr_t t; long prec = 2 + n_randint(state, 200); arb_init(a); arb_init(b); fmpq_init(q); mpfr_init2(t, prec + 100); arb_randtest(a, state, 1 + n_randint(state, 200), 3); arb_randtest(b, state, 1 + n_randint(state, 200), 3); arb_get_rand_fmpq(q, state, a, 1 + n_randint(state, 200)); fmpq_get_mpfr(t, q, MPFR_RNDN); mpfr_acos(t, t, MPFR_RNDN); arb_acos(b, a, prec); if (!arb_contains_mpfr(b, t)) { printf("FAIL: containment\n\n"); printf("a = "); arb_printd(a, 100); printf("\n\n"); printf("b = "); arb_printd(b, 100); printf("\n\n"); abort(); } arb_acos(a, a, prec); if (!arb_equal(a, b)) { printf("FAIL: aliasing\n\n"); abort(); } arb_clear(a); arb_clear(b); fmpq_clear(q); mpfr_clear(t); } /* check large arguments */ for (iter = 0; iter < 10000; iter++) { arb_t a, b, c; long prec1, prec2; prec1 = 2 + n_randint(state, 1000); prec2 = prec1 + 30; arb_init(a); arb_init(b); arb_init(c); arb_randtest_precise(a, state, 1 + n_randint(state, 1000), 100); arb_acos(b, a, prec1); arb_acos(c, a, prec2); if (!arb_overlaps(b, c)) { printf("FAIL: overlap\n\n"); printf("a = "); arb_print(a); printf("\n\n"); printf("b = "); arb_print(b); printf("\n\n"); printf("c = "); arb_print(c); printf("\n\n"); abort(); } /* check sin(asin(x)) = x */ arb_cos(c, b, prec1); if (!arb_contains(c, a)) { printf("FAIL: functional equation\n\n"); printf("a = "); arb_print(a); printf("\n\n"); printf("b = "); arb_print(b); printf("\n\n"); printf("c = "); arb_print(c); printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); arb_clear(c); } flint_randclear(state); flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int main (int argc, char *argv[]) { int n, prec, st, st2, N, i; mpfr_t x, y, z; if (argc != 2 && argc != 3) { fprintf(stderr, "Usage: timing digits \n"); exit(1); } printf ("Using MPFR-%s with GMP-%s\n", mpfr_version, gmp_version); n = atoi(argv[1]); prec = (int) ( n * log(10.0) / log(2.0) + 1.0 ); printf("[precision is %u bits]\n", prec); mpfr_init2(x, prec); mpfr_init2(y, prec); mpfr_init2(z, prec); mpfr_set_d(x, 3.0, GMP_RNDN); mpfr_sqrt(x, x, GMP_RNDN); mpfr_sub_ui (x, x, 1, GMP_RNDN); mpfr_set_d(y, 5.0, GMP_RNDN); mpfr_sqrt(y, y, GMP_RNDN); mpfr_log (z, x, GMP_RNDN); N=1; st = cputime(); do { for (i=0;i<N;i++) mpfr_mul(z, x, y, GMP_RNDN); N=2*N; st2=cputime(); } while (st2-st<1000); printf("x*y took %f ms (%d eval in %d ms)\n", (double)(st2-st)/(N-1),N-1,st2-st); N=1; st = cputime(); do { for (i=0;i<N;i++) mpfr_mul(z, x, x, GMP_RNDN); N=2*N; st2=cputime(); } while (st2-st<1000); printf("x*x took %f ms (%d eval in %d ms)\n", (double)(st2-st)/(N-1),N-1,st2-st); N=1; st = cputime(); do { for (i=0;i<N;i++) mpfr_div(z, x, y, GMP_RNDN); N=2*N; st2=cputime(); } while (st2-st<1000); printf("x/y took %f ms (%d eval in %d ms)\n", (double)(st2-st)/(N-1),N-1,st2-st); N=1; st = cputime(); do { for (i=0;i<N;i++) mpfr_sqrt(z, x, GMP_RNDN); N=2*N; st2=cputime(); } while (st2-st<1000); printf("sqrt(x) took %f ms (%d eval in %d ms)\n", (double)(st2-st)/(N-1),N-1,st2-st); N=1; st = cputime(); do { for (i=0;i<N;i++) mpfr_exp(z, x, GMP_RNDN); N=2*N; st2=cputime(); } while (st2-st<1000); printf("exp(x) took %f ms (%d eval in %d ms)\n", (double)(st2-st)/(N-1),N-1,st2-st); N=1; st = cputime(); do { for (i=0;i<N;i++) mpfr_log(z, x, GMP_RNDN); N=2*N; st2=cputime(); } while (st2-st<1000); printf("log(x) took %f ms (%d eval in %d ms)\n", (double)(st2-st)/(N-1),N-1,st2-st); N=1; st = cputime(); do { for (i=0;i<N;i++) mpfr_sin(z, x, GMP_RNDN); N=2*N; st2=cputime(); } while (st2-st<1000); printf("sin(x) took %f ms (%d eval in %d ms)\n", (double)(st2-st)/(N-1),N-1,st2-st); N=1; st = cputime(); do { for (i=0;i<N;i++) mpfr_cos(z, x, GMP_RNDN); N=2*N; st2=cputime(); } while (st2-st<1000); printf("cos(x) took %f ms (%d eval in %d ms)\n", (double)(st2-st)/(N-1),N-1,st2-st); N=1; st = cputime(); do { for (i=0;i<N;i++) mpfr_acos(z, x, GMP_RNDN); N=2*N; st2=cputime(); } while (st2-st<1000); printf("arccos(x) took %f ms (%d eval in %d ms)\n", (double)(st2-st)/(N-1),N-1,st2-st); N=1; st = cputime(); do { for (i=0;i<N;i++) mpfr_atan(z, x, GMP_RNDN); N=2*N; st2=cputime(); } while (st2-st<1000); printf("arctan(x) took %f ms (%d eval in %d ms)\n", (double)(st2-st)/(N-1),N-1,st2-st); mpfr_clear(x); mpfr_clear(y); mpfr_clear(z); return 0; }
MpfrFloat MpfrFloat::acos(const MpfrFloat& value) { MpfrFloat retval(MpfrFloat::kNoInitialization); mpfr_acos(retval.mData->mFloat, value.mData->mFloat, GMP_RNDN); return retval; }
int main (void) { mpfr_t x, y; int r; tests_start_mpfr (); special_overflow (); special (); mpfr_init (x); mpfr_init (y); MPFR_SET_NAN(x); mpfr_acos (y, x, MPFR_RNDN); if (mpfr_nan_p(y) == 0) { printf ("Error: acos(NaN) != NaN\n"); exit (1); } mpfr_set_ui (x, 2, MPFR_RNDN); mpfr_acos (y, x, MPFR_RNDN); if (mpfr_nan_p(y) == 0) { printf ("Error: acos(2) != NaN\n"); exit (1); } mpfr_set_si (x, -2, MPFR_RNDN); mpfr_acos (y, x, MPFR_RNDN); if (mpfr_nan_p(y) == 0) { printf ("Error: acos(-2) != NaN\n"); exit (1); } /* acos (1) = 0 */ mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_acos (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) { printf ("Error: acos(1) != +0.0\n"); exit (1); } /* acos (0) = Pi/2 */ for (r = 0; r < MPFR_RND_MAX; r++) { mpfr_set_ui (x, 0, MPFR_RNDN); /* exact */ mpfr_acos (y, x, (mpfr_rnd_t) r); mpfr_const_pi (x, (mpfr_rnd_t) r); mpfr_div_2exp (x, x, 1, MPFR_RNDN); /* exact */ if (mpfr_cmp (x, y)) { printf ("Error: acos(0) != Pi/2 for rnd=%s\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r)); exit (1); } } /* acos (-1) = Pi */ for (r = 0; r < MPFR_RND_MAX; r++) { mpfr_set_si (x, -1, MPFR_RNDN); /* exact */ mpfr_acos (y, x, (mpfr_rnd_t) r); mpfr_const_pi (x, (mpfr_rnd_t) r); if (mpfr_cmp (x, y)) { printf ("Error: acos(1) != Pi for rnd=%s\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r)); exit (1); } } test_generic (2, 100, 7); mpfr_clear (x); mpfr_clear (y); data_check ("data/acos", mpfr_acos, "mpfr_acos"); bad_cases (mpfr_acos, mpfr_cos, "mpfr_acos", 0, -40, 2, 4, 128, 800, 30); tests_end_mpfr (); return 0; }
int mpc_acos (mpc_ptr rop, mpc_srcptr op, mpc_rnd_t rnd) { int inex_re, inex_im, inex; mpfr_prec_t p_re, p_im, p; mpc_t z1; mpfr_t pi_over_2; mpfr_exp_t e1, e2; mpfr_rnd_t rnd_im; mpc_rnd_t rnd1; inex_re = 0; inex_im = 0; /* special values */ if (mpfr_nan_p (mpc_realref (op)) || mpfr_nan_p (mpc_imagref (op))) { if (mpfr_inf_p (mpc_realref (op)) || mpfr_inf_p (mpc_imagref (op))) { mpfr_set_inf (mpc_imagref (rop), mpfr_signbit (mpc_imagref (op)) ? +1 : -1); mpfr_set_nan (mpc_realref (rop)); } else if (mpfr_zero_p (mpc_realref (op))) { inex_re = set_pi_over_2 (mpc_realref (rop), +1, MPC_RND_RE (rnd)); mpfr_set_nan (mpc_imagref (rop)); } else { mpfr_set_nan (mpc_realref (rop)); mpfr_set_nan (mpc_imagref (rop)); } return MPC_INEX (inex_re, 0); } if (mpfr_inf_p (mpc_realref (op)) || mpfr_inf_p (mpc_imagref (op))) { if (mpfr_inf_p (mpc_realref (op))) { if (mpfr_inf_p (mpc_imagref (op))) { if (mpfr_sgn (mpc_realref (op)) > 0) { inex_re = set_pi_over_2 (mpc_realref (rop), +1, MPC_RND_RE (rnd)); mpfr_div_2ui (mpc_realref (rop), mpc_realref (rop), 1, GMP_RNDN); } else { /* the real part of the result is 3*pi/4 a = o(pi) error(a) < 1 ulp(a) b = o(3*a) error(b) < 2 ulp(b) c = b/4 exact thus 1 bit is lost */ mpfr_t x; mpfr_prec_t prec; int ok; mpfr_init (x); prec = mpfr_get_prec (mpc_realref (rop)); p = prec; do { p += mpc_ceil_log2 (p); mpfr_set_prec (x, p); mpfr_const_pi (x, GMP_RNDD); mpfr_mul_ui (x, x, 3, GMP_RNDD); ok = mpfr_can_round (x, p - 1, GMP_RNDD, MPC_RND_RE (rnd), prec+(MPC_RND_RE (rnd) == GMP_RNDN)); } while (ok == 0); inex_re = mpfr_div_2ui (mpc_realref (rop), x, 2, MPC_RND_RE (rnd)); mpfr_clear (x); } } else { if (mpfr_sgn (mpc_realref (op)) > 0) mpfr_set_ui (mpc_realref (rop), 0, GMP_RNDN); else inex_re = mpfr_const_pi (mpc_realref (rop), MPC_RND_RE (rnd)); } } else inex_re = set_pi_over_2 (mpc_realref (rop), +1, MPC_RND_RE (rnd)); mpfr_set_inf (mpc_imagref (rop), mpfr_signbit (mpc_imagref (op)) ? +1 : -1); return MPC_INEX (inex_re, 0); } /* pure real argument */ if (mpfr_zero_p (mpc_imagref (op))) { int s_im; s_im = mpfr_signbit (mpc_imagref (op)); if (mpfr_cmp_ui (mpc_realref (op), 1) > 0) { if (s_im) inex_im = mpfr_acosh (mpc_imagref (rop), mpc_realref (op), MPC_RND_IM (rnd)); else inex_im = -mpfr_acosh (mpc_imagref (rop), mpc_realref (op), INV_RND (MPC_RND_IM (rnd))); mpfr_set_ui (mpc_realref (rop), 0, GMP_RNDN); } else if (mpfr_cmp_si (mpc_realref (op), -1) < 0) { mpfr_t minus_op_re; minus_op_re[0] = mpc_realref (op)[0]; MPFR_CHANGE_SIGN (minus_op_re); if (s_im) inex_im = mpfr_acosh (mpc_imagref (rop), minus_op_re, MPC_RND_IM (rnd)); else inex_im = -mpfr_acosh (mpc_imagref (rop), minus_op_re, INV_RND (MPC_RND_IM (rnd))); inex_re = mpfr_const_pi (mpc_realref (rop), MPC_RND_RE (rnd)); } else { inex_re = mpfr_acos (mpc_realref (rop), mpc_realref (op), MPC_RND_RE (rnd)); mpfr_set_ui (mpc_imagref (rop), 0, MPC_RND_IM (rnd)); } if (!s_im) mpc_conj (rop, rop, MPC_RNDNN); return MPC_INEX (inex_re, inex_im); } /* pure imaginary argument */ if (mpfr_zero_p (mpc_realref (op))) { inex_re = set_pi_over_2 (mpc_realref (rop), +1, MPC_RND_RE (rnd)); inex_im = -mpfr_asinh (mpc_imagref (rop), mpc_imagref (op), INV_RND (MPC_RND_IM (rnd))); mpc_conj (rop,rop, MPC_RNDNN); return MPC_INEX (inex_re, inex_im); } /* regular complex argument: acos(z) = Pi/2 - asin(z) */ p_re = mpfr_get_prec (mpc_realref(rop)); p_im = mpfr_get_prec (mpc_imagref(rop)); p = p_re; mpc_init3 (z1, p, p_im); /* we round directly the imaginary part to p_im, with rounding mode opposite to rnd_im */ rnd_im = MPC_RND_IM(rnd); /* the imaginary part of asin(z) has the same sign as Im(z), thus if Im(z) > 0 and rnd_im = RNDZ, we want to round the Im(asin(z)) to -Inf so that -Im(asin(z)) is rounded to zero */ if (rnd_im == GMP_RNDZ) rnd_im = mpfr_sgn (mpc_imagref(op)) > 0 ? GMP_RNDD : GMP_RNDU; else rnd_im = rnd_im == GMP_RNDU ? GMP_RNDD : rnd_im == GMP_RNDD ? GMP_RNDU : rnd_im; /* both RNDZ and RNDA map to themselves for -asin(z) */ rnd1 = MPC_RND (GMP_RNDN, rnd_im); mpfr_init2 (pi_over_2, p); for (;;) { p += mpc_ceil_log2 (p) + 3; mpfr_set_prec (mpc_realref(z1), p); mpfr_set_prec (pi_over_2, p); set_pi_over_2 (pi_over_2, +1, GMP_RNDN); e1 = 1; /* Exp(pi_over_2) */ inex = mpc_asin (z1, op, rnd1); /* asin(z) */ MPC_ASSERT (mpfr_sgn (mpc_imagref(z1)) * mpfr_sgn (mpc_imagref(op)) > 0); inex_im = MPC_INEX_IM(inex); /* inex_im is in {-1, 0, 1} */ e2 = mpfr_get_exp (mpc_realref(z1)); mpfr_sub (mpc_realref(z1), pi_over_2, mpc_realref(z1), GMP_RNDN); if (!mpfr_zero_p (mpc_realref(z1))) { /* the error on x=Re(z1) is bounded by 1/2 ulp(x) + 2^(e1-p-1) + 2^(e2-p-1) */ e1 = e1 >= e2 ? e1 + 1 : e2 + 1; /* the error on x is bounded by 1/2 ulp(x) + 2^(e1-p-1) */ e1 -= mpfr_get_exp (mpc_realref(z1)); /* the error on x is bounded by 1/2 ulp(x) [1 + 2^e1] */ e1 = e1 <= 0 ? 0 : e1; /* the error on x is bounded by 2^e1 * ulp(x) */ mpfr_neg (mpc_imagref(z1), mpc_imagref(z1), GMP_RNDN); /* exact */ inex_im = -inex_im; if (mpfr_can_round (mpc_realref(z1), p - e1, GMP_RNDN, GMP_RNDZ, p_re + (MPC_RND_RE(rnd) == GMP_RNDN))) break; } } inex = mpc_set (rop, z1, rnd); inex_re = MPC_INEX_RE(inex); mpc_clear (z1); mpfr_clear (pi_over_2); return MPC_INEX(inex_re, inex_im); }
void bvisit(const ACsc &x) { apply(result_, *(x.get_arg())); mpfr_ui_div(result_, 1, result_, rnd_); mpfr_acos(result_, result_, rnd_); }
void bvisit(const ACos &x) { apply(result_, *(x.get_arg())); mpfr_acos(result_, result_, rnd_); }
CGAL::Gmpfr acos(const CGAL::Gmpfr &x) { CGAL::Gmpfr result(0, gmp_result_precision(x)); mpfr_acos(result.fr(), x.fr(), gmp_rounding_mode(CGAL::Gmpfr::get_default_rndmode())); return result; }