static void special (void) { mpfr_t x; mpfr_init (x); mpfr_set_nan (x); mpfr_tanh (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (x)); mpfr_set_inf (x, 1); mpfr_tanh (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (x, 1) == 0); mpfr_set_inf (x, -1); mpfr_tanh (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_si (x, -1) == 0); mpfr_set_prec (x, 10); mpfr_set_str_binary (x, "-0.1001011001"); mpfr_tanh (x, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_si_2exp (x, -135, -8) == 0); mpfr_clear (x); }
static void check_nans (void) { mpfr_t x, y; mpfr_init2 (x, 123L); mpfr_init2 (y, 123L); /* nan + 2394875 == nan */ mpfr_set_nan (x); mpfr_add_ui (y, x, 2394875L, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (y)); /* +inf + 2394875 == +inf */ mpfr_set_inf (x, 1); mpfr_add_ui (y, x, 2394875L, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (y)); MPFR_ASSERTN (mpfr_sgn (y) > 0); /* -inf + 2394875 == -inf */ mpfr_set_inf (x, -1); mpfr_add_ui (y, x, 2394875L, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (y)); MPFR_ASSERTN (mpfr_sgn (y) < 0); mpfr_clear (x); mpfr_clear (y); }
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 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 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); }
int isNaN(Real real){ if (no_reals) return 0; #ifdef USE_MPFR return mpfr_nan_p(real->mpfr_val); #else return mpf_nan_p(real->mpf_val); #endif }
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); }
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; tests_start_mpfr (); mpfr_init (x); mpfr_init (y); /* special values */ mpfr_set_nan (x); mpfr_j1 (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (y)); mpfr_set_inf (x, 1); /* +Inf */ mpfr_j1 (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y)); mpfr_set_inf (x, -1); /* -Inf */ mpfr_j1 (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y)); mpfr_set_ui (x, 0, MPFR_RNDN); /* +0 */ mpfr_j1 (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y)); /* j1(+0)=+0 */ mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); /* -0 */ mpfr_j1 (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_NEG (y)); /* j1(-0)=-0 */ mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_j1 (y, x, MPFR_RNDN); mpfr_set_str_binary (x, "0.0111000010100111001001111011101001011100001100011011"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_j1 for x=1, rnd=MPFR_RNDN\n"); printf ("Expected "); mpfr_dump (x); printf ("Got "); mpfr_dump (y); exit (1); } mpfr_clear (x); mpfr_clear (y); test_generic (2, 100, 10); data_check ("data/j1", mpfr_j1, "mpfr_j1"); tests_end_mpfr (); return 0; }
int mpfi_is_inside_fr (mpfr_srcptr a, mpfi_srcptr b) { /* Returns 0 if one of the operands is a NaN */ if (mpfr_nan_p (a) || MPFI_NAN_P (b)) return 0; return mpfr_cmp (a, &(b->left)) >= 0 && mpfr_cmp (a, &(b->right)) <= 0; }
int mpc_acosh (mpc_ptr rop, mpc_srcptr op, mpc_rnd_t rnd) { /* acosh(z) = NaN + i*NaN, if z=0+i*NaN -i*acos(z), if sign(Im(z)) = - i*acos(z), if sign(Im(z)) = + http://functions.wolfram.com/ElementaryFunctions/ArcCosh/27/02/03/01/01/ */ mpc_t a; mpfr_t tmp; int inex; if (mpfr_zero_p (MPC_RE (op)) && mpfr_nan_p (MPC_IM (op))) { mpfr_set_nan (MPC_RE (rop)); mpfr_set_nan (MPC_IM (rop)); return 0; } /* Note reversal of precisions due to later multiplication by i or -i */ mpc_init3 (a, MPC_PREC_IM(rop), MPC_PREC_RE(rop)); if (mpfr_signbit (MPC_IM (op))) { inex = mpc_acos (a, op, RNDC (INV_RND (MPC_RND_IM (rnd)), MPC_RND_RE (rnd))); /* change a to -i*a, i.e., -y+i*x to x+i*y */ tmp[0] = MPC_RE (a)[0]; MPC_RE (a)[0] = MPC_IM (a)[0]; MPC_IM (a)[0] = tmp[0]; MPFR_CHANGE_SIGN (MPC_IM (a)); inex = MPC_INEX (MPC_INEX_IM (inex), -MPC_INEX_RE (inex)); } else { inex = mpc_acos (a, op, RNDC (MPC_RND_IM (rnd), INV_RND(MPC_RND_RE (rnd)))); /* change a to i*a, i.e., y-i*x to x+i*y */ tmp[0] = MPC_RE (a)[0]; MPC_RE (a)[0] = MPC_IM (a)[0]; MPC_IM (a)[0] = tmp[0]; MPFR_CHANGE_SIGN (MPC_RE (a)); inex = MPC_INEX (-MPC_INEX_IM (inex), MPC_INEX_RE (inex)); } mpc_set (rop, a, rnd); mpc_clear (a); return inex; }
static void check_nan (void) { mpfr_t x, got; mpfr_init2 (x, 100L); mpfr_init2 (got, 100L); /* sqrt(NaN) == NaN */ MPFR_SET_NAN (x); MPFR_ASSERTN (test_sqrt (got, x, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_nan_p (got)); /* sqrt(-1) == NaN */ mpfr_set_si (x, -1L, MPFR_RNDZ); MPFR_ASSERTN (test_sqrt (got, x, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_nan_p (got)); /* sqrt(+inf) == +inf */ MPFR_SET_INF (x); MPFR_SET_POS (x); MPFR_ASSERTN (test_sqrt (got, x, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (got)); /* sqrt(-inf) == NaN */ MPFR_SET_INF (x); MPFR_SET_NEG (x); MPFR_ASSERTN (test_sqrt (got, x, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_nan_p (got)); /* sqrt(-0) == 0 */ mpfr_set_si (x, 0L, MPFR_RNDZ); MPFR_SET_NEG (x); MPFR_ASSERTN (test_sqrt (got, x, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_number_p (got)); MPFR_ASSERTN (mpfr_cmp_ui (got, 0L) == 0); mpfr_clear (x); mpfr_clear (got); }
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); }
std::string real::get_string(int p) const { std::string string; char *raw_string; mpfr_exp_t exp; if (mpfr_nan_p(r) != 0) { string = "nan"; } else if (mpfr_inf_p(r) != 0) { if (mpfr_sgn(r) < 0) { string = "-inf"; } else { string = "inf"; } } else { // Dynamically allocate a character array to hold the base-10 // representation of the real. raw_string = new char[(int)mpfr_get_default_prec()]; mpfr_get_str(raw_string, &exp, 10, p, r, MPFR_RNDN); string = std::string(raw_string); if (mpfr_zero_p(r) == 0) { // The cast is to prevent a warning in case mpfr_exp_t is not // defined as an int, but as a short or a long. std::sprintf(raw_string, "%d", (int)(exp - 1)); } else { std::sprintf(raw_string, "0"); } if (string[0] == '-') { string = string.substr(0, 2) + std::string(".") + string.substr(2) + std::string("e") + std::string(raw_string); } else { string = string.substr(0, 1) + std::string(".") + string.substr(1) + std::string("e") + std::string(raw_string); } // Free the array. delete[] raw_string; } return string; }
static void special (void) { mpfr_t x, y; mpfr_init (x); mpfr_init (y); mpfr_set_nan (x); mpfr_li2 (y, x, MPFR_RNDN); if (!mpfr_nan_p (y)) { printf ("Error for li2(NaN)\n"); exit (1); } mpfr_set_inf (x, -1); mpfr_li2 (y, x, MPFR_RNDN); if (!MPFR_IS_INF (y) || MPFR_IS_POS (y)) { printf ("Error for li2(-Inf)\n"); exit (1); } mpfr_set_inf (x, 1); mpfr_li2 (y, x, MPFR_RNDN); if (!MPFR_IS_INF (y) || MPFR_IS_POS (y)) { printf ("Error for li2(+Inf)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_li2 (y, x, MPFR_RNDN); if (!MPFR_IS_ZERO (y) || MPFR_IS_NEG (y)) { printf ("Error for li2(+0)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); mpfr_li2 (y, x, MPFR_RNDN); if (!MPFR_IS_ZERO (y) || MPFR_IS_POS (y)) { printf ("Error for li2(-0)\n"); exit (1); } mpfr_clear (x); mpfr_clear (y); }
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 special (void) { mpfr_t x; int inex; mpfr_init (x); mpfr_set_nan (x); inex = mpfr_log2 (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0); mpfr_set_inf (x, -1); inex = mpfr_log2 (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0); mpfr_set_inf (x, 1); inex = mpfr_log2 (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) > 0 && inex == 0); mpfr_set_ui (x, 0, MPFR_RNDN); inex = mpfr_log2 (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) < 0 && inex == 0); mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); inex = mpfr_log2 (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) < 0 && inex == 0); mpfr_set_si (x, -1, MPFR_RNDN); inex = mpfr_log2 (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0); mpfr_set_si (x, 1, MPFR_RNDN); inex = mpfr_log2 (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x) && inex == 0); mpfr_clear (x); }
int mpfi_interv_fr (mpfi_ptr a, mpfr_srcptr b, mpfr_srcptr c) { int inexact_left, inexact_right, inexact=0; if ( mpfr_nan_p (b) || mpfr_nan_p (c) ) { mpfr_set_nan (&(a->left)); mpfr_set_nan (&(a->right)); MPFR_RET_NAN; } if ( mpfr_cmp (b, c) <= 0 ) { inexact_left = mpfr_set (&(a->left), b, MPFI_RNDD); inexact_right = mpfr_set (&(a->right), c, MPFI_RNDU); } else { inexact_left = mpfr_set (&(a->left), c, MPFI_RNDD); inexact_right = mpfr_set (&(a->right), b, MPFI_RNDU); } /* a cannot be a NaN, it has been tested before */ if (inexact_left) inexact += 1; if (inexact_right) inexact += 2; /* do not allow -0 as lower bound */ if (mpfr_zero_p (&(a->left)) && mpfr_signbit (&(a->left))) { mpfr_neg (&(a->left), &(a->left), MPFI_RNDU); } /* do not allow +0 as upper bound */ if (mpfr_zero_p (&(a->right)) && !mpfr_signbit (&(a->right))) { mpfr_neg (&(a->right), &(a->right), MPFI_RNDD); } return inexact; }
static void special_erfc (void) { mpfr_t x, y; mpfr_inits (x, y, (mpfr_ptr) 0); /* erfc (NaN) = NaN */ mpfr_set_nan (x); mpfr_erfc (y, x, MPFR_RNDN); if (!mpfr_nan_p (y)) { printf ("mpfr_erfc failed for x=NaN\n"); exit (1); } /* erfc(+Inf) = 0+ */ mpfr_set_inf (x, 1); mpfr_erfc (y, x, MPFR_RNDN); if (!MPFR_IS_ZERO (y) || !MPFR_IS_POS (y)) { printf ("mpfr_erf failed for x=+Inf\n"); printf ("expected 0+, got "); mpfr_dump (y); exit (1); } /* erfc(-Inf) = 2 */ mpfr_set_inf (x, -1); mpfr_erfc (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 2)) { printf ("mpfr_erf failed for x=-Inf\n"); printf ("expected 2, got "); mpfr_dump (y); exit (1); } /* erf(+0) = 1 */ mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_erfc (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1)) { printf ("mpfr_erf failed for x=+0\n"); printf ("expected 1, got "); mpfr_dump (y); exit (1); } mpfr_clears (x, y, (mpfr_ptr) 0); }
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); }
int main (void) { mpfr_t x, y, z; tests_start_mpfr (); mpfr_init (x); mpfr_init (y); mpfr_init (z); /* case y=NaN */ mpfr_set_nan (y); mpfr_set_ui (x, 1250, GMP_RNDN); mpfr_copysign (z, x, y, GMP_RNDN); if (!mpfr_nan_p (z)) { printf ("Error in mpfr_copysign (NaN)\n"); exit (1); } /* case y!=NaN */ mpfr_set_ui (y, 123, GMP_RNDN); mpfr_set_ui (x, 1250, GMP_RNDN); mpfr_copysign (z, x, y, GMP_RNDN); if (mpfr_cmp_ui (z, 1250)) { printf ("Error in mpfr_copysign (1250)\n"); exit (1); } mpfr_set_si (y, -17, GMP_RNDN); mpfr_set_ui (x, 42, GMP_RNDN); mpfr_copysign (z, x, y, GMP_RNDN); if (mpfr_cmp_si (z, -42)) { printf ("Error in mpfr_copysign (-42)\n"); exit (1); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); tests_end_mpfr (); return 0; }
tree gfc_conv_mpfr_to_tree (mpfr_t f, int kind, int is_snan) { tree type; int n; REAL_VALUE_TYPE real; n = gfc_validate_kind (BT_REAL, kind, false); gcc_assert (gfc_real_kinds[n].radix == 2); type = gfc_get_real_type (kind); if (mpfr_nan_p (f) && is_snan) real_from_string (&real, "SNaN"); else real_from_mpfr (&real, f, type, GFC_RND_MODE); return build_real (type, real); }
SeedValue seed_mpfr_nan_p (SeedContext ctx, SeedObject function, SeedObject this_object, gsize argument_count, const SeedValue args[], SeedException *exception) { mpfr_ptr rop; gboolean ret; CHECK_ARG_COUNT("mpfr.nan_p", 0); rop = seed_object_get_private(this_object); ret = mpfr_nan_p(rop); return seed_value_from_boolean(ctx, ret, exception); }
static void check_nans (void) { mpfr_t x, s, c; mpfr_init2 (x, 123L); mpfr_init2 (s, 123L); mpfr_init2 (c, 123L); /* sin(NaN)==NaN, cos(NaN)==NaN */ mpfr_set_nan (x); mpfr_sin_cos (s, c, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (s)); MPFR_ASSERTN (mpfr_nan_p (c)); /* sin(+Inf)==NaN, cos(+Inf)==NaN */ mpfr_set_inf (x, 1); mpfr_sin_cos (s, c, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (s)); MPFR_ASSERTN (mpfr_nan_p (c)); /* sin(-Inf)==NaN, cos(-Inf)==NaN */ mpfr_set_inf (x, -1); mpfr_sin_cos (s, c, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (s)); MPFR_ASSERTN (mpfr_nan_p (c)); /* check zero */ mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_sin_cos (s, c, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui (s, 0) == 0 && MPFR_IS_POS (s)); MPFR_ASSERTN (mpfr_cmp_ui (c, 1) == 0); mpfr_neg (x, x, MPFR_RNDN); mpfr_sin_cos (s, c, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui (s, 0) == 0 && MPFR_IS_NEG (s)); MPFR_ASSERTN (mpfr_cmp_ui (c, 1) == 0); /* coverage test */ mpfr_set_prec (x, 2); mpfr_set_ui (x, 4, MPFR_RNDN); mpfr_set_prec (s, 2); mpfr_set_prec (c, 2); mpfr_sin_cos (s, c, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_si_2exp (s, -3, -2) == 0); MPFR_ASSERTN (mpfr_cmp_si_2exp (c, -3, -2) == 0); mpfr_clear (x); mpfr_clear (s); mpfr_clear (c); }
void real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode) { /* We use a string as an intermediate type. */ char buf[128], *rstr; mp_exp_t exp; /* Take care of Infinity and NaN. */ if (mpfr_inf_p (m)) { real_inf (r); if (mpfr_sgn (m) < 0) *r = real_value_negate (r); return; } if (mpfr_nan_p (m)) { real_nan (r, "", 1, TYPE_MODE (type)); return; } rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode); /* The additional 12 chars add space for the sprintf below. This leaves 6 digits for the exponent which is supposedly enough. */ gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12); /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp, mpfr_get_str returns the exponent for mantissa * 16**exp, adjust for that. */ exp *= 4; if (rstr[0] == '-') sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp); else sprintf (buf, "0x.%sp%d", rstr, (int) exp); mpfr_free_str (rstr); real_from_string (r, buf); }
void check53 (double x, double sin_x, mp_rnd_t rnd_mode) { mpfr_t xx, s; mpfr_init2 (xx, 53); mpfr_init2 (s, 53); mpfr_set_d (xx, x, rnd_mode); /* should be exact */ mpfr_sin (s, xx, rnd_mode); if (mpfr_get_d1 (s) != sin_x && (!isnan(sin_x) || !mpfr_nan_p(s))) { fprintf (stderr, "mpfr_sin failed for x=%1.20e, rnd=%s\n", x, mpfr_print_rnd_mode (rnd_mode)); fprintf (stderr, "mpfr_sin gives sin(x)=%1.20e, expected %1.20e\n", mpfr_get_d1 (s), sin_x); exit(1); } mpfr_clear (xx); mpfr_clear (s); }
static void tiny (void) { mpfr_t x, s, c; int i, inex; mpfr_inits2 (64, x, s, c, (mpfr_ptr) 0); for (i = -1; i <= 1; i += 2) { mpfr_set_si (x, i, MPFR_RNDN); mpfr_set_exp (x, mpfr_get_emin ()); inex = mpfr_sin_cos (s, c, x, MPFR_RNDN); MPFR_ASSERTN (inex != 0); MPFR_ASSERTN (mpfr_equal_p (s, x)); MPFR_ASSERTN (!mpfr_nan_p (c) && mpfr_cmp_ui (c, 1) == 0); } mpfr_clears (x, s, c, (mpfr_ptr) 0); }
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; }
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 }
static void exp_range (void) { mpfr_t x; mpfr_exp_t emin; emin = mpfr_get_emin (); set_emin (3); mpfr_init2 (x, 8); mpfr_set_ui (x, 5, MPFR_RNDN); mpfr_exp2 (x, x, MPFR_RNDN); set_emin (emin); if (mpfr_nan_p (x) || mpfr_cmp_ui (x, 32) != 0) { printf ("Error in mpfr_exp2 for x = 5, with emin = 3\n"); printf ("Expected 32, got "); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); printf ("\n"); exit (1); } mpfr_clear (x); }