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 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); } }
static void test_overflow1 (void) { mpfr_t x, y, z, r; int inex; mpfr_inits2 (8, x, y, z, r, (void *) 0); MPFR_SET_POS (x); mpfr_setmax (x, mpfr_get_emax ()); /* x = 2^emax - ulp */ mpfr_set_ui (y, 2, GMP_RNDN); /* y = 2 */ mpfr_neg (z, x, GMP_RNDN); /* z = -x = -(2^emax - ulp) */ mpfr_clear_flags (); /* The intermediate multiplication x * y overflows, but x * y + z = x is representable. */ inex = mpfr_fma (r, x, y, z, GMP_RNDN); if (inex || ! mpfr_equal_p (r, x)) { printf ("Error in test_overflow1\nexpected "); mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); printf (" with inex = 0\n got "); mpfr_out_str (stdout, 2, 0, r, GMP_RNDN); printf (" with inex = %d\n", inex); exit (1); } if (mpfr_overflow_p ()) { printf ("Error in test_overflow1: overflow flag set\n"); exit (1); } mpfr_clears (x, y, z, r, (void *) 0); }
static bool do_mpfr_ckconv (real_value *result, mpfr_srcptr m, bool inexact, const real_format *format) { /* Proceed iff we get a normal number, i.e. not NaN or Inf and no overflow/underflow occurred. If -frounding-math, proceed iff the result of calling FUNC was exact. */ if (!mpfr_number_p (m) || mpfr_overflow_p () || mpfr_underflow_p () || (flag_rounding_math && inexact)) return false; REAL_VALUE_TYPE tmp; real_from_mpfr (&tmp, m, format, GMP_RNDN); /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values. If the REAL_VALUE_TYPE is zero but the mpft_t is not, then we underflowed in the conversion. */ if (!real_isfinite (&tmp) || ((tmp.cl == rvc_zero) != (mpfr_zero_p (m) != 0))) return false; real_convert (result, format, &tmp); return real_identical (result, &tmp); }
static void check_overflow (void) { mpfr_t a, b, c; mpfr_prec_t prec_a; int r; mpfr_init2 (a, 256); mpfr_init2 (b, 256); mpfr_init2 (c, 256); mpfr_set_ui (b, 1, MPFR_RNDN); mpfr_setmax (b, mpfr_get_emax ()); mpfr_set_ui (c, 1, MPFR_RNDN); mpfr_set_exp (c, mpfr_get_emax () - 192); RND_LOOP(r) for (prec_a = 128; prec_a < 512; prec_a += 64) { mpfr_set_prec (a, prec_a); mpfr_clear_overflow (); test_add (a, b, c, (mpfr_rnd_t) r); if (!mpfr_overflow_p ()) { printf ("No overflow in check_overflow\n"); exit (1); } } mpfr_set_exp (c, mpfr_get_emax () - 512); mpfr_set_prec (a, 256); mpfr_clear_overflow (); test_add (a, b, c, MPFR_RNDU); if (!mpfr_overflow_p ()) { printf ("No overflow in check_overflow\n"); exit (1); } mpfr_clear (a); mpfr_clear (b); mpfr_clear (c); }
/* Bug found while adding tests for mpfr_cot */ static void test_20070628 (void) { mpfr_exp_t old_emax; mpfr_t x, y; int inex, err = 0; old_emax = mpfr_get_emax (); if (mpfr_set_emax (256)) { printf ("Can't change exponent range\n"); exit (1); } mpfr_inits2 (53, x, y, (mpfr_ptr) 0); mpfr_set_si (x, -1, MPFR_RNDN); mpfr_set_si_2exp (y, 1, -256, MPFR_RNDN); mpfr_clear_flags (); inex = mpfr_div (x, x, y, MPFR_RNDD); if (MPFR_SIGN (x) >= 0 || ! mpfr_inf_p (x)) { printf ("Error in test_20070628: expected -Inf, got\n"); mpfr_dump (x); err++; } if (inex >= 0) { printf ("Error in test_20070628: expected inex < 0, got %d\n", inex); err++; } if (! mpfr_overflow_p ()) { printf ("Error in test_20070628: overflow flag is not set\n"); err++; } mpfr_clears (x, y, (mpfr_ptr) 0); mpfr_set_emax (old_emax); }
static void check_overflow (void) { mpfr_t x, y; int inex, r; mpfr_inits2 (8, x, y, (mpfr_ptr) 0); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_setmax (x, mpfr_get_emax ()); RND_LOOP(r) { mpfr_clear_overflow (); inex = mpfr_hypot (y, x, x, (mpfr_rnd_t) r); if (!mpfr_overflow_p ()) { printf ("No overflow in check_overflow for %s%s\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), ext ? ", extended exponent range" : ""); exit (1); } MPFR_ASSERTN (MPFR_IS_POS (y)); if (r == MPFR_RNDZ || r == MPFR_RNDD) { MPFR_ASSERTN (inex < 0); MPFR_ASSERTN (!mpfr_inf_p (y)); mpfr_nexttoinf (y); } else { MPFR_ASSERTN (inex > 0); } MPFR_ASSERTN (mpfr_inf_p (y)); } mpfr_clears (x, y, (mpfr_ptr) 0); }
static void check_special (void) { mpfr_t x, y, z; mpfr_exp_t emin, emax; emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_init (x); mpfr_init (y); mpfr_init (z); /* check exp(NaN) = NaN */ mpfr_set_nan (x); test_exp (y, x, MPFR_RNDN); if (!mpfr_nan_p (y)) { printf ("Error for exp(NaN)\n"); exit (1); } /* check exp(+inf) = +inf */ mpfr_set_inf (x, 1); test_exp (y, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) { printf ("Error for exp(+inf)\n"); exit (1); } /* check exp(-inf) = +0 */ mpfr_set_inf (x, -1); test_exp (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) { printf ("Error for exp(-inf)\n"); exit (1); } /* Check overflow. Corner case of mpfr_exp_2 */ mpfr_set_prec (x, 64); mpfr_set_emax (MPFR_EMAX_DEFAULT); mpfr_set_emin (MPFR_EMIN_DEFAULT); mpfr_set_str (x, "0.1011000101110010000101111111010100001100000001110001100111001101E30", 2, MPFR_RNDN); mpfr_exp (x, x, MPFR_RNDD); if (mpfr_cmp_str (x, ".1111111111111111111111111111111111111111111111111111111111111111E1073741823", 2, MPFR_RNDN) != 0) { printf ("Wrong overflow detection in mpfr_exp\n"); mpfr_dump (x); exit (1); } /* Check underflow. Corner case of mpfr_exp_2 */ mpfr_set_str (x, "-0.1011000101110010000101111111011111010001110011110111100110101100E30", 2, MPFR_RNDN); mpfr_exp (x, x, MPFR_RNDN); if (mpfr_cmp_str (x, "0.1E-1073741823", 2, MPFR_RNDN) != 0) { printf ("Wrong underflow (1) detection in mpfr_exp\n"); mpfr_dump (x); exit (1); } mpfr_set_str (x, "-0.1011001101110010000101111111011111010001110011110111100110111101E30", 2, MPFR_RNDN); mpfr_exp (x, x, MPFR_RNDN); if (mpfr_cmp_ui (x, 0) != 0) { printf ("Wrong underflow (2) detection in mpfr_exp\n"); mpfr_dump (x); exit (1); } /* Check overflow. Corner case of mpfr_exp_3 */ if (MPFR_PREC_MAX >= MPFR_EXP_THRESHOLD + 10 && MPFR_PREC_MAX >= 64) { /* this ensures that for small MPFR_EXP_THRESHOLD, the following mpfr_set_str conversion is exact */ mpfr_set_prec (x, (MPFR_EXP_THRESHOLD + 10 > 64) ? MPFR_EXP_THRESHOLD + 10 : 64); mpfr_set_str (x, "0.1011000101110010000101111111010100001100000001110001100111001101E30", 2, MPFR_RNDN); mpfr_clear_overflow (); mpfr_exp (x, x, MPFR_RNDD); if (!mpfr_overflow_p ()) { printf ("Wrong overflow detection in mpfr_exp_3\n"); mpfr_dump (x); exit (1); } /* Check underflow. Corner case of mpfr_exp_3 */ mpfr_set_str (x, "-0.1011000101110010000101111111011111010001110011110111100110101100E30", 2, MPFR_RNDN); mpfr_clear_underflow (); mpfr_exp (x, x, MPFR_RNDN); if (!mpfr_underflow_p ()) { printf ("Wrong underflow detection in mpfr_exp_3\n"); mpfr_dump (x); exit (1); } mpfr_set_prec (x, 53); } /* check overflow */ set_emax (10); mpfr_set_ui (x, 7, MPFR_RNDN); test_exp (y, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) { printf ("Error for exp(7) for emax=10\n"); exit (1); } set_emax (emax); /* check underflow */ set_emin (-10); mpfr_set_si (x, -9, MPFR_RNDN); test_exp (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) { printf ("Error for exp(-9) for emin=-10\n"); printf ("Expected +0\n"); printf ("Got "); mpfr_print_binary (y); puts (""); exit (1); } set_emin (emin); /* check case EXP(x) < -precy */ mpfr_set_prec (y, 2); mpfr_set_str_binary (x, "-0.1E-3"); test_exp (y, x, MPFR_RNDD); if (mpfr_cmp_ui_2exp (y, 3, -2)) { printf ("Error for exp(-1/16), prec=2, RNDD\n"); printf ("expected 0.11, got "); mpfr_dump (y); exit (1); } test_exp (y, x, MPFR_RNDZ); if (mpfr_cmp_ui_2exp (y, 3, -2)) { printf ("Error for exp(-1/16), prec=2, RNDZ\n"); printf ("expected 0.11, got "); mpfr_dump (y); exit (1); } mpfr_set_str_binary (x, "0.1E-3"); test_exp (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1)) { printf ("Error for exp(1/16), prec=2, RNDN\n"); exit (1); } test_exp (y, x, MPFR_RNDU); if (mpfr_cmp_ui_2exp (y, 3, -1)) { printf ("Error for exp(1/16), prec=2, RNDU\n"); exit (1); } /* bug reported by Franky Backeljauw, 28 Mar 2003 */ mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_str_binary (x, "1.1101011000111101011110000111010010101001101001110111e28"); test_exp (y, x, MPFR_RNDN); mpfr_set_prec (x, 153); mpfr_set_prec (z, 153); mpfr_set_str_binary (x, "1.1101011000111101011110000111010010101001101001110111e28"); test_exp (z, x, MPFR_RNDN); mpfr_prec_round (z, 53, MPFR_RNDN); if (mpfr_cmp (y, z)) { printf ("Error in mpfr_exp for large argument\n"); exit (1); } /* corner cases in mpfr_exp_3 */ mpfr_set_prec (x, 2); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_set_prec (y, 2); mpfr_exp_3 (y, x, MPFR_RNDN); /* Check some little things about overflow detection */ set_emin (-125); set_emax (128); mpfr_set_prec (x, 107); mpfr_set_prec (y, 107); mpfr_set_str_binary (x, "0.11110000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000" "00000000E4"); test_exp (y, x, MPFR_RNDN); if (mpfr_cmp_str (y, "0.11000111100001100110010101111101011010010101010000" "1101110111100010111001011111111000110111001011001101010" "01E22", 2, MPFR_RNDN)) { printf ("Special overflow error (1)\n"); mpfr_dump (y); exit (1); } set_emin (emin); set_emax (emax); /* Check for overflow producing a segfault with HUGE exponent */ mpfr_set_ui (x, 3, MPFR_RNDN); mpfr_mul_2ui (x, x, 32, MPFR_RNDN); test_exp (y, x, MPFR_RNDN); /* Can't test return value: May overflow or not*/ /* Bug due to wrong approximation of (x)/log2 */ mpfr_set_prec (x, 163); mpfr_set_str (x, "-4.28ac8fceeadcda06bb56359017b1c81b85b392e7", 16, MPFR_RNDN); mpfr_exp (x, x, MPFR_RNDN); if (mpfr_cmp_str (x, "3.fffffffffffffffffffffffffffffffffffffffe8@-2", 16, MPFR_RNDN)) { printf ("Error for x= -4.28ac8fceeadcda06bb56359017b1c81b85b392e7"); printf ("expected 3.fffffffffffffffffffffffffffffffffffffffe8@-2"); printf ("Got "); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); } /* bug found by Guillaume Melquiond, 13 Sep 2005 */ mpfr_set_prec (x, 53); mpfr_set_str_binary (x, "-1E-400"); mpfr_exp (x, x, MPFR_RNDZ); if (mpfr_cmp_ui (x, 1) == 0) { printf ("Error for exp(-2^(-400))\n"); exit (1); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
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.101101010001001101111010E0"); mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_str (y, "0.100111011001100111000010111101000111010101011110E0", 2, MPFR_RNDN)) { printf("Special Overflow error.\n"); mpfr_dump (y); exit (1); } /* intermediate Pi overflows while atan(+Inf) = Pi/2 is representable */ set_emax (1); mpfr_set_inf (x, +1); mpfr_clear_flags (); mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_str (y, "C90FDAA22169p-47", 16, MPFR_RNDN) || mpfr_overflow_p ()) { printf("atan(+Inf) = Pi/2 should not overflow when emax = %ld\n", (long int) mpfr_get_emax ()); mpfr_dump (y); exit (1); } /* atan(+Inf) = Pi/2 underflows */ set_emax (128); set_emin (3); mpfr_clear_flags (); mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || !mpfr_underflow_p ()) { printf("atan(+Inf) = Pi/2 should underflow when emin = %ld\n", (long int) mpfr_get_emin ()); mpfr_dump (y); exit (1); } /* intermediate Pi overflows while atan(+1) = Pi/4 is representable */ set_emax (1); set_emin (-128); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_clear_flags (); mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_str (y, "C90FDAA22169p-48", 16, MPFR_RNDN) || mpfr_overflow_p ()) { printf("atan(+1) = Pi/4 should not overflow when emax = %ld\n", (long int) mpfr_get_emax ()); mpfr_dump (y); exit (1); } /* atan(+1) = Pi/4 underflows and is rounded up to 1 */ set_emax (128); set_emin (1); mpfr_set_prec (y, 2); mpfr_clear_flags (); mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1) || !mpfr_underflow_p ()) { printf("atan(+1) = Pi/4 should underflow when emin = %+ld\n", (long int) mpfr_get_emin ()); mpfr_dump (y); exit (1); } /* atan(+1) = Pi/4 underflows and is rounded down to 0 */ mpfr_clear_flags (); mpfr_atan (y, x, MPFR_RNDD); if (mpfr_cmp_ui (y, 0) || !mpfr_underflow_p ()) { printf("atan(+1) = Pi/4 should underflow when emin = %+ld\n", (long int) mpfr_get_emin ()); mpfr_dump (y); exit (1); } mpfr_clear (y); mpfr_clear (x); set_emin (emin); set_emax (emax); }
static void overflowed_sin_cos0 (void) { mpfr_t x, y, z; int emax, inex, rnd, err = 0; mpfr_exp_t old_emax; old_emax = mpfr_get_emax (); mpfr_init2 (x, 8); mpfr_init2 (y, 8); mpfr_init2 (z, 8); for (emax = -1; emax <= 0; emax++) { mpfr_set_ui_2exp (z, 1, emax, MPFR_RNDN); mpfr_nextbelow (z); set_emax (emax); /* 1 is not representable. */ /* and if emax < 0, 1 - eps is not representable either. */ RND_LOOP (rnd) { mpfr_set_si (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); mpfr_clear_flags (); inex = mpfr_sin_cos (x, y, x, (mpfr_rnd_t) rnd); if (! mpfr_overflow_p ()) { printf ("Error in overflowed_sin_cos0 (rnd = %s):\n" " The overflow flag is not set.\n", mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); err = 1; } if (! (mpfr_zero_p (x) && MPFR_IS_NEG (x))) { printf ("Error in overflowed_sin_cos0 (rnd = %s):\n" " Got sin = ", mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); mpfr_print_binary (x); printf (" instead of -0.\n"); err = 1; } if (rnd == MPFR_RNDZ || rnd == MPFR_RNDD) { if (inex == 0) { printf ("Error in overflowed_sin_cos0 (rnd = %s):\n" " The inexact value must be non-zero.\n", mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); err = 1; } if (! mpfr_equal_p (y, z)) { printf ("Error in overflowed_sin_cos0 (rnd = %s):\n" " Got cos = ", mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); mpfr_print_binary (y); printf (" instead of 0.11111111E%d.\n", emax); err = 1; } } else { if (inex == 0) { printf ("Error in overflowed_sin_cos0 (rnd = %s):\n" " The inexact value must be non-zero.\n", mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); err = 1; } if (! (mpfr_inf_p (y) && MPFR_IS_POS (y))) { printf ("Error in overflowed_sin_cos0 (rnd = %s):\n" " Got cos = ", mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); mpfr_print_binary (y); printf (" instead of +Inf.\n"); err = 1; } } } set_emax (old_emax); } if (err) exit (1); mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
int main (int argc, char *argv[]) { mpfr_t x, y; mpfr_exp_t emin, emax; tests_start_mpfr (); special_overflow (); emax_m_eps (); exp_range (); mpfr_init (x); mpfr_init (y); mpfr_set_ui (x, 4, MPFR_RNDN); mpfr_exp2 (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 16) != 0) { printf ("Error for 2^4, MPFR_RNDN\n"); exit (1); } mpfr_exp2 (y, x, MPFR_RNDD); if (mpfr_cmp_ui (y, 16) != 0) { printf ("Error for 2^4, MPFR_RNDD\n"); exit (1); } mpfr_exp2 (y, x, MPFR_RNDU); if (mpfr_cmp_ui (y, 16) != 0) { printf ("Error for 2^4, MPFR_RNDU\n"); exit (1); } mpfr_set_si (x, -4, MPFR_RNDN); mpfr_exp2 (y, x, MPFR_RNDN); if (mpfr_cmp_ui_2exp (y, 1, -4) != 0) { printf ("Error for 2^(-4), MPFR_RNDN\n"); exit (1); } mpfr_exp2 (y, x, MPFR_RNDD); if (mpfr_cmp_ui_2exp (y, 1, -4) != 0) { printf ("Error for 2^(-4), MPFR_RNDD\n"); exit (1); } mpfr_exp2 (y, x, MPFR_RNDU); if (mpfr_cmp_ui_2exp (y, 1, -4) != 0) { printf ("Error for 2^(-4), MPFR_RNDU\n"); exit (1); } mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_str (x, /*-1683977482443233.0 / 2199023255552.0*/ "-7.6578429909351734750089235603809357e2", 10, MPFR_RNDN); mpfr_exp2 (y, x, MPFR_RNDN); if (mpfr_cmp_str1 (y, "2.991959870867646566478e-231")) { printf ("Error for x=-1683977482443233/2^41\n"); exit (1); } mpfr_set_prec (x, 10); mpfr_set_prec (y, 10); /* save emin */ emin = mpfr_get_emin (); set_emin (-10); mpfr_set_si (x, -12, MPFR_RNDN); mpfr_exp2 (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) { printf ("Error for x=emin-2, RNDN\n"); printf ("Expected +0\n"); printf ("Got "); mpfr_print_binary (y); puts (""); exit (1); } /* restore emin */ set_emin (emin); /* save emax */ emax = mpfr_get_emax (); set_emax (10); mpfr_set_ui (x, 11, MPFR_RNDN); mpfr_exp2 (y, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) { printf ("Error for x=emax+1, RNDN\n"); exit (1); } /* restore emax */ set_emax (emax); MPFR_SET_INF(x); MPFR_SET_POS(x); mpfr_exp2 (y, x, MPFR_RNDN); if(!MPFR_IS_INF(y)) { printf ("evaluation of function in INF does not return INF\n"); exit (1); } MPFR_CHANGE_SIGN(x); mpfr_exp2 (y, x, MPFR_RNDN); if(!MPFR_IS_ZERO(y)) { printf ("evaluation of function in -INF does not return 0\n"); exit (1); } MPFR_SET_NAN(x); mpfr_exp2 (y, x, MPFR_RNDN); if(!MPFR_IS_NAN(y)) { printf ("evaluation of function in NaN does not return NaN\n"); exit (1); } if ((mpfr_uexp_t) 8 << 31 != 0 || mpfr_get_emax () <= (mpfr_uexp_t) 100000 * 100000) { /* emax <= 10000000000 */ mpfr_set_prec (x, 40); mpfr_set_prec (y, 40); mpfr_set_str (x, "10000000000.5", 10, MPFR_RNDN); mpfr_clear_flags (); mpfr_exp2 (y, x, MPFR_RNDN); if (!(MPFR_IS_INF (y) && MPFR_IS_POS (y) && mpfr_overflow_p ())) { printf ("exp2(10000000000.5) should overflow.\n"); exit (1); } } mpfr_set_prec (x, 2); mpfr_set_prec (y, 2); mpfr_set_str_binary (x, "-1.0E-26"); mpfr_exp2 (y, x, MPFR_RNDD); mpfr_set_str_binary (x, "1.1E-1"); if (mpfr_cmp (x, y)) { printf ("Error for exp(-2^(-26)) for prec=2\n"); exit (1); } test_generic (2, 100, 100); mpfr_clear (x); mpfr_clear (y); overflowed_exp2_0 (); data_check ("data/exp2", mpfr_exp2, "mpfr_exp2"); tests_end_mpfr (); return 0; }
static void test_underflow1 (void) { mpfr_t x, y, z, r; int inex, signy, signz, rnd, err = 0; mpfr_inits2 (8, x, y, z, r, (void *) 0); MPFR_SET_POS (x); mpfr_setmin (x, mpfr_get_emin ()); /* x = 0.1@emin */ for (signy = -1; signy <= 1; signy += 2) { mpfr_set_si_2exp (y, signy, -1, GMP_RNDN); /* |y| = 1/2 */ for (signz = -3; signz <= 3; signz += 2) { RND_LOOP (rnd) { mpfr_set_si (z, signz, GMP_RNDN); if (ABS (signz) != 1) mpfr_setmax (z, mpfr_get_emax ()); /* |z| = 1 or 2^emax - ulp */ mpfr_clear_flags (); inex = mpfr_fma (r, x, y, z, rnd); #define ERRTU1 "Error in test_underflow1 (signy = %d, signz = %d, %s)\n " if (mpfr_nanflag_p ()) { printf (ERRTU1 "NaN flag is set\n", signy, signz, mpfr_print_rnd_mode (rnd)); err = 1; } if (signy < 0 && (rnd == GMP_RNDD || (rnd == GMP_RNDZ && signz > 0))) mpfr_nextbelow (z); if (signy > 0 && (rnd == GMP_RNDU || (rnd == GMP_RNDZ && signz < 0))) mpfr_nextabove (z); if ((mpfr_overflow_p () != 0) ^ (mpfr_inf_p (z) != 0)) { printf (ERRTU1 "wrong overflow flag\n", signy, signz, mpfr_print_rnd_mode (rnd)); err = 1; } if (mpfr_underflow_p ()) { printf (ERRTU1 "underflow flag is set\n", signy, signz, mpfr_print_rnd_mode (rnd)); err = 1; } if (! mpfr_equal_p (r, z)) { printf (ERRTU1 "got ", signy, signz, mpfr_print_rnd_mode (rnd)); mpfr_print_binary (r); printf (" instead of "); mpfr_print_binary (z); printf ("\n"); err = 1; } if (inex >= 0 && (rnd == GMP_RNDD || (rnd == GMP_RNDZ && signz > 0) || (rnd == GMP_RNDN && signy > 0))) { printf (ERRTU1 "ternary value = %d instead of < 0\n", signy, signz, mpfr_print_rnd_mode (rnd), inex); err = 1; } if (inex <= 0 && (rnd == GMP_RNDU || (rnd == GMP_RNDZ && signz < 0) || (rnd == GMP_RNDN && signy < 0))) { printf (ERRTU1 "ternary value = %d instead of > 0\n", signy, signz, mpfr_print_rnd_mode (rnd), inex); err = 1; } } } } if (err) exit (1); mpfr_clears (x, y, z, r, (void *) 0); }
static void overflowed_sec0 (void) { mpfr_t x, y; int emax, i, inex, rnd, err = 0; mpfr_exp_t old_emax; old_emax = mpfr_get_emax (); mpfr_init2 (x, 8); mpfr_init2 (y, 8); for (emax = -1; emax <= 0; emax++) { mpfr_set_ui_2exp (y, 1, emax, MPFR_RNDN); mpfr_nextbelow (y); set_emax (emax); /* 1 is not representable. */ for (i = -1; i <= 1; i++) RND_LOOP (rnd) { mpfr_set_si_2exp (x, i, -512 * ABS (i), MPFR_RNDN); mpfr_clear_flags (); inex = mpfr_sec (x, x, (mpfr_rnd_t) rnd); if (! mpfr_overflow_p ()) { printf ("Error in overflowed_sec0 (i = %d, rnd = %s):\n" " The overflow flag is not set.\n", i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); err = 1; } if (rnd == MPFR_RNDZ || rnd == MPFR_RNDD) { if (inex >= 0) { printf ("Error in overflowed_sec0 (i = %d, rnd = %s):\n" " The inexact value must be negative.\n", i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); err = 1; } if (! mpfr_equal_p (x, y)) { printf ("Error in overflowed_sec0 (i = %d, rnd = %s):\n" " Got ", i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); mpfr_print_binary (x); printf (" instead of 0.11111111E%d.\n", emax); err = 1; } } else { if (inex <= 0) { printf ("Error in overflowed_sec0 (i = %d, rnd = %s):\n" " The inexact value must be positive.\n", i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); err = 1; } if (! (mpfr_inf_p (x) && MPFR_SIGN (x) > 0)) { printf ("Error in overflowed_sec0 (i = %d, rnd = %s):\n" " Got ", i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); mpfr_print_binary (x); printf (" instead of +Inf.\n"); err = 1; } } } set_emax (old_emax); } if (err) exit (1); mpfr_clear (x); mpfr_clear (y); }
static void special (void) { mpfr_t x, y; int i; mpfr_init (x); mpfr_init (y); mpfr_set_nan (x); test_expm1 (y, x, MPFR_RNDN); if (!mpfr_nan_p (y)) { printf ("Error for expm1(NaN)\n"); exit (1); } mpfr_set_inf (x, 1); test_expm1 (y, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) { printf ("Error for expm1(+Inf)\n"); exit (1); } mpfr_set_inf (x, -1); test_expm1 (y, x, MPFR_RNDN); if (mpfr_cmp_si (y, -1)) { printf ("Error for expm1(-Inf)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); test_expm1 (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) { printf ("Error for expm1(+0)\n"); exit (1); } mpfr_neg (x, x, MPFR_RNDN); test_expm1 (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) > 0) { printf ("Error for expm1(-0)\n"); exit (1); } /* Check overflow of expm1(x) */ mpfr_clear_flags (); mpfr_set_str_binary (x, "1.1E1000000000"); i = test_expm1 (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, "1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDU); 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, "1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDD); MPFR_ASSERTN (!MPFR_IS_INF (x) && MPFR_SIGN (x) > 0); MPFR_ASSERTN (mpfr_overflow_p ()); MPFR_ASSERTN (i == -1); /* Check internal underflow of expm1 (x) */ mpfr_set_prec (x, 2); mpfr_clear_flags (); mpfr_set_str_binary (x, "-1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_si (x, -1) == 0); MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ()); MPFR_ASSERTN (i == -1); mpfr_set_str_binary (x, "-1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDD); MPFR_ASSERTN (mpfr_cmp_si (x, -1) == 0); MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ()); MPFR_ASSERTN (i == -1); mpfr_set_str_binary (x, "-1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDZ); MPFR_ASSERTN (mpfr_cmp_str (x, "-0.11", 2, MPFR_RNDN) == 0); MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ()); MPFR_ASSERTN (i == 1); mpfr_set_str_binary (x, "-1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDU); MPFR_ASSERTN (mpfr_cmp_str (x, "-0.11", 2, MPFR_RNDN) == 0); MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ()); MPFR_ASSERTN (i == 1); mpfr_clear (x); mpfr_clear (y); }
static void overflowed_fac0 (void) { mpfr_t x, y; int inex, rnd, err = 0; mp_exp_t old_emax; old_emax = mpfr_get_emax (); mpfr_init2 (x, 8); mpfr_init2 (y, 8); mpfr_set_ui (y, 1, GMP_RNDN); mpfr_nextbelow (y); set_emax (0); /* 1 is not representable. */ RND_LOOP (rnd) { mpfr_clear_flags (); inex = mpfr_fac_ui (x, 0, rnd); if (! mpfr_overflow_p ()) { printf ("Error in overflowed_fac0 (rnd = %s):\n" " The overflow flag is not set.\n", mpfr_print_rnd_mode (rnd)); err = 1; } if (rnd == GMP_RNDZ || rnd == GMP_RNDD) { if (inex >= 0) { printf ("Error in overflowed_fac0 (rnd = %s):\n" " The inexact value must be negative.\n", mpfr_print_rnd_mode (rnd)); err = 1; } if (! mpfr_equal_p (x, y)) { printf ("Error in overflowed_fac0 (rnd = %s):\n" " Got ", mpfr_print_rnd_mode (rnd)); mpfr_print_binary (x); printf (" instead of 0.11111111E0.\n"); err = 1; } } else { if (inex <= 0) { printf ("Error in overflowed_fac0 (rnd = %s):\n" " The inexact value must be positive.\n", mpfr_print_rnd_mode (rnd)); err = 1; } if (! (mpfr_inf_p (x) && MPFR_SIGN (x) > 0)) { printf ("Error in overflowed_fac0 (rnd = %s):\n" " Got ", mpfr_print_rnd_mode (rnd)); mpfr_print_binary (x); printf (" instead of +Inf.\n"); err = 1; } } } set_emax (old_emax); if (err) exit (1); mpfr_clear (x); mpfr_clear (y); }
int mpfr_sinh (mpfr_ptr y, mpfr_srcptr xt, mp_rnd_t rnd_mode) { mpfr_t x; int inexact; MPFR_LOG_FUNC (("x[%#R]=%R rnd=%d", xt, xt, rnd_mode), ("y[%#R]=%R inexact=%d", y, y, inexact)); if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (xt))) { if (MPFR_IS_NAN (xt)) { MPFR_SET_NAN (y); MPFR_RET_NAN; } else if (MPFR_IS_INF (xt)) { MPFR_SET_INF (y); MPFR_SET_SAME_SIGN (y, xt); MPFR_RET (0); } else /* xt is zero */ { MPFR_ASSERTD (MPFR_IS_ZERO (xt)); MPFR_SET_ZERO (y); /* sinh(0) = 0 */ MPFR_SET_SAME_SIGN (y, xt); MPFR_RET (0); } } /* sinh(x) = x + x^3/6 + ... so the error is < 2^(3*EXP(x)-2) */ MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, xt, -2 * MPFR_GET_EXP(xt), 2, 1, rnd_mode, {}); MPFR_TMP_INIT_ABS (x, xt); { mpfr_t t, ti; mp_exp_t d; mp_prec_t Nt; /* Precision of the intermediary variable */ long int err; /* Precision of error */ MPFR_ZIV_DECL (loop); MPFR_SAVE_EXPO_DECL (expo); MPFR_GROUP_DECL (group); MPFR_SAVE_EXPO_MARK (expo); /* compute the precision of intermediary variable */ Nt = MAX (MPFR_PREC (x), MPFR_PREC (y)); /* the optimal number of bits : see algorithms.ps */ Nt = Nt + MPFR_INT_CEIL_LOG2 (Nt) + 4; /* If x is near 0, exp(x) - 1/exp(x) = 2*x+x^3/3+O(x^5) */ if (MPFR_GET_EXP (x) < 0) Nt -= 2*MPFR_GET_EXP (x); /* initialise of intermediary variables */ MPFR_GROUP_INIT_2 (group, Nt, t, ti); /* First computation of sinh */ MPFR_ZIV_INIT (loop, Nt); for (;;) { /* compute sinh */ mpfr_clear_flags (); mpfr_exp (t, x, GMP_RNDD); /* exp(x) */ /* exp(x) can overflow! */ /* BUG/TODO/FIXME: exp can overflow but sinh may be representable! */ if (MPFR_UNLIKELY (mpfr_overflow_p ())) { inexact = mpfr_overflow (y, rnd_mode, MPFR_SIGN (xt)); MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, MPFR_FLAGS_OVERFLOW); break; } d = MPFR_GET_EXP (t); mpfr_ui_div (ti, 1, t, GMP_RNDU); /* 1/exp(x) */ mpfr_sub (t, t, ti, GMP_RNDN); /* exp(x) - 1/exp(x) */ mpfr_div_2ui (t, t, 1, GMP_RNDN); /* 1/2(exp(x) - 1/exp(x)) */ /* it may be that t is zero (in fact, it can only occur when te=1, and thus ti=1 too) */ if (MPFR_IS_ZERO (t)) err = Nt; /* double the precision */ else { /* calculation of the error */ d = d - MPFR_GET_EXP (t) + 2; /* error estimate: err = Nt-(__gmpfr_ceil_log2(1+pow(2,d)));*/ err = Nt - (MAX (d, 0) + 1); if (MPFR_LIKELY (MPFR_CAN_ROUND (t, err, MPFR_PREC (y), rnd_mode))) { inexact = mpfr_set4 (y, t, rnd_mode, MPFR_SIGN (xt)); break; } } /* actualisation of the precision */ Nt += err; MPFR_ZIV_NEXT (loop, Nt); MPFR_GROUP_REPREC_2 (group, Nt, t, ti); } MPFR_ZIV_FREE (loop); MPFR_GROUP_CLEAR (group); MPFR_SAVE_EXPO_FREE (expo); } return mpfr_check_range (y, inexact, rnd_mode); }
static void underflows (void) { mpfr_t x, y, z; int err = 0; int inexact; int i; mp_exp_t emin; mpfr_init2 (x, 64); mpfr_init2 (y, 64); mpfr_set_ui (x, 1, GMP_RNDN); mpfr_set_exp (x, mpfr_get_emin()); for (i = 3; i < 10; i++) { mpfr_set_ui (y, i, GMP_RNDN); mpfr_div_2ui (y, y, 1, GMP_RNDN); test_pow (y, x, y, GMP_RNDN); if (!MPFR_IS_FP(y) || mpfr_cmp_ui (y, 0)) { printf ("Error in mpfr_pow for "); mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); printf (" ^ (%d/2)\nGot ", i); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf (" instead of 0.\n"); exit (1); } } mpfr_init2 (z, 55); mpfr_set_str (x, "0.110011010011101001110001110100010000110111101E0", 2, GMP_RNDN); mpfr_set_str (y, "0.101110010011111001011010100011011100111110011E40", 2, GMP_RNDN); mpfr_clear_flags (); inexact = mpfr_pow (z, x, y, GMP_RNDU); if (!mpfr_underflow_p ()) { printf ("Underflow flag is not set for special underflow test.\n"); err = 1; } if (inexact <= 0) { printf ("Ternary value is wrong for special underflow test.\n"); err = 1; } mpfr_set_ui (x, 0, GMP_RNDN); mpfr_nextabove (x); if (mpfr_cmp (x, z) != 0) { printf ("Wrong value for special underflow test.\nGot "); mpfr_out_str (stdout, 2, 0, z, GMP_RNDN); printf ("\ninstead of "); mpfr_out_str (stdout, 2, 2, x, GMP_RNDN); printf ("\n"); err = 1; } if (err) exit (1); /* MPFR currently (2006-08-19) segfaults on the following code (and possibly makes other programs crash due to the lack of memory), because y is converted into an mpz_t, and the required precision is too high. */ mpfr_set_prec (x, 2); mpfr_set_prec (y, 2); mpfr_set_prec (z, 12); mpfr_set_ui_2exp (x, 3, -2, GMP_RNDN); mpfr_set_ui_2exp (y, 1, mpfr_get_emax () - 1, GMP_RNDN); mpfr_clear_flags (); mpfr_pow (z, x, y, GMP_RNDN); if (!mpfr_underflow_p () || MPFR_NOTZERO (z)) { printf ("Underflow test with large y fails.\n"); exit (1); } emin = mpfr_get_emin (); mpfr_set_emin (-256); mpfr_set_prec (x, 2); mpfr_set_prec (y, 2); mpfr_set_prec (z, 12); mpfr_set_ui_2exp (x, 3, -2, GMP_RNDN); mpfr_set_ui_2exp (y, 1, 38, GMP_RNDN); mpfr_clear_flags (); inexact = mpfr_pow (z, x, y, GMP_RNDN); if (!mpfr_underflow_p () || MPFR_NOTZERO (z) || inexact >= 0) { printf ("Bad underflow detection for 0.75^(2^38). Obtained:\n" "Underflow flag... %-3s (should be 'yes')\n" "Zero result...... %-3s (should be 'yes')\n" "Inexact value.... %-3d (should be negative)\n", mpfr_underflow_p () ? "yes" : "no", MPFR_IS_ZERO (z) ? "yes" : "no", inexact); exit (1); } mpfr_set_emin (emin); emin = mpfr_get_emin (); mpfr_set_emin (-256); mpfr_set_prec (x, 2); mpfr_set_prec (y, 40); mpfr_set_prec (z, 12); mpfr_set_ui_2exp (x, 3, -1, GMP_RNDN); mpfr_set_si_2exp (y, -1, 38, GMP_RNDN); for (i = 0; i < 4; i++) { if (i == 2) mpfr_neg (x, x, GMP_RNDN); mpfr_clear_flags (); inexact = mpfr_pow (z, x, y, GMP_RNDN); if (!mpfr_underflow_p () || MPFR_NOTZERO (z) || (i == 3 ? (inexact <= 0) : (inexact >= 0))) { printf ("Bad underflow detection for ("); mpfr_out_str (stdout, 10, 0, x, GMP_RNDN); printf (")^(-2^38-%d). Obtained:\n" "Overflow flag.... %-3s (should be 'no')\n" "Underflow flag... %-3s (should be 'yes')\n" "Zero result...... %-3s (should be 'yes')\n" "Inexact value.... %-3d (should be %s)\n", i, mpfr_overflow_p () ? "yes" : "no", mpfr_underflow_p () ? "yes" : "no", MPFR_IS_ZERO (z) ? "yes" : "no", inexact, i == 3 ? "positive" : "negative"); exit (1); } inexact = mpfr_sub_ui (y, y, 1, GMP_RNDN); MPFR_ASSERTN (inexact == 0); } mpfr_set_emin (emin); mpfr_clears (x, y, z, (void *) 0); }
static int tiny_aux (int stop, mpfr_exp_t e) { mpfr_t x, y, z; int r, s, spm, inex, err = 0; int expected_dir[2][5] = { { 1, -1, 1, -1, 1 }, { 1, 1, 1, -1, -1 } }; mpfr_exp_t saved_emax; saved_emax = mpfr_get_emax (); mpfr_init2 (x, 32); mpfr_inits2 (8, y, z, (mpfr_ptr) 0); mpfr_set_ui_2exp (x, 1, e, MPFR_RNDN); spm = 1; for (s = 0; s < 2; s++) { RND_LOOP(r) { mpfr_rnd_t rr = (mpfr_rnd_t) r; mpfr_exp_t exponent, emax; /* Exponent of the rounded value in unbounded exponent range. */ exponent = expected_dir[s][r] < 0 && s == 0 ? - e : 1 - e; for (emax = exponent - 1; emax <= exponent; emax++) { unsigned int flags, expected_flags = MPFR_FLAGS_INEXACT; int overflow, expected_inex = expected_dir[s][r]; if (emax > MPFR_EMAX_MAX) break; mpfr_set_emax (emax); mpfr_clear_flags (); inex = mpfr_gamma (y, x, rr); flags = __gmpfr_flags; mpfr_clear_flags (); mpfr_set_si_2exp (z, spm, - e, MPFR_RNDU); overflow = mpfr_overflow_p (); /* z is 1/x - euler rounded toward +inf */ if (overflow && rr == MPFR_RNDN && s == 1) expected_inex = -1; if (expected_inex < 0) mpfr_nextbelow (z); /* 1/x - euler rounded toward -inf */ if (exponent > emax) expected_flags |= MPFR_FLAGS_OVERFLOW; if (!(mpfr_equal_p (y, z) && flags == expected_flags && SAME_SIGN (inex, expected_inex))) { printf ("Error in tiny for s = %d, r = %s, emax = %" MPFR_EXP_FSPEC "d%s\n on ", s, mpfr_print_rnd_mode (rr), emax, exponent > emax ? " (overflow)" : ""); mpfr_dump (x); printf (" expected inex = %2d, ", expected_inex); mpfr_dump (z); printf (" got inex = %2d, ", SIGN (inex)); mpfr_dump (y); printf (" expected flags = %u, got %u\n", expected_flags, flags); if (stop) exit (1); err = 1; } } } mpfr_neg (x, x, MPFR_RNDN); spm = - spm; } mpfr_clears (x, y, z, (mpfr_ptr) 0); mpfr_set_emax (saved_emax); return err; }
/* Put in z the value of x^y, rounded according to 'rnd'. Return the inexact flag in [0, 10]. */ int mpc_pow (mpc_ptr z, mpc_srcptr x, mpc_srcptr y, mpc_rnd_t rnd) { int ret = -2, loop, x_real, x_imag, y_real, z_real = 0, z_imag = 0; mpc_t t, u; mpfr_prec_t p, pr, pi, maxprec; int saved_underflow, saved_overflow; /* save the underflow or overflow flags from MPFR */ saved_underflow = mpfr_underflow_p (); saved_overflow = mpfr_overflow_p (); x_real = mpfr_zero_p (mpc_imagref(x)); y_real = mpfr_zero_p (mpc_imagref(y)); if (y_real && mpfr_zero_p (mpc_realref(y))) /* case y zero */ { if (x_real && mpfr_zero_p (mpc_realref(x))) { /* we define 0^0 to be (1, +0) since the real part is coherent with MPFR where 0^0 gives 1, and the sign of the imaginary part cannot be determined */ mpc_set_ui_ui (z, 1, 0, MPC_RNDNN); return 0; } else /* x^0 = 1 +/- i*0 even for x=NaN see algorithms.tex for the sign of zero */ { mpfr_t n; int inex, cx1; int sign_zi; /* cx1 < 0 if |x| < 1 cx1 = 0 if |x| = 1 cx1 > 0 if |x| > 1 */ mpfr_init (n); inex = mpc_norm (n, x, MPFR_RNDN); cx1 = mpfr_cmp_ui (n, 1); if (cx1 == 0 && inex != 0) cx1 = -inex; sign_zi = (cx1 < 0 && mpfr_signbit (mpc_imagref (y)) == 0) || (cx1 == 0 && mpfr_signbit (mpc_imagref (x)) != mpfr_signbit (mpc_realref (y))) || (cx1 > 0 && mpfr_signbit (mpc_imagref (y))); /* warning: mpc_set_ui_ui does not set Im(z) to -0 if Im(rnd)=RNDD */ ret = mpc_set_ui_ui (z, 1, 0, rnd); if (MPC_RND_IM (rnd) == MPFR_RNDD || sign_zi) mpc_conj (z, z, MPC_RNDNN); mpfr_clear (n); return ret; } } if (!mpc_fin_p (x) || !mpc_fin_p (y)) { /* special values: exp(y*log(x)) */ mpc_init2 (u, 2); mpc_log (u, x, MPC_RNDNN); mpc_mul (u, u, y, MPC_RNDNN); ret = mpc_exp (z, u, rnd); mpc_clear (u); goto end; } if (x_real) /* case x real */ { if (mpfr_zero_p (mpc_realref(x))) /* x is zero */ { /* special values: exp(y*log(x)) */ mpc_init2 (u, 2); mpc_log (u, x, MPC_RNDNN); mpc_mul (u, u, y, MPC_RNDNN); ret = mpc_exp (z, u, rnd); mpc_clear (u); goto end; } /* Special case 1^y = 1 */ if (mpfr_cmp_ui (mpc_realref(x), 1) == 0) { int s1, s2; s1 = mpfr_signbit (mpc_realref (y)); s2 = mpfr_signbit (mpc_imagref (x)); ret = mpc_set_ui (z, +1, rnd); /* the sign of the zero imaginary part is known in some cases (see algorithm.tex). In such cases we have (x +s*0i)^(y+/-0i) = x^y + s*sign(y)*0i where s = +/-1. We extend here this rule to fix the sign of the zero part. Note that the sign must also be set explicitly when rnd=RNDD because mpfr_set_ui(z_i, 0, rnd) always sets z_i to +0. */ if (MPC_RND_IM (rnd) == MPFR_RNDD || s1 != s2) mpc_conj (z, z, MPC_RNDNN); goto end; } /* x^y is real when: (a) x is real and y is integer (b) x is real non-negative and y is real */ if (y_real && (mpfr_integer_p (mpc_realref(y)) || mpfr_cmp_ui (mpc_realref(x), 0) >= 0)) { int s1, s2; s1 = mpfr_signbit (mpc_realref (y)); s2 = mpfr_signbit (mpc_imagref (x)); ret = mpfr_pow (mpc_realref(z), mpc_realref(x), mpc_realref(y), MPC_RND_RE(rnd)); ret = MPC_INEX(ret, mpfr_set_ui (mpc_imagref(z), 0, MPC_RND_IM(rnd))); /* the sign of the zero imaginary part is known in some cases (see algorithm.tex). In such cases we have (x +s*0i)^(y+/-0i) = x^y + s*sign(y)*0i where s = +/-1. We extend here this rule to fix the sign of the zero part. Note that the sign must also be set explicitly when rnd=RNDD because mpfr_set_ui(z_i, 0, rnd) always sets z_i to +0. */ if (MPC_RND_IM(rnd) == MPFR_RNDD || s1 != s2) mpfr_neg (mpc_imagref(z), mpc_imagref(z), MPC_RND_IM(rnd)); goto end; } /* (-1)^(n+I*t) is real for n integer and t real */ if (mpfr_cmp_si (mpc_realref(x), -1) == 0 && mpfr_integer_p (mpc_realref(y))) z_real = 1; /* for x real, x^y is imaginary when: (a) x is negative and y is half-an-integer (b) x = -1 and Re(y) is half-an-integer */ if ((mpfr_cmp_ui (mpc_realref(x), 0) < 0) && is_odd (mpc_realref(y), 1) && (y_real || mpfr_cmp_si (mpc_realref(x), -1) == 0)) z_imag = 1; } else /* x non real */ /* I^(t*I) and (-I)^(t*I) are real for t real, I^(n+t*I) and (-I)^(n+t*I) are real for n even and t real, and I^(n+t*I) and (-I)^(n+t*I) are imaginary for n odd and t real (s*I)^n is real for n even and imaginary for n odd */ if ((mpc_cmp_si_si (x, 0, 1) == 0 || mpc_cmp_si_si (x, 0, -1) == 0 || (mpfr_cmp_ui (mpc_realref(x), 0) == 0 && y_real)) && mpfr_integer_p (mpc_realref(y))) { /* x is I or -I, and Re(y) is an integer */ if (is_odd (mpc_realref(y), 0)) z_imag = 1; /* Re(y) odd: z is imaginary */ else z_real = 1; /* Re(y) even: z is real */ } else /* (t+/-t*I)^(2n) is imaginary for n odd and real for n even */ if (mpfr_cmpabs (mpc_realref(x), mpc_imagref(x)) == 0 && y_real && mpfr_integer_p (mpc_realref(y)) && is_odd (mpc_realref(y), 0) == 0) { if (is_odd (mpc_realref(y), -1)) /* y/2 is odd */ z_imag = 1; else z_real = 1; } pr = mpfr_get_prec (mpc_realref(z)); pi = mpfr_get_prec (mpc_imagref(z)); p = (pr > pi) ? pr : pi; p += 12; /* experimentally, seems to give less than 10% of failures in Ziv's strategy; probably wrong now since q is not computed */ if (p < 64) p = 64; mpc_init2 (u, p); mpc_init2 (t, p); pr += MPC_RND_RE(rnd) == MPFR_RNDN; pi += MPC_RND_IM(rnd) == MPFR_RNDN; maxprec = MPC_MAX_PREC (z); x_imag = mpfr_zero_p (mpc_realref(x)); for (loop = 0;; loop++) { int ret_exp; mpfr_exp_t dr, di; mpfr_prec_t q; mpc_log (t, x, MPC_RNDNN); mpc_mul (t, t, y, MPC_RNDNN); /* Compute q such that |Re (y log x)|, |Im (y log x)| < 2^q. We recompute it at each loop since we might get different bounds if the precision is not enough. */ q = mpfr_get_exp (mpc_realref(t)) > 0 ? mpfr_get_exp (mpc_realref(t)) : 0; if (mpfr_get_exp (mpc_imagref(t)) > (mpfr_exp_t) q) q = mpfr_get_exp (mpc_imagref(t)); mpfr_clear_overflow (); mpfr_clear_underflow (); ret_exp = mpc_exp (u, t, MPC_RNDNN); if (mpfr_underflow_p () || mpfr_overflow_p ()) { /* under- and overflow flags are set by mpc_exp */ mpc_set (z, u, MPC_RNDNN); ret = ret_exp; goto exact; } /* Since the error bound is global, we have to take into account the exponent difference between the real and imaginary parts. We assume either the real or the imaginary part of u is not zero. */ dr = mpfr_zero_p (mpc_realref(u)) ? mpfr_get_exp (mpc_imagref(u)) : mpfr_get_exp (mpc_realref(u)); di = mpfr_zero_p (mpc_imagref(u)) ? dr : mpfr_get_exp (mpc_imagref(u)); if (dr > di) { di = dr - di; dr = 0; } else { dr = di - dr; di = 0; } /* the term -3 takes into account the factor 4 in the complex error (see algorithms.tex) plus one due to the exponent difference: if z = a + I*b, where the relative error on z is at most 2^(-p), and EXP(a) = EXP(b) + k, the relative error on b is at most 2^(k-p) */ if ((z_imag || (p > q + 3 + dr && mpfr_can_round (mpc_realref(u), p - q - 3 - dr, MPFR_RNDN, MPFR_RNDZ, pr))) && (z_real || (p > q + 3 + di && mpfr_can_round (mpc_imagref(u), p - q - 3 - di, MPFR_RNDN, MPFR_RNDZ, pi)))) break; /* if Re(u) is not known to be zero, assume it is a normal number, i.e., neither zero, Inf or NaN, otherwise we might enter an infinite loop */ MPC_ASSERT (z_imag || mpfr_number_p (mpc_realref(u))); /* idem for Im(u) */ MPC_ASSERT (z_real || mpfr_number_p (mpc_imagref(u))); if (ret == -2) /* we did not yet call mpc_pow_exact, or it aborted because intermediate computations had > maxprec bits */ { /* check exact cases (see algorithms.tex) */ if (y_real) { maxprec *= 2; ret = mpc_pow_exact (z, x, mpc_realref(y), rnd, maxprec); if (ret != -1 && ret != -2) goto exact; } p += dr + di + 64; } else p += p / 2; mpc_set_prec (t, p); mpc_set_prec (u, p); } if (z_real) { /* When the result is real (see algorithm.tex for details), Im(x^y) = + sign(imag(y))*0i, if |x| > 1 + sign(imag(x))*sign(real(y))*0i, if |x| = 1 - sign(imag(y))*0i, if |x| < 1 */ mpfr_t n; int inex, cx1; int sign_zi, sign_rex, sign_imx; /* cx1 < 0 if |x| < 1 cx1 = 0 if |x| = 1 cx1 > 0 if |x| > 1 */ sign_rex = mpfr_signbit (mpc_realref (x)); sign_imx = mpfr_signbit (mpc_imagref (x)); mpfr_init (n); inex = mpc_norm (n, x, MPFR_RNDN); cx1 = mpfr_cmp_ui (n, 1); if (cx1 == 0 && inex != 0) cx1 = -inex; sign_zi = (cx1 < 0 && mpfr_signbit (mpc_imagref (y)) == 0) || (cx1 == 0 && sign_imx != mpfr_signbit (mpc_realref (y))) || (cx1 > 0 && mpfr_signbit (mpc_imagref (y))); /* copy RE(y) to n since if z==y we will destroy Re(y) below */ mpfr_set_prec (n, mpfr_get_prec (mpc_realref (y))); mpfr_set (n, mpc_realref (y), MPFR_RNDN); ret = mpfr_set (mpc_realref(z), mpc_realref(u), MPC_RND_RE(rnd)); if (y_real && (x_real || x_imag)) { /* FIXME: with y_real we assume Im(y) is really 0, which is the case for example when y comes from pow_fr, but in case Im(y) is +0 or -0, we might get different results */ mpfr_set_ui (mpc_imagref (z), 0, MPC_RND_IM (rnd)); fix_sign (z, sign_rex, sign_imx, n); ret = MPC_INEX(ret, 0); /* imaginary part is exact */ } else { ret = MPC_INEX (ret, mpfr_set_ui (mpc_imagref (z), 0, MPC_RND_IM (rnd))); /* warning: mpfr_set_ui does not set Im(z) to -0 if Im(rnd) = RNDD */ if (MPC_RND_IM (rnd) == MPFR_RNDD || sign_zi) mpc_conj (z, z, MPC_RNDNN); } mpfr_clear (n); } else if (z_imag) { ret = mpfr_set (mpc_imagref(z), mpc_imagref(u), MPC_RND_IM(rnd)); /* if z is imaginary and y real, then x cannot be real */ if (y_real && x_imag) { int sign_rex = mpfr_signbit (mpc_realref (x)); /* If z overlaps with y we set Re(z) before checking Re(y) below, but in that case y=0, which was dealt with above. */ mpfr_set_ui (mpc_realref (z), 0, MPC_RND_RE (rnd)); /* Note: fix_sign only does something when y is an integer, then necessarily y = 1 or 3 (mod 4), and in that case the sign of Im(x) is irrelevant. */ fix_sign (z, sign_rex, 0, mpc_realref (y)); ret = MPC_INEX(0, ret); } else ret = MPC_INEX(mpfr_set_ui (mpc_realref(z), 0, MPC_RND_RE(rnd)), ret); } else ret = mpc_set (z, u, rnd); exact: mpc_clear (t); mpc_clear (u); /* restore underflow and overflow flags from MPFR */ if (saved_underflow) mpfr_set_underflow (); if (saved_overflow) mpfr_set_overflow (); end: return ret; }
int main (int argc, char *argv[]) { mpfr_t x, y; mpfr_exp_t emin, emax; int inex, ov; tests_start_mpfr (); special_overflow (); emax_m_eps (); exp_range (); mpfr_init (x); mpfr_init (y); mpfr_set_ui (x, 4, MPFR_RNDN); mpfr_exp10 (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 10000) != 0) { printf ("Error for 10^4, MPFR_RNDN\n"); exit (1); } mpfr_exp10 (y, x, MPFR_RNDD); if (mpfr_cmp_ui (y, 10000) != 0) { printf ("Error for 10^4, MPFR_RNDD\n"); exit (1); } mpfr_exp10 (y, x, MPFR_RNDU); if (mpfr_cmp_ui (y, 10000) != 0) { printf ("Error for 10^4, MPFR_RNDU\n"); exit (1); } mpfr_set_prec (x, 10); mpfr_set_prec (y, 10); /* save emin */ emin = mpfr_get_emin (); set_emin (-11); mpfr_set_si (x, -4, MPFR_RNDN); mpfr_exp10 (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) { printf ("Error for emin = -11, x = -4, RNDN\n"); printf ("Expected +0\n"); printf ("Got "); mpfr_print_binary (y); puts (""); exit (1); } /* restore emin */ set_emin (emin); /* save emax */ emax = mpfr_get_emax (); set_emax (13); mpfr_set_ui (x, 4, MPFR_RNDN); mpfr_exp10 (y, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) { printf ("Error for emax = 13, x = 4, RNDN\n"); printf ("Expected +inf\n"); printf ("Got "); mpfr_print_binary (y); puts (""); exit (1); } /* restore emax */ set_emax (emax); MPFR_SET_INF (x); MPFR_SET_POS (x); mpfr_exp10 (y, x, MPFR_RNDN); if (!MPFR_IS_INF (y)) { printf ("evaluation of function in INF does not return INF\n"); exit (1); } MPFR_CHANGE_SIGN (x); mpfr_exp10 (y, x, MPFR_RNDN); if (!MPFR_IS_ZERO (y)) { printf ("evaluation of function in -INF does not return 0\n"); exit (1); } MPFR_SET_NAN (x); mpfr_exp10 (y, x, MPFR_RNDN); if (!MPFR_IS_NAN (y)) { printf ("evaluation of function in NaN does not return NaN\n"); exit (1); } if ((mpfr_uexp_t) 8 << 31 != 0 || mpfr_get_emax () <= (mpfr_uexp_t) 100000 * 100000) { /* emax <= 10000000000 */ mpfr_set_prec (x, 40); mpfr_set_prec (y, 40); mpfr_set_str (x, "3010299957", 10, MPFR_RNDN); mpfr_clear_flags (); inex = mpfr_exp10 (y, x, MPFR_RNDN); ov = mpfr_overflow_p (); if (!(MPFR_IS_INF (y) && MPFR_IS_POS (y) && ov)) { printf ("Overflow error for x = 3010299957, MPFR_RNDN.\n"); mpfr_dump (y); printf ("inex = %d, %soverflow\n", inex, ov ? "" : "no "); exit (1); } } test_generic (2, 100, 100); mpfr_clear (x); mpfr_clear (y); overfl_exp10_0 (); data_check ("data/exp10", mpfr_exp10, "mpfr_exp10"); tests_end_mpfr (); return 0; }
int main (void) { mpfr_t a; mp_limb_t *p, tmp; mp_size_t s; mpfr_prec_t pr; int max; tests_start_mpfr (); for(pr = MPFR_PREC_MIN ; pr < 500 ; pr++) { mpfr_init2 (a, pr); if (!mpfr_check(a)) ERROR("for init"); /* Check special cases */ MPFR_SET_NAN(a); if (!mpfr_check(a)) ERROR("for nan"); MPFR_SET_POS(a); MPFR_SET_INF(a); if (!mpfr_check(a)) ERROR("for inf"); MPFR_SET_ZERO(a); if (!mpfr_check(a)) ERROR("for zero"); MPFR_EXP (a) = MPFR_EXP_MIN; if (mpfr_check(a)) ERROR("for EXP = MPFR_EXP_MIN"); /* Check var */ mpfr_set_ui(a, 2, MPFR_RNDN); if (!mpfr_check(a)) ERROR("for set_ui"); mpfr_clear_overflow(); max = 1000; /* Allows max 2^1000 bits for the exponent */ while ((!mpfr_overflow_p()) && (max>0)) { mpfr_mul(a, a, a, MPFR_RNDN); if (!mpfr_check(a)) ERROR("for mul"); max--; } if (max==0) ERROR("can't reach overflow"); mpfr_set_ui(a, 2137, MPFR_RNDN); /* Corrupt a and check for it */ MPFR_SIGN(a) = 2; if (mpfr_check(a)) ERROR("sgn"); MPFR_SET_POS(a); /* Check prec */ MPFR_PREC(a) = MPFR_PREC_MIN - 1; if (mpfr_check(a)) ERROR("precmin"); #if MPFR_VERSION_MAJOR < 3 /* Disable the test with MPFR >= 3 since mpfr_prec_t is now signed. The "if" below is sufficient, but the MPFR_PREC_MAX+1 generates a warning with GCC 4.4.4 even though the test is always false. */ if ((mpfr_prec_t) 0 - 1 > 0) { MPFR_PREC(a) = MPFR_PREC_MAX+1; if (mpfr_check(a)) ERROR("precmax"); } #endif MPFR_PREC(a) = pr; if (!mpfr_check(a)) ERROR("prec"); /* Check exponent */ MPFR_EXP(a) = MPFR_EXP_INVALID; if (mpfr_check(a)) ERROR("exp invalid"); MPFR_EXP(a) = -MPFR_EXP_INVALID; if (mpfr_check(a)) ERROR("-exp invalid"); MPFR_EXP(a) = 0; if (!mpfr_check(a)) ERROR("exp 0"); /* Check Mantissa */ p = MPFR_MANT(a); MPFR_MANT(a) = NULL; if (mpfr_check(a)) ERROR("Mantissa Null Ptr"); MPFR_MANT(a) = p; /* Check size */ s = MPFR_GET_ALLOC_SIZE(a); MPFR_SET_ALLOC_SIZE(a, 0); if (mpfr_check(a)) ERROR("0 size"); MPFR_SET_ALLOC_SIZE(a, MP_SIZE_T_MIN); if (mpfr_check(a)) ERROR("min size"); MPFR_SET_ALLOC_SIZE(a, MPFR_LIMB_SIZE(a)-1 ); if (mpfr_check(a)) ERROR("size < prec"); MPFR_SET_ALLOC_SIZE(a, s); /* Check normal form */ tmp = MPFR_MANT(a)[0]; if ((pr % GMP_NUMB_BITS) != 0) { MPFR_MANT(a)[0] = MPFR_LIMB_MAX; if (mpfr_check(a)) ERROR("last bits non 0"); } MPFR_MANT(a)[0] = tmp; MPFR_MANT(a)[MPFR_LIMB_SIZE(a)-1] &= MPFR_LIMB_MASK (GMP_NUMB_BITS-1); if (mpfr_check(a)) ERROR("last bits non 0"); /* Final */ mpfr_set_ui(a, 2137, MPFR_RNDN); if (!mpfr_check(a)) ERROR("after last set"); mpfr_clear (a); if (mpfr_check(a)) ERROR("after clear"); } tests_end_mpfr (); return 0; }
static void test_generic (mpfr_prec_t p0, mpfr_prec_t p1, unsigned int nmax) { mpfr_prec_t prec, xprec, yprec; mpfr_t x, y, z, t, w; #if defined(TWO_ARGS_ALL) mpfr_t u; #endif #if defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) double d; #endif #if defined(ULONG_ARG1) || defined(ULONG_ARG2) unsigned long i; #endif mpfr_rnd_t rnd; int inexact, compare, compare2; unsigned int n; unsigned long ctrt = 0, ctrn = 0; int test_of = 1, test_uf = 1; mpfr_exp_t old_emin, old_emax; old_emin = mpfr_get_emin (); old_emax = mpfr_get_emax (); mpfr_inits2 (MPFR_PREC_MIN, x, y, z, t, w, (mpfr_ptr) 0); #if defined(TWO_ARGS_ALL) mpfr_init2 (u, MPFR_PREC_MIN); #endif /* generic test */ for (prec = p0; prec <= p1; prec++) { mpfr_set_prec (z, prec); mpfr_set_prec (t, prec); yprec = prec + 10; mpfr_set_prec (y, yprec); mpfr_set_prec (w, yprec); /* Note: in precision p1, we test 4 special cases. */ for (n = 0; n < (prec == p1 ? nmax + 4 : nmax); n++) { int infinite_input = 0; unsigned int flags; mpfr_exp_t oemin, oemax; xprec = prec; if (randlimb () & 1) { xprec *= (double) randlimb () / MP_LIMB_T_MAX; if (xprec < MPFR_PREC_MIN) xprec = MPFR_PREC_MIN; } mpfr_set_prec (x, xprec); #if defined(TWO_ARGS) mpfr_set_prec (u, xprec); #elif defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_set_prec (u, IEEE_DBL_MANT_DIG); #elif defined(ULONG_ARG1) || defined(ULONG_ARG2) mpfr_set_prec (u, sizeof (unsigned long) * CHAR_BIT); #endif if (n > 3 || prec < p1) { #if defined(RAND_FUNCTION) RAND_FUNCTION (x); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) RAND_FUNCTION (u); #endif #else /* ! defined(RAND_FUNCTION) */ tests_default_random (x, TEST_RANDOM_POS, TEST_RANDOM_EMIN, TEST_RANDOM_EMAX, TEST_RANDOM_ALWAYS_SCALE); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) tests_default_random (u, TEST_RANDOM_POS2, TEST_RANDOM_EMIN, TEST_RANDOM_EMAX, TEST_RANDOM_ALWAYS_SCALE); #endif #endif /* ! defined(RAND_FUNCTION) */ } else { /* Special cases tested in precision p1 if n <= 3. They are useful really in the extended exponent range. */ #if (defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2)) && defined(MPFR_ERRDIVZERO) goto next_n; #endif set_emin (MPFR_EMIN_MIN); set_emax (MPFR_EMAX_MAX); if (n <= 1) { mpfr_set_si (x, n == 0 ? 1 : -1, MPFR_RNDN); mpfr_set_exp (x, mpfr_get_emin ()); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_set_si (u, randlimb () % 2 == 0 ? 1 : -1, MPFR_RNDN); mpfr_set_exp (u, mpfr_get_emin ()); #endif } else /* 2 <= n <= 3 */ { if (getenv ("MPFR_CHECK_MAX") == NULL) goto next_n; mpfr_set_si (x, n == 0 ? 1 : -1, MPFR_RNDN); mpfr_setmax (x, REDUCE_EMAX); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_set_si (u, randlimb () % 2 == 0 ? 1 : -1, MPFR_RNDN); mpfr_setmax (u, mpfr_get_emax ()); #endif } } #if defined(ULONG_ARG1) || defined(ULONG_ARG2) i = randlimb (); inexact = mpfr_set_ui (u, i, MPFR_RNDN); MPFR_ASSERTN (inexact == 0); #endif /* Exponent range for the test. */ oemin = mpfr_get_emin (); oemax = mpfr_get_emax (); rnd = RND_RAND (); mpfr_clear_flags (); #ifdef DEBUG_TGENERIC TGENERIC_INFO (TEST_FUNCTION, MPFR_PREC (y)); #endif #if defined(TWO_ARGS) compare = TEST_FUNCTION (y, x, u, rnd); #elif defined(DOUBLE_ARG1) d = mpfr_get_d (u, rnd); compare = TEST_FUNCTION (y, d, x, rnd); /* d can be infinite due to overflow in mpfr_get_d */ infinite_input |= DOUBLE_ISINF (d); #elif defined(DOUBLE_ARG2) d = mpfr_get_d (u, rnd); compare = TEST_FUNCTION (y, x, d, rnd); /* d can be infinite due to overflow in mpfr_get_d */ infinite_input |= DOUBLE_ISINF (d); #elif defined(ULONG_ARG1) compare = TEST_FUNCTION (y, i, x, rnd); #elif defined(ULONG_ARG2) compare = TEST_FUNCTION (y, x, i, rnd); #else compare = TEST_FUNCTION (y, x, rnd); #endif flags = __gmpfr_flags; if (mpfr_get_emin () != oemin || mpfr_get_emax () != oemax) { printf ("tgeneric: the exponent range has been modified" " by the tested function!\n"); exit (1); } TGENERIC_CHECK ("bad inexact flag", (compare != 0) ^ (mpfr_inexflag_p () == 0)); ctrt++; /* Tests in a reduced exponent range. */ { unsigned int oldflags = flags; mpfr_exp_t e, emin, emax; /* Determine the smallest exponent range containing the exponents of the mpfr_t inputs (x, and u if TWO_ARGS) and output (y). */ emin = MPFR_EMAX_MAX; emax = MPFR_EMIN_MIN; if (MPFR_IS_PURE_FP (x)) { e = MPFR_GET_EXP (x); if (e < emin) emin = e; if (e > emax) emax = e; } #if defined(TWO_ARGS) if (MPFR_IS_PURE_FP (u)) { e = MPFR_GET_EXP (u); if (e < emin) emin = e; if (e > emax) emax = e; } #endif if (MPFR_IS_PURE_FP (y)) { e = MPFR_GET_EXP (y); if (test_of && e - 1 >= emax) { unsigned int ex_flags; mpfr_set_emax (e - 1); mpfr_clear_flags (); #if defined(TWO_ARGS) inexact = TEST_FUNCTION (w, x, u, rnd); #elif defined(DOUBLE_ARG1) inexact = TEST_FUNCTION (w, d, x, rnd); #elif defined(DOUBLE_ARG2) inexact = TEST_FUNCTION (w, x, d, rnd); #elif defined(ULONG_ARG1) inexact = TEST_FUNCTION (w, i, x, rnd); #elif defined(ULONG_ARG2) inexact = TEST_FUNCTION (w, x, i, rnd); #else inexact = TEST_FUNCTION (w, x, rnd); #endif flags = __gmpfr_flags; mpfr_set_emax (oemax); ex_flags = MPFR_FLAGS_OVERFLOW | MPFR_FLAGS_INEXACT; if (flags != ex_flags) { printf ("tgeneric: error for " MAKE_STR(TEST_FUNCTION) ", reduced exponent range [%" MPFR_EXP_FSPEC "d,%" MPFR_EXP_FSPEC "d] (overflow test) on:\n", (mpfr_eexp_t) oemin, (mpfr_eexp_t) e - 1); printf ("x = "); mpfr_dump (x); #if defined(TWO_ARGS_ALL) printf ("u = "); mpfr_dump (u); #endif printf ("yprec = %u, rnd_mode = %s\n", (unsigned int) yprec, mpfr_print_rnd_mode (rnd)); printf ("Expected flags ="); flags_out (ex_flags); printf (" got flags ="); flags_out (flags); printf ("inex = %d, w = ", inexact); mpfr_dump (w); exit (1); } test_of = 0; /* Overflow is tested only once. */ } if (test_uf && e + 1 <= emin) { unsigned int ex_flags; mpfr_set_emin (e + 1); mpfr_clear_flags (); #if defined(TWO_ARGS) inexact = TEST_FUNCTION (w, x, u, rnd); #elif defined(DOUBLE_ARG1) inexact = TEST_FUNCTION (w, d, x, rnd); #elif defined(DOUBLE_ARG2) inexact = TEST_FUNCTION (w, x, d, rnd); #elif defined(ULONG_ARG1) inexact = TEST_FUNCTION (w, i, x, rnd); #elif defined(ULONG_ARG2) inexact = TEST_FUNCTION (w, x, i, rnd); #else inexact = TEST_FUNCTION (w, x, rnd); #endif flags = __gmpfr_flags; mpfr_set_emin (oemin); ex_flags = MPFR_FLAGS_UNDERFLOW | MPFR_FLAGS_INEXACT; if (flags != ex_flags) { printf ("tgeneric: error for " MAKE_STR(TEST_FUNCTION) ", reduced exponent range [%" MPFR_EXP_FSPEC "d,%" MPFR_EXP_FSPEC "d] (underflow test) on:\n", (mpfr_eexp_t) e + 1, (mpfr_eexp_t) oemax); printf ("x = "); mpfr_dump (x); #if defined(TWO_ARGS_ALL) printf ("u = "); mpfr_dump (u); #endif printf ("yprec = %u, rnd_mode = %s\n", (unsigned int) yprec, mpfr_print_rnd_mode (rnd)); printf ("Expected flags ="); flags_out (ex_flags); printf (" got flags ="); flags_out (flags); printf ("inex = %d, w = ", inexact); mpfr_dump (w); exit (1); } test_uf = 0; /* Underflow is tested only once. */ } if (e < emin) emin = e; if (e > emax) emax = e; } if (emin > emax) emin = emax; /* case where all values are singular */ /* Consistency test in a reduced exponent range. Doing it for the first 10 samples and for prec == p1 (which has some special cases) should be sufficient. */ if (ctrt <= 10 || prec == p1) { mpfr_set_emin (emin); mpfr_set_emax (emax); #ifdef DEBUG_TGENERIC /* Useful information in case of assertion failure. */ printf ("tgeneric: reduced exponent range [%" MPFR_EXP_FSPEC "d,%" MPFR_EXP_FSPEC "d]\n", (mpfr_eexp_t) emin, (mpfr_eexp_t) emax); #endif mpfr_clear_flags (); #if defined(TWO_ARGS) inexact = TEST_FUNCTION (w, x, u, rnd); #elif defined(DOUBLE_ARG1) inexact = TEST_FUNCTION (w, d, x, rnd); #elif defined(DOUBLE_ARG2) inexact = TEST_FUNCTION (w, x, d, rnd); #elif defined(ULONG_ARG1) inexact = TEST_FUNCTION (w, i, x, rnd); #elif defined(ULONG_ARG2) inexact = TEST_FUNCTION (w, x, i, rnd); #else inexact = TEST_FUNCTION (w, x, rnd); #endif flags = __gmpfr_flags; mpfr_set_emin (oemin); mpfr_set_emax (oemax); if (! (SAME_VAL (w, y) && SAME_SIGN (inexact, compare) && flags == oldflags)) { printf ("tgeneric: error for " MAKE_STR(TEST_FUNCTION) ", reduced exponent range [%" MPFR_EXP_FSPEC "d,%" MPFR_EXP_FSPEC "d] on:\n", (mpfr_eexp_t) emin, (mpfr_eexp_t) emax); printf ("x = "); mpfr_dump (x); #if defined(TWO_ARGS_ALL) printf ("u = "); mpfr_dump (u); #endif printf ("yprec = %u, rnd_mode = %s\n", (unsigned int) yprec, mpfr_print_rnd_mode (rnd)); printf ("Expected:\n y = "); mpfr_dump (y); printf (" inex = %d, flags =", compare); flags_out (oldflags); printf ("Got:\n w = "); mpfr_dump (w); printf (" inex = %d, flags =", inexact); flags_out (flags); exit (1); } } __gmpfr_flags = oldflags; /* restore the flags */ } if (MPFR_IS_SINGULAR (y)) { if (MPFR_IS_NAN (y) || mpfr_nanflag_p ()) TGENERIC_CHECK ("bad NaN flag", MPFR_IS_NAN (y) && mpfr_nanflag_p ()); else if (MPFR_IS_INF (y)) { TGENERIC_CHECK ("bad overflow flag", (compare != 0) ^ (mpfr_overflow_p () == 0)); TGENERIC_CHECK ("bad divide-by-zero flag", (compare == 0 && !infinite_input) ^ (mpfr_divby0_p () == 0)); } else if (MPFR_IS_ZERO (y)) TGENERIC_CHECK ("bad underflow flag", (compare != 0) ^ (mpfr_underflow_p () == 0)); } else if (mpfr_divby0_p ()) { TGENERIC_CHECK ("both overflow and divide-by-zero", ! mpfr_overflow_p ()); TGENERIC_CHECK ("both underflow and divide-by-zero", ! mpfr_underflow_p ()); TGENERIC_CHECK ("bad compare value (divide-by-zero)", compare == 0); } else if (mpfr_overflow_p ()) { TGENERIC_CHECK ("both underflow and overflow", ! mpfr_underflow_p ()); TGENERIC_CHECK ("bad compare value (overflow)", compare != 0); mpfr_nexttoinf (y); TGENERIC_CHECK ("should have been max MPFR number (overflow)", MPFR_IS_INF (y)); } else if (mpfr_underflow_p ()) { TGENERIC_CHECK ("bad compare value (underflow)", compare != 0); mpfr_nexttozero (y); TGENERIC_CHECK ("should have been min MPFR number (underflow)", MPFR_IS_ZERO (y)); } else if (mpfr_can_round (y, yprec, rnd, rnd, prec)) { ctrn++; mpfr_set (t, y, rnd); /* Risk of failures are known when some flags are already set before the function call. Do not set the erange flag, as it will remain set after the function call and no checks are performed in such a case (see the mpfr_erangeflag_p test below). */ if (randlimb () & 1) __gmpfr_flags = MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE; #ifdef DEBUG_TGENERIC TGENERIC_INFO (TEST_FUNCTION, MPFR_PREC (z)); #endif /* Let's increase the precision of the inputs in a random way. In most cases, this doesn't make any difference, but for the mpfr_fmod bug fixed in r6230, this triggers the bug. */ mpfr_prec_round (x, mpfr_get_prec (x) + (randlimb () & 15), MPFR_RNDN); #if defined(TWO_ARGS) mpfr_prec_round (u, mpfr_get_prec (u) + (randlimb () & 15), MPFR_RNDN); inexact = TEST_FUNCTION (z, x, u, rnd); #elif defined(DOUBLE_ARG1) inexact = TEST_FUNCTION (z, d, x, rnd); #elif defined(DOUBLE_ARG2) inexact = TEST_FUNCTION (z, x, d, rnd); #elif defined(ULONG_ARG1) inexact = TEST_FUNCTION (z, i, x, rnd); #elif defined(ULONG_ARG2) inexact = TEST_FUNCTION (z, x, i, rnd); #else inexact = TEST_FUNCTION (z, x, rnd); #endif if (mpfr_erangeflag_p ()) goto next_n; if (! mpfr_equal_p (t, z)) { printf ("tgeneric: results differ for " MAKE_STR(TEST_FUNCTION) " on\n x = "); mpfr_dump (x); #if defined(TWO_ARGS_ALL) printf (" u = "); mpfr_dump (u); #endif printf (" prec = %u, rnd_mode = %s\n", (unsigned int) prec, mpfr_print_rnd_mode (rnd)); printf ("Got "); mpfr_dump (z); printf ("Expected "); mpfr_dump (t); printf ("Approx "); mpfr_dump (y); exit (1); } compare2 = mpfr_cmp (t, y); /* if rounding to nearest, cannot know the sign of t - f(x) because of composed rounding: y = o(f(x)) and t = o(y) */ if (compare * compare2 >= 0) compare = compare + compare2; else compare = inexact; /* cannot determine sign(t-f(x)) */ if (! SAME_SIGN (inexact, compare)) { printf ("Wrong inexact flag for rnd=%s: expected %d, got %d" "\n", mpfr_print_rnd_mode (rnd), compare, inexact); printf ("x = "); mpfr_dump (x); #if defined(TWO_ARGS_ALL) printf ("u = "); mpfr_dump (u); #endif printf ("y = "); mpfr_dump (y); printf ("t = "); mpfr_dump (t); exit (1); } } else if (getenv ("MPFR_SUSPICIOUS_OVERFLOW") != NULL) { /* For developers only! */ MPFR_ASSERTN (MPFR_IS_PURE_FP (y)); mpfr_nexttoinf (y); if (MPFR_IS_INF (y) && MPFR_IS_LIKE_RNDZ (rnd, MPFR_IS_NEG (y)) && !mpfr_overflow_p () && TGENERIC_SO_TEST) { printf ("Possible bug! |y| is the maximum finite number " "and has been obtained when\nrounding toward zero" " (%s). Thus there is a very probable overflow,\n" "but the overflow flag is not set!\n", mpfr_print_rnd_mode (rnd)); printf ("x = "); mpfr_dump (x); #if defined(TWO_ARGS_ALL) printf ("u = "); mpfr_dump (u); #endif exit (1); } } next_n: /* In case the exponent range has been changed by tests_default_random() or for special values... */ mpfr_set_emin (old_emin); mpfr_set_emax (old_emax); } } #ifndef TGENERIC_NOWARNING if (3 * ctrn < 2 * ctrt) printf ("Warning! Too few normal cases in generic tests (%lu / %lu)\n", ctrn, ctrt); #endif mpfr_clears (x, y, z, t, w, (mpfr_ptr) 0); #if defined(TWO_ARGS_ALL) mpfr_clear (u); #endif }
static void test_2exp (void) { mpfr_t x; int res; mpfr_init2 (x, 32); mpfr_set_ui_2exp (x, 1, 0, MPFR_RNDN); if (mpfr_cmp_ui(x, 1)) ERROR("(1U,0)"); mpfr_set_ui_2exp (x, 1024, -10, MPFR_RNDN); if (mpfr_cmp_ui(x, 1)) ERROR("(1024U,-10)"); mpfr_set_ui_2exp (x, 1024, 10, MPFR_RNDN); if (mpfr_cmp_ui(x, 1024*1024)) ERROR("(1024U,+10)"); mpfr_set_si_2exp (x, -1024L * 1024L, -10, MPFR_RNDN); if (mpfr_cmp_si(x, -1024)) ERROR("(1M,-10)"); mpfr_set_ui_2exp (x, 0x92345678, 16, MPFR_RNDN); if (mpfr_cmp_str (x, "92345678@4", 16, MPFR_RNDN)) ERROR("(x92345678U,+16)"); mpfr_set_si_2exp (x, -0x1ABCDEF0, -256, MPFR_RNDN); if (mpfr_cmp_str (x, "-1ABCDEF0@-64", 16, MPFR_RNDN)) ERROR("(-x1ABCDEF0,-256)"); mpfr_set_prec (x, 2); res = mpfr_set_si_2exp (x, 7, 10, MPFR_RNDU); if (mpfr_cmp_ui (x, 1<<13) || res <= 0) ERROR ("Prec 2 + si_2exp"); res = mpfr_set_ui_2exp (x, 7, 10, MPFR_RNDU); if (mpfr_cmp_ui (x, 1<<13) || res <= 0) ERROR ("Prec 2 + ui_2exp"); mpfr_clear_flags (); mpfr_set_ui_2exp (x, 17, MPFR_EMAX_MAX, MPFR_RNDN); if (!mpfr_inf_p (x) || MPFR_IS_NEG (x)) ERROR ("mpfr_set_ui_2exp and overflow (bad result)"); if (!mpfr_overflow_p ()) ERROR ("mpfr_set_ui_2exp and overflow (overflow flag not set)"); mpfr_clear_flags (); mpfr_set_si_2exp (x, 17, MPFR_EMAX_MAX, MPFR_RNDN); if (!mpfr_inf_p (x) || MPFR_IS_NEG (x)) ERROR ("mpfr_set_si_2exp (pos) and overflow (bad result)"); if (!mpfr_overflow_p ()) ERROR ("mpfr_set_si_2exp (pos) and overflow (overflow flag not set)"); mpfr_clear_flags (); mpfr_set_si_2exp (x, -17, MPFR_EMAX_MAX, MPFR_RNDN); if (!mpfr_inf_p (x) || MPFR_IS_POS (x)) ERROR ("mpfr_set_si_2exp (neg) and overflow (bad result)"); if (!mpfr_overflow_p ()) ERROR ("mpfr_set_si_2exp (neg) and overflow (overflow flag not set)"); mpfr_clear (x); }
static void special_overflow (void) { mpfr_t x, y; int i; mpfr_exp_t emin, emax; emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_clear_overflow (); set_emin (-125); set_emax (128); mpfr_init2 (x, 24); mpfr_init2 (y, 24); mpfr_set_str_binary (x, "0.101100100000000000110100E7"); i = mpfr_tanh (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1) || i != 1) { printf("Overflow error (1). i=%d\ny=", i); mpfr_dump (y); exit (1); } MPFR_ASSERTN (!mpfr_overflow_p ()); i = mpfr_tanh (y, x, MPFR_RNDZ); if (mpfr_cmp_str (y, "0.111111111111111111111111E0", 2, MPFR_RNDN) || i != -1) { printf("Overflow error (2).i=%d\ny=", i); mpfr_dump (y); exit (1); } MPFR_ASSERTN (!mpfr_overflow_p ()); set_emin (emin); set_emax (emax); mpfr_set_str_binary (x, "0.1E1000000000"); i = mpfr_tanh (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1) || i != 1) { printf("Overflow error (3). i=%d\ny=", i); mpfr_dump (y); exit (1); } MPFR_ASSERTN (!mpfr_overflow_p ()); mpfr_set_str_binary (x, "-0.1E1000000000"); i = mpfr_tanh (y, x, MPFR_RNDU); if (mpfr_cmp_str (y, "-0.111111111111111111111111E0", 2, MPFR_RNDN) || i != 1) { printf("Overflow error (4). i=%d\ny=", i); mpfr_dump (y); exit (1); } mpfr_clear (y); mpfr_clear (x); }
static void test_overflow2 (void) { mpfr_t x, y, z, r; int i, inex, rnd, err = 0; mpfr_inits2 (8, x, y, z, r, (void *) 0); MPFR_SET_POS (x); mpfr_setmin (x, mpfr_get_emax ()); /* x = 0.1@emax */ mpfr_set_si (y, -2, GMP_RNDN); /* y = -2 */ /* The intermediate multiplication x * y will overflow. */ for (i = -9; i <= 9; i++) RND_LOOP (rnd) { int inf, overflow; inf = rnd == GMP_RNDN || rnd == GMP_RNDD; overflow = inf || i <= 0; inex = mpfr_set_si_2exp (z, i, mpfr_get_emin (), GMP_RNDN); MPFR_ASSERTN (inex == 0); mpfr_clear_flags (); /* One has: x * y = -1@emax exactly (but not representable). */ inex = mpfr_fma (r, x, y, z, rnd); if (overflow ^ (mpfr_overflow_p () != 0)) { printf ("Error in test_overflow2 (i = %d, %s): wrong overflow" " flag (should be %d)\n", i, mpfr_print_rnd_mode (rnd), overflow); err = 1; } if (mpfr_nanflag_p ()) { printf ("Error in test_overflow2 (i = %d, %s): NaN flag should" " not be set\n", i, mpfr_print_rnd_mode (rnd)); err = 1; } if (mpfr_nan_p (r)) { printf ("Error in test_overflow2 (i = %d, %s): got NaN\n", i, mpfr_print_rnd_mode (rnd)); err = 1; } else if (MPFR_SIGN (r) >= 0) { printf ("Error in test_overflow2 (i = %d, %s): wrong sign " "(+ instead of -)\n", i, mpfr_print_rnd_mode (rnd)); err = 1; } else if (inf && ! mpfr_inf_p (r)) { printf ("Error in test_overflow2 (i = %d, %s): expected -Inf," " got\n", i, mpfr_print_rnd_mode (rnd)); mpfr_dump (r); err = 1; } else if (!inf && (mpfr_inf_p (r) || (mpfr_nextbelow (r), ! mpfr_inf_p (r)))) { printf ("Error in test_overflow2 (i = %d, %s): expected -MAX," " got\n", i, mpfr_print_rnd_mode (rnd)); mpfr_dump (r); err = 1; } if (inf ? inex >= 0 : inex <= 0) { printf ("Error in test_overflow2 (i = %d, %s): wrong inexact" " flag (got %d)\n", i, mpfr_print_rnd_mode (rnd), inex); err = 1; } } if (err) exit (1); mpfr_clears (x, y, z, r, (void *) 0); }
/* Test n random bad cases. A precision py in [pymin,pymax] and * a number y of precision py are chosen randomly. One computes * x = inv(y) in precision px = py + psup (rounded to nearest). * Then (in general), y is a bad case for fct in precision py (in * the directed rounding modes, but also in the rounding-to-nearest * mode for some lower precision: see data_check). * fct, inv, name: data related to the function. * pos, emin, emax: arguments for tests_default_random. */ void bad_cases (int (*fct)(FLIST), int (*inv)(FLIST), const char *name, int pos, mpfr_exp_t emin, mpfr_exp_t emax, mpfr_prec_t pymin, mpfr_prec_t pymax, mpfr_prec_t psup, int n) { mpfr_t x, y, z; char *dbgenv; int i, dbg; mpfr_exp_t old_emin, old_emax; old_emin = mpfr_get_emin (); old_emax = mpfr_get_emax (); dbgenv = getenv ("MPFR_DEBUG_BADCASES"); dbg = dbgenv != 0 ? atoi (dbgenv) : 0; /* debug level */ mpfr_inits (x, y, z, (mpfr_ptr) 0); for (i = 0; i < n; i++) { mpfr_prec_t px, py, pz; int inex; if (dbg) printf ("bad_cases: i = %d\n", i); py = pymin + (randlimb () % (pymax - pymin + 1)); mpfr_set_prec (y, py); tests_default_random (y, pos, emin, emax); if (dbg) { printf ("bad_cases: yprec =%4ld, y = ", (long) py); mpfr_out_str (stdout, 16, 0, y, MPFR_RNDN); printf ("\n"); } px = py + psup; mpfr_set_prec (x, px); mpfr_clear_flags (); inv (x, y, MPFR_RNDN); if (mpfr_nanflag_p () || mpfr_overflow_p () || mpfr_underflow_p ()) { if (dbg) printf ("bad_cases: no normal inverse\n"); goto next_i; } if (dbg > 1) { printf ("bad_cases: x = "); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); printf ("\n"); } pz = px; do { pz += 32; mpfr_set_prec (z, pz); if (fct (z, x, MPFR_RNDN) == 0) { if (dbg) printf ("bad_cases: exact case\n"); goto next_i; } if (dbg) { if (dbg > 1) { printf ("bad_cases: %s(x) ~= ", name); mpfr_out_str (stdout, 16, 0, z, MPFR_RNDN); } else { printf ("bad_cases: [MPFR_RNDZ] ~= "); mpfr_out_str (stdout, 16, 40, z, MPFR_RNDZ); } printf ("\n"); } inex = mpfr_prec_round (z, py, MPFR_RNDN); if (mpfr_nanflag_p () || mpfr_overflow_p () || mpfr_underflow_p () || ! mpfr_equal_p (z, y)) { if (dbg) printf ("bad_cases: inverse doesn't match\n"); goto next_i; } } while (inex == 0); /* We really have a bad case. */ do py--; while (py >= MPFR_PREC_MIN && mpfr_prec_round (z, py, MPFR_RNDZ) == 0); py++; /* py is now the smallest output precision such that we have a bad case in the directed rounding modes. */ if (mpfr_prec_round (y, py, MPFR_RNDZ) != 0) { printf ("Internal error for i = %d\n", i); exit (1); } if ((inex > 0 && MPFR_IS_POS (z)) || (inex < 0 && MPFR_IS_NEG (z))) { mpfr_nexttozero (y); if (mpfr_zero_p (y)) goto next_i; } if (dbg) { printf ("bad_cases: yprec =%4ld, y = ", (long) py); mpfr_out_str (stdout, 16, 0, y, MPFR_RNDN); printf ("\n"); } /* Note: y is now the expected result rounded toward zero. */ test5rm (fct, x, y, z, MPFR_RNDZ, 0, name); next_i: /* In case the exponent range has been changed by tests_default_random()... */ mpfr_set_emin (old_emin); mpfr_set_emax (old_emax); } mpfr_clears (x, y, z, (mpfr_ptr) 0); }
int mpc_exp (mpc_ptr rop, mpc_srcptr op, mpc_rnd_t rnd) { mpfr_t x, y, z; mpfr_prec_t prec; int ok = 0; int inex_re, inex_im; int saved_underflow, saved_overflow; /* special values */ if (mpfr_nan_p (mpc_realref (op)) || mpfr_nan_p (mpc_imagref (op))) /* NaNs exp(nan +i*y) = nan -i*0 if y = -0, nan +i*0 if y = +0, nan +i*nan otherwise exp(x+i*nan) = +/-0 +/-i*0 if x=-inf, +/-inf +i*nan if x=+inf, nan +i*nan otherwise */ { if (mpfr_zero_p (mpc_imagref (op))) return mpc_set (rop, op, MPC_RNDNN); if (mpfr_inf_p (mpc_realref (op))) { if (mpfr_signbit (mpc_realref (op))) return mpc_set_ui_ui (rop, 0, 0, MPC_RNDNN); else { mpfr_set_inf (mpc_realref (rop), +1); mpfr_set_nan (mpc_imagref (rop)); return MPC_INEX(0, 0); /* Inf/NaN are exact */ } } mpfr_set_nan (mpc_realref (rop)); mpfr_set_nan (mpc_imagref (rop)); return MPC_INEX(0, 0); /* NaN is exact */ } if (mpfr_zero_p (mpc_imagref(op))) /* special case when the input is real exp(x-i*0) = exp(x) -i*0, even if x is NaN exp(x+i*0) = exp(x) +i*0, even if x is NaN */ { inex_re = mpfr_exp (mpc_realref(rop), mpc_realref(op), MPC_RND_RE(rnd)); inex_im = mpfr_set (mpc_imagref(rop), mpc_imagref(op), MPC_RND_IM(rnd)); return MPC_INEX(inex_re, inex_im); } if (mpfr_zero_p (mpc_realref (op))) /* special case when the input is imaginary */ { inex_re = mpfr_cos (mpc_realref (rop), mpc_imagref (op), MPC_RND_RE(rnd)); inex_im = mpfr_sin (mpc_imagref (rop), mpc_imagref (op), MPC_RND_IM(rnd)); return MPC_INEX(inex_re, inex_im); } if (mpfr_inf_p (mpc_realref (op))) /* real part is an infinity, exp(-inf +i*y) = 0*(cos y +i*sin y) exp(+inf +i*y) = +/-inf +i*nan if y = +/-inf +inf*(cos y +i*sin y) if 0 < |y| < inf */ { mpfr_t n; mpfr_init2 (n, 2); if (mpfr_signbit (mpc_realref (op))) mpfr_set_ui (n, 0, GMP_RNDN); else mpfr_set_inf (n, +1); if (mpfr_inf_p (mpc_imagref (op))) { inex_re = mpfr_set (mpc_realref (rop), n, GMP_RNDN); if (mpfr_signbit (mpc_realref (op))) inex_im = mpfr_set (mpc_imagref (rop), n, GMP_RNDN); else { mpfr_set_nan (mpc_imagref (rop)); inex_im = 0; /* NaN is exact */ } } else { mpfr_t c, s; mpfr_init2 (c, 2); mpfr_init2 (s, 2); mpfr_sin_cos (s, c, mpc_imagref (op), GMP_RNDN); inex_re = mpfr_copysign (mpc_realref (rop), n, c, GMP_RNDN); inex_im = mpfr_copysign (mpc_imagref (rop), n, s, GMP_RNDN); mpfr_clear (s); mpfr_clear (c); } mpfr_clear (n); return MPC_INEX(inex_re, inex_im); } if (mpfr_inf_p (mpc_imagref (op))) /* real part is finite non-zero number, imaginary part is an infinity */ { mpfr_set_nan (mpc_realref (rop)); mpfr_set_nan (mpc_imagref (rop)); return MPC_INEX(0, 0); /* NaN is exact */ } /* from now on, both parts of op are regular numbers */ prec = MPC_MAX_PREC(rop) + MPC_MAX (MPC_MAX (-mpfr_get_exp (mpc_realref (op)), 0), -mpfr_get_exp (mpc_imagref (op))); /* When op is close to 0, then exp is close to 1+Re(op), while cos is close to 1-Im(op); to decide on the ternary value of exp*cos, we need a high enough precision so that none of exp or cos is computed as 1. */ mpfr_init2 (x, 2); mpfr_init2 (y, 2); mpfr_init2 (z, 2); /* save the underflow or overflow flags from MPFR */ saved_underflow = mpfr_underflow_p (); saved_overflow = mpfr_overflow_p (); do { prec += mpc_ceil_log2 (prec) + 5; mpfr_set_prec (x, prec); mpfr_set_prec (y, prec); mpfr_set_prec (z, prec); /* FIXME: x may overflow so x.y does overflow too, while Re(exp(op)) could be represented in the precision of rop. */ mpfr_clear_overflow (); mpfr_clear_underflow (); mpfr_exp (x, mpc_realref(op), GMP_RNDN); /* error <= 0.5ulp */ mpfr_sin_cos (z, y, mpc_imagref(op), GMP_RNDN); /* errors <= 0.5ulp */ mpfr_mul (y, y, x, GMP_RNDN); /* error <= 2ulp */ ok = mpfr_overflow_p () || mpfr_zero_p (x) || mpfr_can_round (y, prec - 2, GMP_RNDN, GMP_RNDZ, MPC_PREC_RE(rop) + (MPC_RND_RE(rnd) == GMP_RNDN)); if (ok) /* compute imaginary part */ { mpfr_mul (z, z, x, GMP_RNDN); ok = mpfr_overflow_p () || mpfr_zero_p (x) || mpfr_can_round (z, prec - 2, GMP_RNDN, GMP_RNDZ, MPC_PREC_IM(rop) + (MPC_RND_IM(rnd) == GMP_RNDN)); } } while (ok == 0); inex_re = mpfr_set (mpc_realref(rop), y, MPC_RND_RE(rnd)); inex_im = mpfr_set (mpc_imagref(rop), z, MPC_RND_IM(rnd)); if (mpfr_overflow_p ()) { /* overflow in real exponential, inex is sign of infinite result */ inex_re = mpfr_sgn (y); inex_im = mpfr_sgn (z); } else if (mpfr_underflow_p ()) { /* underflow in real exponential, inex is opposite of sign of 0 result */ inex_re = (mpfr_signbit (y) ? +1 : -1); inex_im = (mpfr_signbit (z) ? +1 : -1); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); /* restore underflow and overflow flags from MPFR */ if (saved_underflow) mpfr_set_underflow (); if (saved_overflow) mpfr_set_overflow (); return MPC_INEX(inex_re, inex_im); }
static void check_max(void) { mpfr_t xx, yy, zz; mpfr_exp_t emin; mpfr_init2(xx, 4); mpfr_init2(yy, 4); mpfr_init2(zz, 4); mpfr_set_str1 (xx, "0.68750"); mpfr_mul_2si(xx, xx, MPFR_EMAX_DEFAULT/2, MPFR_RNDN); mpfr_set_str1 (yy, "0.68750"); mpfr_mul_2si(yy, yy, MPFR_EMAX_DEFAULT - MPFR_EMAX_DEFAULT/2 + 1, MPFR_RNDN); mpfr_clear_flags(); test_mul(zz, xx, yy, MPFR_RNDU); if (!(mpfr_overflow_p() && MPFR_IS_INF(zz))) { printf("check_max failed (should be an overflow)\n"); exit(1); } mpfr_clear_flags(); test_mul(zz, xx, yy, MPFR_RNDD); if (mpfr_overflow_p() || MPFR_IS_INF(zz)) { printf("check_max failed (should NOT be an overflow)\n"); exit(1); } mpfr_set_str1 (xx, "0.93750"); mpfr_mul_2si(xx, xx, MPFR_EMAX_DEFAULT, MPFR_RNDN); if (!(MPFR_IS_FP(xx) && MPFR_IS_FP(zz))) { printf("check_max failed (internal error)\n"); exit(1); } if (mpfr_cmp(xx, zz) != 0) { printf("check_max failed: got "); mpfr_out_str(stdout, 2, 0, zz, MPFR_RNDZ); printf(" instead of "); mpfr_out_str(stdout, 2, 0, xx, MPFR_RNDZ); printf("\n"); exit(1); } /* check underflow */ emin = mpfr_get_emin (); set_emin (0); mpfr_set_str_binary (xx, "0.1E0"); mpfr_set_str_binary (yy, "0.1E0"); test_mul (zz, xx, yy, MPFR_RNDN); /* exact result is 0.1E-1, which should round to 0 */ MPFR_ASSERTN(mpfr_cmp_ui (zz, 0) == 0 && MPFR_IS_POS(zz)); set_emin (emin); /* coverage test for mpfr_powerof2_raw */ emin = mpfr_get_emin (); set_emin (0); mpfr_set_prec (xx, mp_bits_per_limb + 1); mpfr_set_str_binary (xx, "0.1E0"); mpfr_nextabove (xx); mpfr_set_str_binary (yy, "0.1E0"); test_mul (zz, xx, yy, MPFR_RNDN); /* exact result is just above 0.1E-1, which should round to minfloat */ MPFR_ASSERTN(mpfr_cmp (zz, yy) == 0); set_emin (emin); mpfr_clear(xx); mpfr_clear(yy); mpfr_clear(zz); }
static void test_generic (mp_prec_t p0, mp_prec_t p1, unsigned int N) { mp_prec_t prec, xprec, yprec; mpfr_t x, y, z, t; #ifdef TWO_ARGS mpfr_t u; #elif defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_t u; double d; #endif mp_rnd_t rnd; int inexact, compare, compare2; unsigned int n; unsigned long ctrt = 0, ctrn = 0; mp_exp_t old_emin, old_emax; old_emin = mpfr_get_emin (); old_emax = mpfr_get_emax (); mpfr_init (x); mpfr_init (y); mpfr_init (z); mpfr_init (t); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_init (u); #endif /* generic test */ for (prec = p0; prec <= p1; prec++) { mpfr_set_prec (z, prec); mpfr_set_prec (t, prec); yprec = prec + 10; mpfr_set_prec (y, yprec); /* Note: in precision p1, we test 4 special cases. */ for (n = 0; n < (prec == p1 ? N + 4 : N); n++) { xprec = prec; if (randlimb () & 1) { xprec *= (double) randlimb () / MP_LIMB_T_MAX; if (xprec < MPFR_PREC_MIN) xprec = MPFR_PREC_MIN; } mpfr_set_prec (x, xprec); #ifdef TWO_ARGS mpfr_set_prec (u, xprec); #elif defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_set_prec (u, IEEE_DBL_MANT_DIG); #endif if (n > 3 || prec < p1) { #if defined(RAND_FUNCTION) RAND_FUNCTION (x); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) RAND_FUNCTION (u); #endif #else tests_default_random (x, TEST_RANDOM_POS, TEST_RANDOM_EMIN, TEST_RANDOM_EMAX); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) tests_default_random (u, TEST_RANDOM_POS2, TEST_RANDOM_EMIN, TEST_RANDOM_EMAX); #endif #endif } else { /* Special cases tested in precision p1 if n <= 3. They are useful really in the extended exponent range. */ set_emin (MPFR_EMIN_MIN); set_emax (MPFR_EMAX_MAX); if (n <= 1) { mpfr_set_si (x, n == 0 ? 1 : -1, GMP_RNDN); mpfr_set_exp (x, mpfr_get_emin ()); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_set_si (u, randlimb () % 2 == 0 ? 1 : -1, GMP_RNDN); mpfr_set_exp (u, mpfr_get_emin ()); #endif } else /* 2 <= n <= 3 */ { if (getenv ("MPFR_CHECK_MAX") == NULL) goto next_n; mpfr_set_si (x, n == 0 ? 1 : -1, GMP_RNDN); mpfr_setmax (x, REDUCE_EMAX); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_set_si (u, randlimb () % 2 == 0 ? 1 : -1, GMP_RNDN); mpfr_setmax (u, mpfr_get_emax ()); #endif } } rnd = RND_RAND (); mpfr_clear_flags (); #ifdef DEBUG_TGENERIC TGENERIC_INFO (TEST_FUNCTION, MPFR_PREC (y)); #endif #if defined(TWO_ARGS) compare = TEST_FUNCTION (y, x, u, rnd); #elif defined(DOUBLE_ARG1) d = mpfr_get_d (u, rnd); compare = TEST_FUNCTION (y, d, x, rnd); #elif defined(DOUBLE_ARG2) d = mpfr_get_d (u, rnd); compare = TEST_FUNCTION (y, x, d, rnd); #else compare = TEST_FUNCTION (y, x, rnd); #endif TGENERIC_CHECK ("Bad inexact flag", (compare != 0) ^ (mpfr_inexflag_p () == 0)); ctrt++; if (MPFR_IS_SINGULAR (y)) { if (MPFR_IS_NAN (y) || mpfr_nanflag_p ()) TGENERIC_CHECK ("Bad NaN flag", MPFR_IS_NAN (y) && mpfr_nanflag_p ()); else if (MPFR_IS_INF (y)) TGENERIC_CHECK ("Bad overflow flag", (compare != 0) ^ (mpfr_overflow_p () == 0)); else if (MPFR_IS_ZERO (y)) TGENERIC_CHECK ("Bad underflow flag", (compare != 0) ^ (mpfr_underflow_p () == 0)); } else if (mpfr_overflow_p ()) { TGENERIC_CHECK ("Bad compare value (overflow)", compare != 0); mpfr_nexttoinf (y); TGENERIC_CHECK ("Should have been max MPFR number", MPFR_IS_INF (y)); } else if (mpfr_underflow_p ()) { TGENERIC_CHECK ("Bad compare value (underflow)", compare != 0); mpfr_nexttozero (y); TGENERIC_CHECK ("Should have been min MPFR number", MPFR_IS_ZERO (y)); } else if (mpfr_can_round (y, yprec, rnd, rnd, prec)) { ctrn++; mpfr_set (t, y, rnd); /* Risk of failures are known when some flags are already set before the function call. Do not set the erange flag, as it will remain set after the function call and no checks are performed in such a case (see the mpfr_erangeflag_p test below). */ if (randlimb () & 1) __gmpfr_flags = MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE; #ifdef DEBUG_TGENERIC TGENERIC_INFO (TEST_FUNCTION, MPFR_PREC (z)); #endif /* Let's increase the precision of the inputs in a random way. In most cases, this doesn't make any difference, but this triggers the mpfr_fmod bug fixed in r6235. */ mpfr_prec_round (x, mpfr_get_prec (x) + (randlimb () & 15), GMP_RNDN); #if defined(TWO_ARGS) mpfr_prec_round (u, mpfr_get_prec (u) + (randlimb () & 15), GMP_RNDN); inexact = TEST_FUNCTION (z, x, u, rnd); #elif defined(DOUBLE_ARG1) inexact = TEST_FUNCTION (z, d, x, rnd); #elif defined(DOUBLE_ARG2) inexact = TEST_FUNCTION (z, x, d, rnd); #else inexact = TEST_FUNCTION (z, x, rnd); #endif if (mpfr_erangeflag_p ()) goto next_n; if (mpfr_nan_p (z) || mpfr_cmp (t, z) != 0) { printf ("results differ for x="); mpfr_out_str (stdout, 2, xprec, x, GMP_RNDN); #ifdef TWO_ARGS printf ("\nu="); mpfr_out_str (stdout, 2, xprec, u, GMP_RNDN); #elif defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) printf ("\nu="); mpfr_out_str (stdout, 2, IEEE_DBL_MANT_DIG, u, GMP_RNDN); #endif printf (" prec=%u rnd_mode=%s\n", (unsigned) prec, mpfr_print_rnd_mode (rnd)); printf ("got "); mpfr_out_str (stdout, 2, prec, z, GMP_RNDN); puts (""); printf ("expected "); mpfr_out_str (stdout, 2, prec, t, GMP_RNDN); puts (""); printf ("approx "); mpfr_print_binary (y); puts (""); exit (1); } compare2 = mpfr_cmp (t, y); /* if rounding to nearest, cannot know the sign of t - f(x) because of composed rounding: y = o(f(x)) and t = o(y) */ if (compare * compare2 >= 0) compare = compare + compare2; else compare = inexact; /* cannot determine sign(t-f(x)) */ if (((inexact == 0) && (compare != 0)) || ((inexact > 0) && (compare <= 0)) || ((inexact < 0) && (compare >= 0))) { printf ("Wrong inexact flag for rnd=%s: expected %d, got %d" "\n", mpfr_print_rnd_mode (rnd), compare, inexact); printf ("x="); mpfr_print_binary (x); puts (""); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) printf ("u="); mpfr_print_binary (u); puts (""); #endif printf ("y="); mpfr_print_binary (y); puts (""); printf ("t="); mpfr_print_binary (t); puts (""); exit (1); } } else if (getenv ("MPFR_SUSPICIOUS_OVERFLOW") != NULL) { /* For developers only! */ MPFR_ASSERTN (MPFR_IS_PURE_FP (y)); mpfr_nexttoinf (y); if (MPFR_IS_INF (y) && MPFR_IS_LIKE_RNDZ (rnd, MPFR_IS_NEG (y)) && !mpfr_overflow_p ()) { printf ("Possible bug! |y| is the maximum finite number " "and has been obtained when\nrounding toward zero" " (%s). Thus there is a very probable overflow,\n" "but the overflow flag is not set!\n", mpfr_print_rnd_mode (rnd)); printf ("x="); mpfr_print_binary (x); puts (""); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) printf ("u="); mpfr_print_binary (u); puts (""); #endif exit (1); } } next_n: /* In case the exponent range has been changed by tests_default_random() or for special values... */ mpfr_set_emin (old_emin); mpfr_set_emax (old_emax); } } #ifndef TGENERIC_NOWARNING if (3 * ctrn < 2 * ctrt) printf ("Warning! Too few normal cases in generic tests (%lu / %lu)\n", ctrn, ctrt); #endif mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (t); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_clear (u); #endif }
int mpc_div (mpc_ptr a, mpc_srcptr b, mpc_srcptr c, mpc_rnd_t rnd) { int ok_re = 0, ok_im = 0; mpc_t res, c_conj; mpfr_t q; mpfr_prec_t prec; int inex, inexact_prod, inexact_norm, inexact_re, inexact_im, loops = 0; int underflow_norm, overflow_norm, underflow_prod, overflow_prod; int underflow_re = 0, overflow_re = 0, underflow_im = 0, overflow_im = 0; mpfr_rnd_t rnd_re = MPC_RND_RE (rnd), rnd_im = MPC_RND_IM (rnd); int saved_underflow, saved_overflow; int tmpsgn; mpfr_exp_t e, emin, emax, emid; /* for scaling of exponents */ mpc_t b_scaled, c_scaled; mpfr_t b_re, b_im, c_re, c_im; /* According to the C standard G.3, there are three types of numbers: */ /* finite (both parts are usual real numbers; contains 0), infinite */ /* (at least one part is a real infinity) and all others; the latter */ /* are numbers containing a nan, but no infinity, and could reasonably */ /* be called nan. */ /* By G.5.1.4, infinite/finite=infinite; finite/infinite=0; */ /* all other divisions that are not finite/finite return nan+i*nan. */ /* Division by 0 could be handled by the following case of division by */ /* a real; we handle it separately instead. */ if (mpc_zero_p (c)) /* both Re(c) and Im(c) are zero */ return mpc_div_zero (a, b, c, rnd); else if (mpc_inf_p (b) && mpc_fin_p (c)) /* either Re(b) or Im(b) is infinite and both Re(c) and Im(c) are ordinary */ return mpc_div_inf_fin (a, b, c); else if (mpc_fin_p (b) && mpc_inf_p (c)) return mpc_div_fin_inf (a, b, c); else if (!mpc_fin_p (b) || !mpc_fin_p (c)) { mpc_set_nan (a); return MPC_INEX (0, 0); } else if (mpfr_zero_p(mpc_imagref(c))) return mpc_div_real (a, b, c, rnd); else if (mpfr_zero_p(mpc_realref(c))) return mpc_div_imag (a, b, c, rnd); prec = MPC_MAX_PREC(a); mpc_init2 (res, 2); mpfr_init (q); /* compute scaling of exponents: none of Re(c) and Im(c) can be zero, but one of Re(b) or Im(b) could be zero */ e = mpfr_get_exp (mpc_realref (c)); emin = emax = e; e = mpfr_get_exp (mpc_imagref (c)); if (e > emax) emax = e; else if (e < emin) emin = e; if (!mpfr_zero_p (mpc_realref (b))) { e = mpfr_get_exp (mpc_realref (b)); if (e > emax) emax = e; else if (e < emin) emin = e; } if (!mpfr_zero_p (mpc_imagref (b))) { e = mpfr_get_exp (mpc_imagref (b)); if (e > emax) emax = e; else if (e < emin) emin = e; } /* all input exponents are in [emin, emax] */ emid = emin / 2 + emax / 2; /* scale the inputs */ b_re[0] = mpc_realref (b)[0]; if (!mpfr_zero_p (mpc_realref (b))) MPFR_EXP(b_re) = MPFR_EXP(mpc_realref (b)) - emid; b_im[0] = mpc_imagref (b)[0]; if (!mpfr_zero_p (mpc_imagref (b))) MPFR_EXP(b_im) = MPFR_EXP(mpc_imagref (b)) - emid; c_re[0] = mpc_realref (c)[0]; MPFR_EXP(c_re) = MPFR_EXP(mpc_realref (c)) - emid; c_im[0] = mpc_imagref (c)[0]; MPFR_EXP(c_im) = MPFR_EXP(mpc_imagref (c)) - emid; /* create the scaled inputs without allocating new memory */ mpc_realref (b_scaled)[0] = b_re[0]; mpc_imagref (b_scaled)[0] = b_im[0]; mpc_realref (c_scaled)[0] = c_re[0]; mpc_imagref (c_scaled)[0] = c_im[0]; /* create the conjugate of c in c_conj without allocating new memory */ mpc_realref (c_conj)[0] = mpc_realref (c_scaled)[0]; mpc_imagref (c_conj)[0] = mpc_imagref (c_scaled)[0]; MPFR_CHANGE_SIGN (mpc_imagref (c_conj)); /* save the underflow or overflow flags from MPFR */ saved_underflow = mpfr_underflow_p (); saved_overflow = mpfr_overflow_p (); do { loops ++; prec += loops <= 2 ? mpc_ceil_log2 (prec) + 5 : prec / 2; mpc_set_prec (res, prec); mpfr_set_prec (q, prec); /* first compute norm(c_scaled) */ mpfr_clear_underflow (); mpfr_clear_overflow (); inexact_norm = mpc_norm (q, c_scaled, MPFR_RNDU); underflow_norm = mpfr_underflow_p (); overflow_norm = mpfr_overflow_p (); if (underflow_norm) mpfr_set_ui (q, 0ul, MPFR_RNDN); /* to obtain divisions by 0 later on */ /* now compute b_scaled*conjugate(c_scaled) */ mpfr_clear_underflow (); mpfr_clear_overflow (); inexact_prod = mpc_mul (res, b_scaled, c_conj, MPC_RNDZZ); inexact_re = MPC_INEX_RE (inexact_prod); inexact_im = MPC_INEX_IM (inexact_prod); underflow_prod = mpfr_underflow_p (); overflow_prod = mpfr_overflow_p (); /* unfortunately, does not distinguish between under-/overflow in real or imaginary parts hopefully, the side-effects of mpc_mul do indeed raise the mpfr exceptions */ if (overflow_prod) { /* FIXME: in case overflow_norm is also true, the code below is wrong, since the after division by the norm, we might end up with finite real and/or imaginary parts. A workaround would be to scale the inputs (in case the exponents are within the same range). */ int isinf = 0; /* determine if the real part of res is the maximum or the minimum representable number */ tmpsgn = mpfr_sgn (mpc_realref(res)); if (tmpsgn > 0) { mpfr_nextabove (mpc_realref(res)); isinf = mpfr_inf_p (mpc_realref(res)); mpfr_nextbelow (mpc_realref(res)); } else if (tmpsgn < 0) { mpfr_nextbelow (mpc_realref(res)); isinf = mpfr_inf_p (mpc_realref(res)); mpfr_nextabove (mpc_realref(res)); } if (isinf) { mpfr_set_inf (mpc_realref(res), tmpsgn); overflow_re = 1; } /* same for the imaginary part */ tmpsgn = mpfr_sgn (mpc_imagref(res)); isinf = 0; if (tmpsgn > 0) { mpfr_nextabove (mpc_imagref(res)); isinf = mpfr_inf_p (mpc_imagref(res)); mpfr_nextbelow (mpc_imagref(res)); } else if (tmpsgn < 0) { mpfr_nextbelow (mpc_imagref(res)); isinf = mpfr_inf_p (mpc_imagref(res)); mpfr_nextabove (mpc_imagref(res)); } if (isinf) { mpfr_set_inf (mpc_imagref(res), tmpsgn); overflow_im = 1; } mpc_set (a, res, rnd); goto end; } /* divide the product by the norm */ if (inexact_norm == 0 && (inexact_re == 0 || inexact_im == 0)) { /* The division has good chances to be exact in at least one part. */ /* Since this can cause problems when not rounding to the nearest, */ /* we use the division code of mpfr, which handles the situation. */ mpfr_clear_underflow (); mpfr_clear_overflow (); inexact_re |= mpfr_div (mpc_realref (res), mpc_realref (res), q, MPFR_RNDZ); underflow_re = mpfr_underflow_p (); overflow_re = mpfr_overflow_p (); ok_re = !inexact_re || underflow_re || overflow_re || mpfr_can_round (mpc_realref (res), prec - 4, MPFR_RNDN, MPFR_RNDZ, MPC_PREC_RE(a) + (rnd_re == MPFR_RNDN)); if (ok_re) /* compute imaginary part */ { mpfr_clear_underflow (); mpfr_clear_overflow (); inexact_im |= mpfr_div (mpc_imagref (res), mpc_imagref (res), q, MPFR_RNDZ); underflow_im = mpfr_underflow_p (); overflow_im = mpfr_overflow_p (); ok_im = !inexact_im || underflow_im || overflow_im || mpfr_can_round (mpc_imagref (res), prec - 4, MPFR_RNDN, MPFR_RNDZ, MPC_PREC_IM(a) + (rnd_im == MPFR_RNDN)); } } else { /* The division is inexact, so for efficiency reasons we invert q */ /* only once and multiply by the inverse. */ if (mpfr_ui_div (q, 1ul, q, MPFR_RNDZ) || inexact_norm) { /* if 1/q is inexact, the approximations of the real and imaginary part below will be inexact, unless RE(res) or IM(res) is zero */ inexact_re |= !mpfr_zero_p (mpc_realref (res)); inexact_im |= !mpfr_zero_p (mpc_imagref (res)); } mpfr_clear_underflow (); mpfr_clear_overflow (); inexact_re |= mpfr_mul (mpc_realref (res), mpc_realref (res), q, MPFR_RNDZ); underflow_re = mpfr_underflow_p (); overflow_re = mpfr_overflow_p (); ok_re = !inexact_re || underflow_re || overflow_re || mpfr_can_round (mpc_realref (res), prec - 4, MPFR_RNDN, MPFR_RNDZ, MPC_PREC_RE(a) + (rnd_re == MPFR_RNDN)); if (ok_re) /* compute imaginary part */ { mpfr_clear_underflow (); mpfr_clear_overflow (); inexact_im |= mpfr_mul (mpc_imagref (res), mpc_imagref (res), q, MPFR_RNDZ); underflow_im = mpfr_underflow_p (); overflow_im = mpfr_overflow_p (); ok_im = !inexact_im || underflow_im || overflow_im || mpfr_can_round (mpc_imagref (res), prec - 4, MPFR_RNDN, MPFR_RNDZ, MPC_PREC_IM(a) + (rnd_im == MPFR_RNDN)); } } } while ((!ok_re || !ok_im) && !underflow_norm && !overflow_norm && !underflow_prod && !overflow_prod); inex = mpc_set (a, res, rnd); inexact_re = MPC_INEX_RE (inex); inexact_im = MPC_INEX_IM (inex); end: /* fix values and inexact flags in case of overflow/underflow */ /* FIXME: heuristic, certainly does not cover all cases */ if (overflow_re || (underflow_norm && !underflow_prod)) { mpfr_set_inf (mpc_realref (a), mpfr_sgn (mpc_realref (res))); inexact_re = mpfr_sgn (mpc_realref (res)); } else if (underflow_re || (overflow_norm && !overflow_prod)) { inexact_re = mpfr_signbit (mpc_realref (res)) ? 1 : -1; mpfr_set_zero (mpc_realref (a), -inexact_re); } if (overflow_im || (underflow_norm && !underflow_prod)) { mpfr_set_inf (mpc_imagref (a), mpfr_sgn (mpc_imagref (res))); inexact_im = mpfr_sgn (mpc_imagref (res)); } else if (underflow_im || (overflow_norm && !overflow_prod)) { inexact_im = mpfr_signbit (mpc_imagref (res)) ? 1 : -1; mpfr_set_zero (mpc_imagref (a), -inexact_im); } mpc_clear (res); mpfr_clear (q); /* restore underflow and overflow flags from MPFR */ if (saved_underflow) mpfr_set_underflow (); if (saved_overflow) mpfr_set_overflow (); return MPC_INEX (inexact_re, inexact_im); }