static void check_nans (void) { mpfr_t x, xi, xf; mpfr_init2 (x, 123); mpfr_init2 (xi, 123); mpfr_init2 (xf, 123); /* nan */ mpfr_set_nan (x); mpfr_modf (xi, xf, x, GMP_RNDN); MPFR_ASSERTN (mpfr_nan_p (xi)); MPFR_ASSERTN (mpfr_nan_p (xf)); /* +inf */ mpfr_set_inf (x, 1); mpfr_modf (xi, xf, x, GMP_RNDN); MPFR_ASSERTN (mpfr_inf_p (xi)); MPFR_ASSERTN (mpfr_sgn (xi) > 0); MPFR_ASSERTN (mpfr_zero_p (xf)); /* -inf */ mpfr_set_inf (x, -1); mpfr_modf (xi ,xf, x, GMP_RNDN); MPFR_ASSERTN (mpfr_inf_p (xi)); MPFR_ASSERTN (mpfr_sgn (xi) < 0); MPFR_ASSERTN (mpfr_zero_p (xf)); mpfr_clear (x); mpfr_clear (xi); mpfr_clear (xf); }
static void special (void) { mpfr_t x, y, z; mpfr_init (x); mpfr_init (y); mpfr_init (z); mpfr_set_nan (x); mpfr_hypot (z, x, y, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (z)); mpfr_set_inf (x, 1); mpfr_set_inf (y, -1); mpfr_hypot (z, x, y, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (z) && mpfr_sgn (z) > 0); mpfr_set_inf (x, -1); mpfr_set_nan (y); mpfr_hypot (z, x, y, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (z) && mpfr_sgn (z) > 0); mpfr_set_nan (x); mpfr_set_inf (y, -1); mpfr_hypot (z, x, y, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (z) && mpfr_sgn (z) > 0); mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
static void check_nans (void) { mpfr_t x, y; mpfr_init2 (x, 123L); mpfr_init2 (y, 123L); /* nan - 1 == nan */ mpfr_set_nan (x); mpfr_sub_ui (y, x, 1L, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (y)); /* +inf - 1 == +inf */ mpfr_set_inf (x, 1); mpfr_sub_ui (y, x, 1L, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (y)); MPFR_ASSERTN (mpfr_sgn (y) > 0); /* -inf - 1 == -inf */ mpfr_set_inf (x, -1); mpfr_sub_ui (y, x, 1L, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (y)); MPFR_ASSERTN (mpfr_sgn (y) < 0); mpfr_clear (x); mpfr_clear (y); }
static PyObject * GMPy_Real_Is_Infinite(PyObject *x, CTXT_Object *context) { MPFR_Object *tempx; int res; if (MPFR_Check(x)) { res = mpfr_inf_p(MPFR(x)); } else { CHECK_CONTEXT(context); if (!(tempx = GMPy_MPFR_From_Real(x, 1, context))) { return NULL; } res = mpfr_inf_p(tempx->f); Py_DECREF((PyObject*)tempx); } if (res) { Py_RETURN_TRUE; } else { Py_RETURN_FALSE; } }
static void check_nans (void) { mpfr_t x, sh, ch; mpfr_init2 (x, 123); mpfr_init2 (sh, 123); mpfr_init2 (ch, 123); /* nan */ mpfr_set_nan (x); mpfr_sinh_cosh (sh, ch, x, GMP_RNDN); MPFR_ASSERTN (mpfr_nan_p (sh)); MPFR_ASSERTN (mpfr_nan_p (ch)); /* +inf */ mpfr_set_inf (x, 1); mpfr_sinh_cosh (sh, ch, x, GMP_RNDN); MPFR_ASSERTN (mpfr_inf_p (sh)); MPFR_ASSERTN (mpfr_sgn (sh) > 0); MPFR_ASSERTN (mpfr_inf_p (ch)); MPFR_ASSERTN (mpfr_sgn (ch) > 0); /* -inf */ mpfr_set_inf (x, -1); mpfr_sinh_cosh (sh, ch, x, GMP_RNDN); MPFR_ASSERTN (mpfr_inf_p (sh)); MPFR_ASSERTN (mpfr_sgn (sh) < 0); MPFR_ASSERTN (mpfr_inf_p (ch)); MPFR_ASSERTN (mpfr_sgn (ch) > 0); mpfr_clear (x); mpfr_clear (sh); mpfr_clear (ch); }
static void special (void) { mpfr_t x; int i; mpfr_init (x); mpfr_set_nan (x); mpfr_sinh (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (x)); mpfr_set_inf (x, 1); mpfr_sinh (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); mpfr_set_inf (x, -1); mpfr_sinh (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) < 0); mpfr_set_prec (x, 10); mpfr_set_str_binary (x, "-0.1001011001"); mpfr_sinh (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_si_2exp (x, -159, -8) == 0); /* corner case */ mpfr_set_prec (x, 2); mpfr_set_str_binary (x, "1E-6"); mpfr_sinh (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui_2exp (x, 1, -6) == 0); mpfr_clear_flags (); mpfr_set_str_binary (x, "1E1000000000"); i = mpfr_sinh (x, x, MPFR_RNDN); MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_SIGN (x) > 0); MPFR_ASSERTN (mpfr_overflow_p ()); MPFR_ASSERTN (i == 1); mpfr_clear_flags (); mpfr_set_str_binary (x, "-1E1000000000"); i = mpfr_sinh (x, x, MPFR_RNDN); MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_SIGN (x) < 0); MPFR_ASSERTN (mpfr_overflow_p () && !mpfr_underflow_p ()); MPFR_ASSERTN (i == -1); mpfr_clear_flags (); mpfr_set_str_binary (x, "-1E1000000000"); i = mpfr_sinh (x, x, MPFR_RNDD); MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_SIGN (x) < 0); MPFR_ASSERTN (mpfr_overflow_p () && !mpfr_underflow_p ()); MPFR_ASSERTN (i == -1); mpfr_clear_flags (); mpfr_set_str_binary (x, "-1E1000000000"); i = mpfr_sinh (x, x, MPFR_RNDU); MPFR_ASSERTN (!MPFR_IS_INF (x) && MPFR_SIGN (x) < 0); MPFR_ASSERTN (mpfr_overflow_p () && !mpfr_underflow_p ()); MPFR_ASSERTN (i == 1); mpfr_clear (x); }
static void check_nan (void) { mpfr_t d, q; mpfr_init2 (d, 100L); mpfr_init2 (q, 100L); /* 1/+inf == 0 */ MPFR_CLEAR_FLAGS (d); MPFR_SET_INF (d); MPFR_SET_POS (d); MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_number_p (q)); MPFR_ASSERTN (mpfr_sgn (q) == 0); /* 1/-inf == -0 */ MPFR_CLEAR_FLAGS (d); MPFR_SET_INF (d); MPFR_SET_NEG (d); MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_number_p (q)); MPFR_ASSERTN (mpfr_sgn (q) == 0); /* 1/nan == nan */ MPFR_SET_NAN (d); MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_nan_p (q)); /* 0/0 == nan */ mpfr_set_ui (d, 0L, GMP_RNDN); MPFR_ASSERTN (mpfr_ui_div (q, 0L, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_nan_p (q)); /* 1/+0 = +inf */ mpfr_set_ui (d, 0L, GMP_RNDN); MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0); /* 1/-0 = -inf */ mpfr_set_ui (d, 0L, GMP_RNDN); mpfr_neg (d, d, GMP_RNDN); MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0); /* 0/1 = +0 */ mpfr_set_ui (d, 1L, GMP_RNDN); MPFR_ASSERTN (mpfr_ui_div (q, 0L, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_cmp_ui (q, 0) == 0 && MPFR_IS_POS (q)); /* 0/-1 = -0 */ mpfr_set_si (d, -1, GMP_RNDN); MPFR_ASSERTN (mpfr_ui_div (q, 0L, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_cmp_ui (q, 0) == 0 && MPFR_IS_NEG (q)); mpfr_clear (d); mpfr_clear (q); }
/* the rounding mode is mpfr_rnd_t here since we return an mpfr number */ int mpc_norm (mpfr_ptr a, mpc_srcptr b, mpfr_rnd_t rnd) { mpfr_t u, v; mp_prec_t prec; int inexact, overflow; prec = MPFR_PREC(a); /* handling of special values; consistent with abs in that norm = abs^2; so norm (+-inf, nan) = norm (nan, +-inf) = +inf */ if ( (mpfr_nan_p (MPC_RE (b)) || mpfr_nan_p (MPC_IM (b))) || (mpfr_inf_p (MPC_RE (b)) || mpfr_inf_p (MPC_IM (b)))) return mpc_abs (a, b, rnd); mpfr_init (u); mpfr_init (v); if (!mpfr_zero_p(MPC_RE(b)) && !mpfr_zero_p(MPC_IM(b)) && 2 * SAFE_ABS (mp_exp_t, MPFR_EXP (MPC_RE (b)) - MPFR_EXP (MPC_IM (b))) > (mp_exp_t)prec) /* If real and imaginary part have very different magnitudes, then the */ /* generic code increases the precision too much. Instead, compute the */ /* squarings _exactly_. */ { mpfr_set_prec (u, 2 * MPFR_PREC (MPC_RE (b))); mpfr_set_prec (v, 2 * MPFR_PREC (MPC_IM (b))); mpfr_sqr (u, MPC_RE (b), GMP_RNDN); mpfr_sqr (v, MPC_IM (b), GMP_RNDN); inexact = mpfr_add (a, u, v, rnd); } else { do { prec += mpc_ceil_log2 (prec) + 3; mpfr_set_prec (u, prec); mpfr_set_prec (v, prec); inexact = mpfr_sqr (u, MPC_RE(b), GMP_RNDN); /* err<=1/2ulp */ inexact |= mpfr_sqr (v, MPC_IM(b), GMP_RNDN); /* err<=1/2ulp*/ inexact |= mpfr_add (u, u, v, GMP_RNDN); /* err <= 3/2 ulps */ overflow = mpfr_inf_p (u); } while (!overflow && inexact && mpfr_can_round (u, prec - 2, GMP_RNDN, rnd, MPFR_PREC(a)) == 0); inexact |= mpfr_set (a, u, rnd); } mpfr_clear (u); mpfr_clear (v); return inexact; }
static void special (void) { mpfr_t x, y; int inex; mpfr_init (x); mpfr_init (y); /* rec_sqrt(NaN) = NaN */ mpfr_set_nan (x); inex = mpfr_rec_sqrt (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (x) && inex == 0); /* rec_sqrt(+Inf) = +0 */ mpfr_set_inf (x, 1); inex = mpfr_rec_sqrt (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_zero_p (x) && MPFR_IS_POS(x) && inex == 0); /* rec_sqrt(-Inf) = NaN */ mpfr_set_inf (x, -1); inex = mpfr_rec_sqrt (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (x) && inex == 0); /* rec_sqrt(+0) = +Inf */ mpfr_set_ui (x, 0, MPFR_RNDN); inex = mpfr_rec_sqrt (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (x) && MPFR_IS_POS(x) && inex == 0); /* rec_sqrt(-0) = +Inf */ mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); inex = mpfr_rec_sqrt (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (x) && MPFR_IS_POS(x) && inex == 0); /* rec_sqrt(-1) = NaN */ mpfr_set_si (x, -1, MPFR_RNDN); inex = mpfr_rec_sqrt (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (x) && inex == 0); /* rec_sqrt(1) = 1 */ mpfr_set_ui (x, 1, MPFR_RNDN); inex = mpfr_rec_sqrt (x, x, MPFR_RNDN); MPFR_ASSERTN((mpfr_cmp_ui (x, 1) == 0) && (inex == 0)); mpfr_set_prec (x, 23); mpfr_set_prec (y, 33); mpfr_set_str_binary (x, "1.0001110110101001010100e-1"); inex = mpfr_rec_sqrt (y, x, MPFR_RNDU); mpfr_set_prec (x, 33); mpfr_set_str_binary (x, "1.01010110101110100100100101011"); MPFR_ASSERTN (inex > 0 && mpfr_cmp (x, y) == 0); mpfr_clear (x); mpfr_clear (y); }
static void check_overflow () { mpfr_t max; mpfi_t a; mpfr_t b; int inexact; mpfi_init2 (a, 53); mpfr_init2 (max, 53); mpfr_init2 (b, 53); mpfr_set_ui (&(a->left), 1, MPFI_RNDD); mpfr_set_inf (max, +1); mpfr_nextbelow (max); mpfr_set (&(a->right), max, MPFI_RNDU); mpfr_set_ui (b, +1, MPFI_RNDD); inexact = mpfi_add_fr (a, a, b); if (!mpfr_inf_p (&(a->right))) { printf ("Error: mpfi_add_fr does not correctly handle positive " "overflow.\n"); exit (1); } if (!MPFI_RIGHT_IS_INEXACT (inexact)) { printf ("Error: mpfi_add_fr does not return correct value when positive " "overflow.\n"); exit (1); } mpfr_set_inf (max, -1); mpfr_nextabove (max); mpfr_set (&(a->left), max, MPFI_RNDD); mpfr_set_ui (&(a->right), 1, MPFI_RNDU); mpfr_set_si (b, -1, MPFI_RNDD); inexact = mpfi_add_fr (a, a, b); if (!mpfr_inf_p (&(a->left))) { printf ("Error: mpfi_add_fr does not correctly handle negative " "overflow.\n"); exit (1); } if (!MPFI_LEFT_IS_INEXACT (inexact)) { printf ("Error: mpfi_add_fr does not return correct value when negative " "overflow.\n"); exit (1); } mpfi_clear (a); mpfr_clear (b); mpfr_clear (max); }
static void test_special (void) { mpfr_t x, y; int inex; mpfr_init2 (x, MPFR_PREC_MIN); mpfr_init2 (y, MPFR_PREC_MIN); mpfr_set_nan (x); inex = mpfr_round_nearest_away (mpfr_sin, y, x); if (inex != 0) { printf ("Wrong ternary value for sin(NaN)\n"); exit (1); } if (mpfr_nan_p (y) == 0) { printf ("Wrong output for sin(NaN)\n"); exit (1); } mpfr_set_inf (x, 1); inex = mpfr_round_nearest_away (mpfr_exp, y, x); if (inex != 0) { printf ("Wrong ternary value for exp(+Inf)\n"); printf ("expected 0, got %d\n", inex); exit (1); } if (mpfr_inf_p (y) == 0 || mpfr_sgn (y) <= 0) { printf ("Wrong output for exp(+Inf)\n"); exit (1); } mpfr_set_inf (x, -1); inex = mpfr_round_nearest_away (mpfr_cbrt, y, x); if (inex != 0) { printf ("Wrong ternary value for cbrt(-Inf)\n"); exit (1); } if (mpfr_inf_p (y) == 0 || mpfr_sgn (y) >= 0) { printf ("Wrong output for cbrt(-Inf)\n"); exit (1); } mpfr_clear (x); mpfr_clear (y); }
static void special_overflow (void) { /* Check for overflow in 3 cases: 1. cosh(x) is representable, but not exp(x) 2. cosh(x) is not representable in the selected range of exp. 3. cosh(x) exp overflow even with the largest range of exp */ mpfr_t x, y; mp_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, 24); mpfr_set_str_binary (x, "0.101100100000000000110100E7"); mpfr_cosh (y, x, GMP_RNDN); if (mpfr_cmp_str (y, "0.101010001111001010001110E128", 2, GMP_RNDN)) { printf("Special overflow error 1.\n"); mpfr_dump (y); exit (1); } mpfr_set_str_binary (x, "0.101100100000000000110100E8"); mpfr_cosh (y, x, GMP_RNDN); if (!mpfr_inf_p(y)) { printf("Special overflow error 2.\n"); mpfr_dump (y); exit (1); } set_emin (emin); set_emax (emax); mpfr_set_str_binary (x, "0.101100100000000000110100E1000000"); mpfr_cosh (y, x, GMP_RNDN); if (!mpfr_inf_p(y)) { printf("Special overflow error 3.\n"); mpfr_dump (y); exit (1); } mpfr_clear (y); mpfr_clear (x); }
static void emax_m_eps (void) { if (mpfr_get_emax () <= LONG_MAX) { mpfr_t x, y; int inex, ov; mpfr_init2 (x, sizeof(mpfr_exp_t) * CHAR_BIT * 4); mpfr_init2 (y, 8); mpfr_set_si (x, mpfr_get_emax (), MPFR_RNDN); mpfr_clear_flags (); inex = mpfr_exp2 (y, x, MPFR_RNDN); ov = mpfr_overflow_p (); if (!ov || !mpfr_inf_p (y) || inex <= 0) { printf ("Overflow error for x = emax, MPFR_RNDN.\n"); mpfr_dump (y); printf ("inex = %d, %soverflow\n", inex, ov ? "" : "no "); exit (1); } mpfr_nextbelow (x); mpfr_clear_flags (); inex = mpfr_exp2 (y, x, MPFR_RNDN); ov = mpfr_overflow_p (); if (!ov || !mpfr_inf_p (y) || inex <= 0) { printf ("Overflow error for x = emax - eps, MPFR_RNDN.\n"); mpfr_dump (y); printf ("inex = %d, %soverflow\n", inex, ov ? "" : "no "); exit (1); } mpfr_clear_flags (); inex = mpfr_exp2 (y, x, MPFR_RNDD); ov = mpfr_overflow_p (); if (ov || mpfr_inf_p (y) || inex >= 0 || (mpfr_nextabove (y), !mpfr_inf_p (y))) { printf ("Overflow error for x = emax - eps, MPFR_RNDD.\n"); mpfr_dump (y); printf ("inex = %d, %soverflow\n", inex, ov ? "" : "no "); exit (1); } mpfr_clear (x); mpfr_clear (y); } }
void check_overflow () { mpfr_t max; mpfi_t a; mpq_t q; int inexact; mpq_init (q); mpfi_init2 (a, 53); mpfr_init2 (max, 53); mpq_set_ui (q, 42, 17); mpfr_set_ui (&(a->left), 1, MPFI_RNDD); mpfr_set_inf (max, +1); mpfr_nextbelow (max); mpfr_set (&(a->right), max, MPFI_RNDU); inexact = mpfi_add_q (a, a, q); if (!mpfr_inf_p (&(a->right))) { printf ("Error: mpfi_add_q does not correctly handle overflow.\n"); exit (1); } if (!MPFI_RIGHT_IS_INEXACT (inexact)) { printf ("Error: mpfi_add_q does not return correct value " "when overflow.\n"); exit (1); } mpfr_set_inf (max, -1); mpfr_nextabove (max); mpfr_set (&(a->left), max, MPFI_RNDD); mpfr_set_ui (&(a->right), 1, MPFI_RNDU); mpq_set_si (q, -42, 17); inexact = mpfi_add_q (a, a, q); if (!mpfr_inf_p (&(a->left))) { printf ("Error: mpfi_add_q does not correctly handle negative " "overflow.\n"); exit (1); } if (!MPFI_LEFT_IS_INEXACT (inexact)) { printf ("Error: mpfi_add_q does not return correct value when negative " "overflow.\n"); exit (1); } mpfi_clear (a); mpfr_clear (max); mpq_clear (q); }
static void check_specials (void) { mpfr_t x, y; mpfr_init2 (x, 123L); mpfr_init2 (y, 123L); mpfr_set_nan (x); mpfr_coth (y, x, MPFR_RNDN); if (! mpfr_nan_p (y)) { printf ("Error: coth(NaN) != NaN\n"); exit (1); } mpfr_set_inf (x, 1); mpfr_coth (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1)) { printf ("Error: coth(Inf) != 1\n"); exit (1); } mpfr_set_inf (x, -1); mpfr_coth (y, x, MPFR_RNDN); if (mpfr_cmp_si (y, -1)) { printf ("Error: coth(-Inf) != -1\n"); exit (1); } /* coth(+/-0) = +/-Inf */ mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_coth (y, x, MPFR_RNDN); if (! (mpfr_inf_p (y) && MPFR_SIGN (y) > 0)) { printf ("Error: coth(+0) != +Inf\n"); exit (1); } mpfr_neg (x, x, MPFR_RNDN); mpfr_coth (y, x, MPFR_RNDN); if (! (mpfr_inf_p (y) && MPFR_SIGN (y) < 0)) { printf ("Error: coth(-0) != -Inf\n"); exit (1); } mpfr_clear (x); mpfr_clear (y); }
static void check_special (void) { mpfr_t x, y; int inexact; mpfr_init (x); mpfr_init (y); mpfr_set_inf (x, 1); PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) < 0, "ERROR: mpfr_set_inf failed to set variable to +infinity.\n"); inexact = mpfr_set (y, x, MPFR_RNDN); PRINT_ERROR_IF (!mpfr_inf_p (y) || mpfr_sgn (y) < 0 || inexact != 0, "ERROR: mpfr_set failed to set variable to +infinity.\n"); inexact = mpfr_set_ui (y, 0, MPFR_RNDN); PRINT_ERROR_IF (!mpfr_zero_p (y) || mpfr_sgn (y) < 0 || inexact != 0, "ERROR: mpfr_set_ui failed to set variable to +0.\n"); mpfr_set_inf (x, -1); PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) > 0, "ERROR: mpfr_set_inf failed to set variable to -infinity.\n"); inexact = mpfr_set (y, x, MPFR_RNDN); PRINT_ERROR_IF (!mpfr_inf_p (y) || mpfr_sgn (y) > 0 || inexact != 0, "ERROR: mpfr_set failed to set variable to -infinity.\n"); mpfr_set_zero (x, 1); PRINT_ERROR_IF (!mpfr_zero_p (x) || mpfr_sgn (x) < 0, "ERROR: mpfr_set_zero failed to set variable to +0.\n"); inexact = mpfr_set (y, x, MPFR_RNDN); PRINT_ERROR_IF (!mpfr_zero_p (y) || mpfr_sgn (y) < 0 || inexact != 0, "ERROR: mpfr_set failed to set variable to +0.\n"); mpfr_set_zero (x, -1); PRINT_ERROR_IF (!mpfr_zero_p (x) || mpfr_sgn (x) > 0, "ERROR: mpfr_set_zero failed to set variable to -0.\n"); inexact = mpfr_set (y, x, MPFR_RNDN); PRINT_ERROR_IF (!mpfr_zero_p (y) || mpfr_sgn (y) > 0 || inexact != 0, "ERROR: mpfr_set failed to set variable to -0.\n"); mpfr_set_nan (x); PRINT_ERROR_IF (!mpfr_nan_p (x), "ERROR: mpfr_set_nan failed to set variable to NaN.\n"); inexact = mpfr_set (y, x, MPFR_RNDN); PRINT_ERROR_IF (!mpfr_nan_p (y) || inexact != 0, "ERROR: mpfr_set failed to set variable to NaN.\n"); mpfr_clear (x); mpfr_clear (y); }
static void special (void) { mpfr_t x; int inex; mpfr_init (x); mpfr_set_nan (x); mpfr_clear_flags (); inex = test_log1p (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0); MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN); mpfr_set_inf (x, -1); mpfr_clear_flags (); inex = test_log1p (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0); MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN); mpfr_set_inf (x, 1); mpfr_clear_flags (); inex = test_log1p (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) > 0 && inex == 0); MPFR_ASSERTN (__gmpfr_flags == 0); mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_clear_flags (); inex = test_log1p (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS (x) && inex == 0); MPFR_ASSERTN (__gmpfr_flags == 0); mpfr_neg (x, x, MPFR_RNDN); mpfr_clear_flags (); inex = test_log1p (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_NEG (x) && inex == 0); MPFR_ASSERTN (__gmpfr_flags == 0); mpfr_set_si (x, -1, MPFR_RNDN); mpfr_clear_flags (); inex = test_log1p (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) < 0 && inex == 0); MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0); mpfr_set_si (x, -2, MPFR_RNDN); mpfr_clear_flags (); inex = test_log1p (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0); MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN); mpfr_clear (x); }
static void check_large (void) { mpz_t z; mpfr_t x, y; mpfr_exp_t emax, emin; mpz_init (z); mpfr_init2 (x, 160); mpfr_init2 (y, 160); mpz_set_str (z, "77031627725494291259359895954016675357279104942148788042", 10); mpfr_set_z (x, z, MPFR_RNDN); mpfr_set_str_binary (y, "0.1100100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000000011011100000111001101000100101001000000100100111000001001E186"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_z on large input\n"); exit (1); } /* check overflow */ emax = mpfr_get_emax (); set_emax (2); mpz_set_str (z, "7", 10); mpfr_set_z (x, z, MPFR_RNDU); MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); set_emax (3); mpfr_set_prec (x, 2); mpz_set_str (z, "7", 10); mpfr_set_z (x, z, MPFR_RNDU); MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); set_emax (emax); /* check underflow */ emin = mpfr_get_emin (); set_emin (3); mpz_set_str (z, "1", 10); mpfr_set_z (x, z, MPFR_RNDZ); MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x)); set_emin (2); mpfr_set_z (x, z, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x)); set_emin (emin); mpz_clear (z); mpfr_clear (x); mpfr_clear (y); }
/* comparisons, see description in mpc-tests.h */ int same_mpfr_value (mpfr_ptr got, mpfr_ptr ref, int known_sign) { /* The sign of zeroes and infinities is checked only when known_sign is true. */ if (mpfr_nan_p (got)) return mpfr_nan_p (ref); if (mpfr_inf_p (got)) return mpfr_inf_p (ref) && (!known_sign || mpfr_signbit (got) == mpfr_signbit (ref)); if (mpfr_zero_p (got)) return mpfr_zero_p (ref) && (!known_sign || mpfr_signbit (got) == mpfr_signbit (ref)); return mpfr_cmp (got, ref) == 0; }
int tpl_same_mpfr_value (mpfr_ptr x1, mpfr_ptr x2, int known_sign) { /* The sign of zeroes and infinities is checked only when known_sign is true. */ if (mpfr_nan_p (x1)) return mpfr_nan_p (x2); if (mpfr_inf_p (x1)) return mpfr_inf_p (x2) && (!known_sign || mpfr_signbit (x1) == mpfr_signbit (x2)); if (mpfr_zero_p (x1)) return mpfr_zero_p (x2) && (!known_sign || mpfr_signbit (x1) == mpfr_signbit (x2)); return mpfr_cmp (x1, x2) == 0; }
static void special_overflow (void) { mpfr_t x, y; int inex; 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, 24); mpfr_set_str_binary (x, "0.101100100000000000110100E15"); inex = mpfr_exp2 (y, x, MPFR_RNDN); if (!mpfr_inf_p (y) || inex <= 0) { printf ("Overflow error.\n"); mpfr_dump (y); printf ("inex = %d\n", inex); exit (1); } mpfr_clear (y); mpfr_clear (x); set_emin (emin); set_emax (emax); }
static void test_nan_inf_zero (void) { mpfr_ptr val; int sign; int kind; reset_stack (); val = new_mpfr (MPFR_PREC_MIN); mpfr_set_nan (val); kind = (mpfr_custom_get_kind) (val); if (kind != MPFR_NAN_KIND) { printf ("mpfr_custom_get_kind error: "); mpfr_dump (val); printf (" is kind %d instead of %d\n", kind, (int) MPFR_NAN_KIND); exit (1); } val = new_nan (MPFR_PREC_MIN); if (!mpfr_nan_p(val)) { printf ("Error: mpfr_custom_init_set doesn't set NAN mpfr.\n"); exit (1); } val = new_inf (MPFR_PREC_MIN); if (!mpfr_inf_p(val) || mpfr_sgn(val) >= 0) { printf ("Error: mpfr_custom_init_set doesn't set -INF mpfr.\n"); exit (1); } sign = 1; mpfr_set_inf (val, sign); kind = (mpfr_custom_get_kind) (val); if ((ABS (kind) != MPFR_INF_KIND) || (SIGN (kind) != SIGN (sign))) { printf ("mpfr_custom_get_kind error: "); mpfr_dump (val); printf (" is kind %d instead of %d\n", kind, (int) MPFR_INF_KIND); printf (" have sign %d instead of %d\n", SIGN (kind), SIGN (sign)); exit (1); } sign = -1; mpfr_set_zero (val, sign); kind = (mpfr_custom_get_kind) (val); if ((ABS (kind) != MPFR_ZERO_KIND) || (SIGN (kind) != SIGN (sign))) { printf ("mpfr_custom_get_kind error: "); mpfr_dump (val); printf (" is kind %d instead of %d\n", kind, (int) MPFR_ZERO_KIND); printf (" have sign %d instead of %d\n", SIGN (kind), SIGN (sign)); exit (1); } reset_stack (); }
static void check_nans (void) { mpfr_t s, x, y; mpfr_init2 (x, 8L); mpfr_init2 (y, 8L); mpfr_init2 (s, 8L); /* +inf + -inf == nan */ mpfr_set_inf (x, 1); mpfr_set_inf (y, -1); test_add (s, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (s)); /* +inf + 1 == +inf */ mpfr_set_inf (x, 1); mpfr_set_ui (y, 1L, MPFR_RNDN); test_add (s, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (s)); MPFR_ASSERTN (mpfr_sgn (s) > 0); /* -inf + 1 == -inf */ mpfr_set_inf (x, -1); mpfr_set_ui (y, 1L, MPFR_RNDN); test_add (s, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (s)); MPFR_ASSERTN (mpfr_sgn (s) < 0); /* 1 + +inf == +inf */ mpfr_set_ui (x, 1L, MPFR_RNDN); mpfr_set_inf (y, 1); test_add (s, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (s)); MPFR_ASSERTN (mpfr_sgn (s) > 0); /* 1 + -inf == -inf */ mpfr_set_ui (x, 1L, MPFR_RNDN); mpfr_set_inf (y, -1); test_add (s, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (s)); MPFR_ASSERTN (mpfr_sgn (s) < 0); mpfr_clear (x); mpfr_clear (y); mpfr_clear (s); }
static void check_nans (void) { mpfr_t x, y; int inexact; mpfr_init2 (x, 123); mpfr_init2 (y, 123); /* nan / 1.0 is nan */ mpfr_set_nan (x); mpfr_clear_flags (); inexact = mpfr_div_d (y, x, 1.0, GMP_RNDN); MPFR_ASSERTN (inexact == 0); MPFR_ASSERTN ((__gmpfr_flags ^ MPFR_FLAGS_NAN) == 0); MPFR_ASSERTN (mpfr_nan_p (y)); /* +inf / 1.0 == +inf */ mpfr_set_inf (x, 1); mpfr_clear_flags (); inexact = mpfr_div_d (y, x, 1.0, GMP_RNDN); MPFR_ASSERTN (inexact == 0); MPFR_ASSERTN (__gmpfr_flags == 0); MPFR_ASSERTN (mpfr_inf_p (y)); MPFR_ASSERTN (MPFR_IS_POS (y)); /* -inf / 1.0 == -inf */ mpfr_set_inf (x, -1); mpfr_clear_flags (); inexact = mpfr_div_d (y, x, 1.0, GMP_RNDN); MPFR_ASSERTN (inexact == 0); MPFR_ASSERTN (__gmpfr_flags == 0); MPFR_ASSERTN (mpfr_inf_p (y)); MPFR_ASSERTN (MPFR_IS_NEG (y)); /* 0.0 / 0.0 is nan */ mpfr_set_d (x, 0.0, GMP_RNDN); mpfr_clear_flags (); inexact = mpfr_div_d (y, x, 0.0, GMP_RNDN); MPFR_ASSERTN (inexact == 0); MPFR_ASSERTN ((__gmpfr_flags ^ MPFR_FLAGS_NAN) == 0); MPFR_ASSERTN (mpfr_nan_p (y)); mpfr_clear (x); mpfr_clear (y); }
void print_fp (FILE *fout, mpfr_t f, const char *suffix) { if (mpfr_inf_p (f)) mpfr_fprintf (fout, "\t%sINF%s", mpfr_signbit (f) ? "-" : "", suffix); else mpfr_fprintf (fout, "\t%Ra%s", f, suffix); }
static void check_nans (void) { mpfr_t p, x, y; mpfr_init2 (x, 123L); mpfr_init2 (y, 123L); mpfr_init2 (p, 123L); /* nan * 0 == nan */ mpfr_set_nan (x); mpfr_set_ui (y, 0L, MPFR_RNDN); test_mul (p, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (p)); /* 1 * nan == nan */ mpfr_set_ui (x, 1L, MPFR_RNDN); mpfr_set_nan (y); test_mul (p, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (p)); /* 0 * +inf == nan */ mpfr_set_ui (x, 0L, MPFR_RNDN); mpfr_set_nan (y); test_mul (p, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (p)); /* +1 * +inf == +inf */ mpfr_set_ui (x, 1L, MPFR_RNDN); mpfr_set_inf (y, 1); test_mul (p, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (p)); MPFR_ASSERTN (mpfr_sgn (p) > 0); /* -1 * +inf == -inf */ mpfr_set_si (x, -1L, MPFR_RNDN); mpfr_set_inf (y, 1); test_mul (p, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (p)); MPFR_ASSERTN (mpfr_sgn (p) < 0); mpfr_clear (x); mpfr_clear (y); mpfr_clear (p); }
int main (int argc, char *argv[]) { mpfr_t x, y; unsigned int n; tests_start_mpfr (); test_generic (2, 100, 20); mpfr_init2 (x, 53); mpfr_init2 (y, 53); /* check NaN */ mpfr_set_nan (x); mpfr_log10 (y, x, GMP_RNDN); MPFR_ASSERTN(mpfr_nan_p (y)); /* check Inf */ mpfr_set_inf (x, -1); mpfr_log10 (y, x, GMP_RNDN); MPFR_ASSERTN(mpfr_nan_p (y)); mpfr_set_inf (x, 1); mpfr_log10 (y, x, GMP_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && mpfr_sgn (y) > 0); /* check negative argument */ mpfr_set_si (x, -1, GMP_RNDN); mpfr_log10 (y, x, GMP_RNDN); MPFR_ASSERTN(mpfr_nan_p (y)); /* check log10(1) = 0 */ mpfr_set_ui (x, 1, GMP_RNDN); mpfr_log10 (y, x, GMP_RNDN); MPFR_ASSERTN((mpfr_cmp_ui (y, 0) == 0) && (MPFR_IS_POS (y))); /* check log10(10^n)=n */ mpfr_set_ui (x, 1, GMP_RNDN); for (n = 1; n <= 15; n++) { mpfr_mul_ui (x, x, 10, GMP_RNDN); /* x = 10^n */ mpfr_log10 (y, x, GMP_RNDN); if (mpfr_cmp_ui (y, n) ) { printf ("log10(10^n) <> n for n=%u\n", n); exit (1); } } mpfr_clear (x); mpfr_clear (y); tests_end_mpfr (); return 0; }
/* this routine deals with the case where z if finite and w infinite */ static int mpc_div_fin_inf (mpc_ptr rop, mpc_srcptr z, mpc_srcptr w) /* Assumes z finite and w infinite; implementation according to C99 G.5.1.8 */ { mpfr_t c, d, a, b, x, y, zero; mpfr_init2 (c, 2); /* needed to hold a signed zero, +1 or -1 */ mpfr_init2 (d, 2); mpfr_init2 (x, 2); mpfr_init2 (y, 2); mpfr_init2 (zero, 2); mpfr_set_ui (zero, 0ul, MPFR_RNDN); mpfr_init2 (a, mpfr_get_prec (mpc_realref (z))); mpfr_init2 (b, mpfr_get_prec (mpc_imagref (z))); mpfr_set_ui (c, (mpfr_inf_p (mpc_realref (w)) ? 1 : 0), MPFR_RNDN); MPFR_COPYSIGN (c, c, mpc_realref (w), MPFR_RNDN); mpfr_set_ui (d, (mpfr_inf_p (mpc_imagref (w)) ? 1 : 0), MPFR_RNDN); MPFR_COPYSIGN (d, d, mpc_imagref (w), MPFR_RNDN); mpfr_mul (a, mpc_realref (z), c, MPFR_RNDN); /* exact */ mpfr_mul (b, mpc_imagref (z), d, MPFR_RNDN); mpfr_add (x, a, b, MPFR_RNDN); mpfr_mul (b, mpc_imagref (z), c, MPFR_RNDN); mpfr_mul (a, mpc_realref (z), d, MPFR_RNDN); mpfr_sub (y, b, a, MPFR_RNDN); MPFR_COPYSIGN (mpc_realref (rop), zero, x, MPFR_RNDN); MPFR_COPYSIGN (mpc_imagref (rop), zero, y, MPFR_RNDN); mpfr_clear (c); mpfr_clear (d); mpfr_clear (x); mpfr_clear (y); mpfr_clear (zero); mpfr_clear (a); mpfr_clear (b); return MPC_INEX (0, 0); /* exact */ }
static void check_nans (void) { #if !defined(MPFR_ERRDIVZERO) mpfr_t x, y; int inexact; mpfr_init2 (x, 123); mpfr_init2 (y, 123); /* nan + 1.0 is nan */ mpfr_set_nan (x); mpfr_clear_flags (); inexact = mpfr_add_d (y, x, 1.0, MPFR_RNDN); MPFR_ASSERTN (inexact == 0); MPFR_ASSERTN ((__gmpfr_flags ^ MPFR_FLAGS_NAN) == 0); MPFR_ASSERTN (mpfr_nan_p (y)); /* +inf + 1.0 == +inf */ mpfr_set_inf (x, 1); mpfr_clear_flags (); inexact = mpfr_add_d (y, x, 1.0, MPFR_RNDN); MPFR_ASSERTN (inexact == 0); MPFR_ASSERTN (__gmpfr_flags == 0); MPFR_ASSERTN (mpfr_inf_p (y)); MPFR_ASSERTN (MPFR_IS_POS (y)); /* -inf + 1.0 == -inf */ mpfr_set_inf (x, -1); mpfr_clear_flags (); inexact = mpfr_add_d (y, x, 1.0, MPFR_RNDN); MPFR_ASSERTN (inexact == 0); MPFR_ASSERTN (__gmpfr_flags == 0); MPFR_ASSERTN (mpfr_inf_p (y)); MPFR_ASSERTN (MPFR_IS_NEG (y)); mpfr_clear (x); mpfr_clear (y); #endif }
void check_pow_ui (void) { mpfr_t a, b; mpfr_init2 (a, 53); mpfr_init2 (b, 53); /* check in-place operations */ mpfr_set_d (b, 0.6926773, GMP_RNDN); mpfr_pow_ui (a, b, 10, GMP_RNDN); mpfr_pow_ui (b, b, 10, GMP_RNDN); if (mpfr_cmp (a, b)) { fprintf (stderr, "Error for mpfr_pow_ui (b, b, ...)\n"); exit (1); } /* check large exponents */ mpfr_set_d (b, 1, GMP_RNDN); mpfr_pow_ui (a, b, 4294967295UL, GMP_RNDN); mpfr_set_inf (a, -1); mpfr_pow_ui (a, a, 4049053855UL, GMP_RNDN); if (!mpfr_inf_p (a) || (mpfr_sgn (a) >= 0)) { fprintf (stderr, "Error for (-Inf)^4049053855\n"); exit (1); } mpfr_set_inf (a, -1); mpfr_pow_ui (a, a, (unsigned long) 30002752, GMP_RNDN); if (!mpfr_inf_p (a) || (mpfr_sgn (a) <= 0)) { fprintf (stderr, "Error for (-Inf)^30002752\n"); exit (1); } mpfr_clear (a); mpfr_clear (b); }