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 check_max(void) { mpfr_t xx, yy, zz; mp_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, GMP_RNDN); mpfr_set_str1 (yy, "0.68750"); mpfr_mul_2si(yy, yy, MPFR_EMAX_DEFAULT - MPFR_EMAX_DEFAULT/2 + 1, GMP_RNDN); mpfr_clear_flags(); mpfr_mul(zz, xx, yy, GMP_RNDU); if (!(mpfr_overflow_p() && MPFR_IS_INF(zz))) { printf("check_max failed (should be an overflow)\n"); exit(1); } mpfr_clear_flags(); mpfr_mul(zz, xx, yy, GMP_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, GMP_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, GMP_RNDZ); printf(" instead of "); mpfr_out_str(stdout, 2, 0, xx, GMP_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"); mpfr_mul (zz, xx, yy, GMP_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"); mpfr_mul (zz, xx, yy, GMP_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 special (void) { mpfr_t x, y; int inex; int sign; mpfr_exp_t emin, emax; mpfr_init (x); mpfr_init (y); mpfr_set_nan (x); mpfr_lgamma (y, &sign, x, MPFR_RNDN); if (!mpfr_nan_p (y)) { printf ("Error for lgamma(NaN)\n"); exit (1); } mpfr_set_inf (x, -1); mpfr_lgamma (y, &sign, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) { printf ("Error for lgamma(-Inf)\n"); exit (1); } mpfr_set_inf (x, 1); sign = -17; mpfr_lgamma (y, &sign, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0 || sign != 1) { printf ("Error for lgamma(+Inf)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); sign = -17; mpfr_lgamma (y, &sign, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0 || sign != 1) { printf ("Error for lgamma(+0)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); sign = -17; mpfr_lgamma (y, &sign, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0 || sign != -1) { printf ("Error for lgamma(-0)\n"); exit (1); } mpfr_set_ui (x, 1, MPFR_RNDN); sign = -17; mpfr_lgamma (y, &sign, x, MPFR_RNDN); if (MPFR_IS_NAN (y) || mpfr_cmp_ui (y, 0) || MPFR_IS_NEG (y) || sign != 1) { printf ("Error for lgamma(1)\n"); exit (1); } mpfr_set_si (x, -1, MPFR_RNDN); mpfr_lgamma (y, &sign, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) { printf ("Error for lgamma(-1)\n"); exit (1); } mpfr_set_ui (x, 2, MPFR_RNDN); sign = -17; mpfr_lgamma (y, &sign, x, MPFR_RNDN); if (MPFR_IS_NAN (y) || mpfr_cmp_ui (y, 0) || MPFR_IS_NEG (y) || sign != 1) { printf ("Error for lgamma(2)\n"); exit (1); } mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); #define CHECK_X1 "1.0762904832837976166" #define CHECK_Y1 "-0.039418362817587634939" mpfr_set_str (x, CHECK_X1, 10, MPFR_RNDN); sign = -17; mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_str (x, CHECK_Y1, 10, MPFR_RNDN); if (mpfr_equal_p (y, x) == 0 || sign != 1) { printf ("mpfr_lgamma("CHECK_X1") is wrong:\n" "expected "); mpfr_print_binary (x); putchar ('\n'); printf ("got "); mpfr_print_binary (y); putchar ('\n'); exit (1); } #define CHECK_X2 "9.23709516716202383435e-01" #define CHECK_Y2 "0.049010669407893718563" mpfr_set_str (x, CHECK_X2, 10, MPFR_RNDN); sign = -17; mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_str (x, CHECK_Y2, 10, MPFR_RNDN); if (mpfr_equal_p (y, x) == 0 || sign != 1) { printf ("mpfr_lgamma("CHECK_X2") is wrong:\n" "expected "); mpfr_print_binary (x); putchar ('\n'); printf ("got "); mpfr_print_binary (y); putchar ('\n'); exit (1); } mpfr_set_prec (x, 8); mpfr_set_prec (y, 175); mpfr_set_ui (x, 33, MPFR_RNDN); sign = -17; mpfr_lgamma (y, &sign, x, MPFR_RNDU); mpfr_set_prec (x, 175); mpfr_set_str_binary (x, "0.1010001100011101101011001101110010100001000001000001110011000001101100001111001001000101011011100100010101011110100111110101010100010011010010000101010111001100011000101111E7"); if (mpfr_equal_p (x, y) == 0 || sign != 1) { printf ("Error in mpfr_lgamma (1)\n"); exit (1); } mpfr_set_prec (x, 21); mpfr_set_prec (y, 8); mpfr_set_ui (y, 120, MPFR_RNDN); sign = -17; mpfr_lgamma (x, &sign, y, MPFR_RNDZ); mpfr_set_prec (y, 21); mpfr_set_str_binary (y, "0.111000101000001100101E9"); if (mpfr_equal_p (x, y) == 0 || sign != 1) { printf ("Error in mpfr_lgamma (120)\n"); printf ("Expected "); mpfr_print_binary (y); puts (""); printf ("Got "); mpfr_print_binary (x); puts (""); exit (1); } mpfr_set_prec (x, 3); mpfr_set_prec (y, 206); mpfr_set_str_binary (x, "0.110e10"); sign = -17; inex = mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_prec (x, 206); mpfr_set_str_binary (x, "0.10000111011000000011100010101001100110001110000111100011000100100110110010001011011110101001111011110110000001010100111011010000000011100110110101100111000111010011110010000100010111101010001101000110101001E13"); if (mpfr_equal_p (x, y) == 0 || sign != 1) { printf ("Error in mpfr_lgamma (768)\n"); exit (1); } if (inex >= 0) { printf ("Wrong flag for mpfr_lgamma (768)\n"); exit (1); } mpfr_set_prec (x, 4); mpfr_set_prec (y, 4); mpfr_set_str_binary (x, "0.1100E-66"); sign = -17; mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_str_binary (x, "0.1100E6"); if (mpfr_equal_p (x, y) == 0 || sign != 1) { printf ("Error for lgamma(0.1100E-66)\n"); printf ("Expected "); mpfr_dump (x); printf ("Got "); mpfr_dump (y); exit (1); } mpfr_set_prec (x, 256); mpfr_set_prec (y, 32); mpfr_set_si_2exp (x, -1, 200, MPFR_RNDN); mpfr_add_ui (x, x, 1, MPFR_RNDN); mpfr_div_2ui (x, x, 1, MPFR_RNDN); sign = -17; mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_prec (x, 32); mpfr_set_str_binary (x, "-0.10001000111011111011000010100010E207"); if (mpfr_equal_p (x, y) == 0 || sign != 1) { printf ("Error for lgamma(-2^199+0.5)\n"); printf ("Got "); mpfr_dump (y); printf ("instead of "); mpfr_dump (x); exit (1); } mpfr_set_prec (x, 256); mpfr_set_prec (y, 32); mpfr_set_si_2exp (x, -1, 200, MPFR_RNDN); mpfr_sub_ui (x, x, 1, MPFR_RNDN); mpfr_div_2ui (x, x, 1, MPFR_RNDN); sign = -17; mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_prec (x, 32); mpfr_set_str_binary (x, "-0.10001000111011111011000010100010E207"); if (mpfr_equal_p (x, y) == 0 || sign != -1) { printf ("Error for lgamma(-2^199-0.5)\n"); printf ("Got "); mpfr_dump (y); printf ("with sign %d instead of ", sign); mpfr_dump (x); printf ("with sign -1.\n"); exit (1); } mpfr_set_prec (x, 10); mpfr_set_prec (y, 10); mpfr_set_str_binary (x, "-0.1101111000E-3"); inex = mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_str_binary (x, "10.01001011"); if (mpfr_equal_p (x, y) == 0 || sign != -1 || inex >= 0) { printf ("Error for lgamma(-0.1101111000E-3)\n"); printf ("Got "); mpfr_dump (y); printf ("instead of "); mpfr_dump (x); printf ("with sign %d instead of -1 (inex=%d).\n", sign, inex); exit (1); } mpfr_set_prec (x, 18); mpfr_set_prec (y, 28); mpfr_set_str_binary (x, "-1.10001101010001101e-196"); inex = mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_prec (x, 28); mpfr_set_str_binary (x, "0.100001110110101011011010011E8"); MPFR_ASSERTN (mpfr_equal_p (x, y) && inex < 0); /* values reported by Kaveh Ghazi on 14 Jul 2007, where mpfr_lgamma() takes forever */ #define VAL1 "-0.11100001001010110111001010001001001011110100110000110E-55" #define OUT1 "100110.01000000010111001110110101110101001001100110111" #define VAL2 "-0.11100001001010110111001010001001001011110011111111100E-55" #define OUT2 "100110.0100000001011100111011010111010100100110011111" #define VAL3 "-0.11100001001010110111001010001001001001110101101010100E-55" #define OUT3 "100110.01000000010111001110110101110101001011110111011" #define VAL4 "-0.10001111110110110100100100000000001111110001001001011E-57" #define OUT4 "101000.0001010111110011101101000101111111010001100011" #define VAL5 "-0.10001111110110110100100100000000001111011111100001000E-57" #define OUT5 "101000.00010101111100111011010001011111110100111000001" #define VAL6 "-0.10001111110110110100100100000000001111011101100011001E-57" #define OUT6 "101000.0001010111110011101101000101111111010011101111" mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_str_binary (x, VAL1); mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_str_binary (x, OUT1); MPFR_ASSERTN(sign == -1 && mpfr_equal_p(x, y)); mpfr_set_str_binary (x, VAL2); mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_str_binary (x, OUT2); MPFR_ASSERTN(sign == -1 && mpfr_equal_p (x, y)); mpfr_set_str_binary (x, VAL3); mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_str_binary (x, OUT3); MPFR_ASSERTN(sign == -1 && mpfr_equal_p (x, y)); mpfr_set_str_binary (x, VAL4); mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_str_binary (x, OUT4); MPFR_ASSERTN(sign == -1 && mpfr_equal_p (x, y)); mpfr_set_str_binary (x, VAL5); mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_str_binary (x, OUT5); MPFR_ASSERTN(sign == -1 && mpfr_equal_p (x, y)); mpfr_set_str_binary (x, VAL6); mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_str_binary (x, OUT6); MPFR_ASSERTN(sign == -1 && mpfr_equal_p (x, y)); /* further test from Kaveh Ghazi */ mpfr_set_str_binary (x, "-0.10011010101001010010001110010111010111011101010111001E-53"); mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_str_binary (x, "100101.00111101101010000000101010111010001111001101111"); MPFR_ASSERTN(sign == -1 && mpfr_equal_p (x, y)); /* bug found by Kevin Rauch on 26 Oct 2007 */ emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_set_emin (-1000000000); mpfr_set_emax (1000000000); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_lgamma (x, &sign, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_get_emin () == -1000000000); MPFR_ASSERTN(mpfr_get_emax () == 1000000000); mpfr_set_emin (emin); mpfr_set_emax (emax); /* two other bugs reported by Kevin Rauch on 27 Oct 2007 */ mpfr_set_prec (x, 128); mpfr_set_prec (y, 128); mpfr_set_str_binary (x, "0.11000110011110111111110010100110000000000000000000000000000000000000000000000000000000000000000001000011000110100100110111101010E-765689"); inex = mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_str_binary (x, "10000001100100101111011011010000111010001001110000111010011000101001011111011111110011011010110100101111110111001001010100011101E-108"); MPFR_ASSERTN(inex < 0 && mpfr_cmp (y, x) == 0 && sign > 0); mpfr_set_prec (x, 128); mpfr_set_prec (y, 256); mpfr_set_str_binary (x, "0.1011111111111111100000111011111E-31871"); inex = mpfr_lgamma (y, &sign, x, MPFR_RNDN); mpfr_set_prec (x, 256); mpfr_set_str (x, "AC9729B83707E6797612D0D76DAF42B1240A677FF1B6E3783FD4E53037143B1P-237", 16, MPFR_RNDN); MPFR_ASSERTN(inex < 0 && mpfr_cmp (y, x) == 0 && sign > 0); mpfr_clear (x); mpfr_clear (y); }

inline ExponentType<BigFloat> MinExponent<BigFloat>( const BigFloat& alpha ) { return mpfr_get_emin(); }

int mpfr_urandom (mpfr_ptr rop, gmp_randstate_t rstate, mpfr_rnd_t rnd_mode) { mpfr_limb_ptr rp; mpfr_prec_t nbits; mp_size_t nlimbs; mp_size_t n; mpfr_exp_t exp; mpfr_exp_t emin; int cnt; int inex; rp = MPFR_MANT (rop); nbits = MPFR_PREC (rop); nlimbs = MPFR_LIMB_SIZE (rop); MPFR_SET_POS (rop); exp = 0; emin = mpfr_get_emin (); if (MPFR_UNLIKELY (emin > 0)) { if (rnd_mode == MPFR_RNDU || rnd_mode == MPFR_RNDA || (emin == 1 && rnd_mode == MPFR_RNDN && random_rounding_bit (rstate))) { mpfr_set_ui_2exp (rop, 1, emin - 1, rnd_mode); return +1; } else { MPFR_SET_ZERO (rop); return -1; } } /* Exponent */ #define DRAW_BITS 8 /* we draw DRAW_BITS at a time */ cnt = DRAW_BITS; MPFR_ASSERTN(DRAW_BITS <= GMP_NUMB_BITS); while (cnt == DRAW_BITS) { /* generate DRAW_BITS in rp[0] */ mpfr_rand_raw (rp, rstate, DRAW_BITS); if (MPFR_UNLIKELY (rp[0] == 0)) cnt = DRAW_BITS; else { count_leading_zeros (cnt, rp[0]); cnt -= GMP_NUMB_BITS - DRAW_BITS; } if (MPFR_UNLIKELY (exp < emin + cnt)) { /* To get here, we have been drawing more than -emin zeros in a row, then return 0 or the smallest representable positive number. The rounding to nearest mode is subtle: If exp - cnt == emin - 1, the rounding bit is set, except if cnt == DRAW_BITS in which case the rounding bit is outside rp[0] and must be generated. */ if (rnd_mode == MPFR_RNDU || rnd_mode == MPFR_RNDA || (rnd_mode == MPFR_RNDN && cnt == exp - emin - 1 && (cnt != DRAW_BITS || random_rounding_bit (rstate)))) { mpfr_set_ui_2exp (rop, 1, emin - 1, rnd_mode); return +1; } else { MPFR_SET_ZERO (rop); return -1; } } exp -= cnt; } MPFR_EXP (rop) = exp; /* Warning: may be outside the current exponent range */ /* Significand: we need generate only nbits-1 bits, since the most significant is 1 */ mpfr_rand_raw (rp, rstate, nbits - 1); n = nlimbs * GMP_NUMB_BITS - nbits; if (MPFR_LIKELY (n != 0)) /* this will put the low bits to zero */ mpn_lshift (rp, rp, nlimbs, n); /* Set the msb to 1 since it was fixed by the exponent choice */ rp[nlimbs - 1] |= MPFR_LIMB_HIGHBIT; /* Rounding */ if (rnd_mode == MPFR_RNDU || rnd_mode == MPFR_RNDA || (rnd_mode == MPFR_RNDN && random_rounding_bit (rstate))) { /* Take care of the exponent range: it may have been reduced */ if (exp < emin) mpfr_set_ui_2exp (rop, 1, emin - 1, rnd_mode); else if (exp > mpfr_get_emax ()) mpfr_set_inf (rop, +1); /* overflow, flag set by mpfr_check_range */ else mpfr_nextabove (rop); inex = +1; } else inex = -1; return mpfr_check_range (rop, inex, rnd_mode); }

static void underflowed_cothinf (void) { mpfr_t x, y; int i, inex, rnd, err = 0; mpfr_exp_t old_emin; old_emin = mpfr_get_emin (); mpfr_init2 (x, 8); mpfr_init2 (y, 8); for (i = -1; i <= 1; i += 2) RND_LOOP (rnd) { mpfr_set_inf (x, i); mpfr_clear_flags (); set_emin (2); /* 1 is not representable. */ inex = mpfr_coth (x, x, (mpfr_rnd_t) rnd); set_emin (old_emin); if (! mpfr_underflow_p ()) { printf ("Error in underflowed_cothinf (i = %d, rnd = %s):\n" " The underflow flag is not set.\n", i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); err = 1; } mpfr_set_si (y, (i < 0 && (rnd == MPFR_RNDD || rnd == MPFR_RNDA)) || (i > 0 && (rnd == MPFR_RNDU || rnd == MPFR_RNDA)) ? 2 : 0, MPFR_RNDN); if (i < 0) mpfr_neg (y, y, MPFR_RNDN); if (! (mpfr_equal_p (x, y) && MPFR_MULT_SIGN (MPFR_SIGN (x), MPFR_SIGN (y)) > 0)) { printf ("Error in underflowed_cothinf (i = %d, rnd = %s):\n" " Got ", i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); mpfr_print_binary (x); printf (" instead of "); mpfr_print_binary (y); printf (".\n"); err = 1; } if ((rnd == MPFR_RNDD || (i > 0 && (rnd == MPFR_RNDN || rnd == MPFR_RNDZ))) && inex >= 0) { printf ("Error in underflowed_cothinf (i = %d, rnd = %s):\n" " The inexact value must be negative.\n", i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); err = 1; } if ((rnd == MPFR_RNDU || (i < 0 && (rnd == MPFR_RNDN || rnd == MPFR_RNDZ))) && inex <= 0) { printf ("Error in underflowed_cothinf (i = %d, rnd = %s):\n" " The inexact value must be positive.\n", i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); err = 1; } } if (err) exit (1); mpfr_clear (x); mpfr_clear (y); }

int main (int argc, char *argv[]) { mpfr_t x, y; mpfr_exp_t emin, emax; tests_start_mpfr (); test_set_underflow (); test_set_overflow (); check_default_rnd(); mpfr_init (x); mpfr_init (y); emin = mpfr_get_emin (); emax = mpfr_get_emax (); if (emin >= emax) { printf ("Error: emin >= emax\n"); exit (1); } mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_mul_2exp (x, x, 1024, MPFR_RNDN); mpfr_set_double_range (); mpfr_check_range (x, 0, MPFR_RNDN); if (!mpfr_inf_p (x) || (mpfr_sgn(x) <= 0)) { printf ("Error: 2^1024 rounded to nearest should give +Inf\n"); exit (1); } set_emax (1025); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_mul_2exp (x, x, 1024, MPFR_RNDN); mpfr_set_double_range (); mpfr_check_range (x, 0, MPFR_RNDD); if (!mpfr_number_p (x)) { printf ("Error: 2^1024 rounded down should give a normal number\n"); exit (1); } mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_mul_2exp (x, x, 1023, MPFR_RNDN); mpfr_add (x, x, x, MPFR_RNDN); if (!mpfr_inf_p (x) || (mpfr_sgn(x) <= 0)) { printf ("Error: x+x rounded to nearest for x=2^1023 should give +Inf\n"); printf ("emax = %ld\n", (long) mpfr_get_emax ()); printf ("got "); mpfr_print_binary (x); puts (""); exit (1); } mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_mul_2exp (x, x, 1023, MPFR_RNDN); mpfr_add (x, x, x, MPFR_RNDD); if (!mpfr_number_p (x)) { printf ("Error: x+x rounded down for x=2^1023 should give" " a normal number\n"); exit (1); } mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_div_2exp (x, x, 1022, MPFR_RNDN); mpfr_set_str_binary (y, "1.1e-1022"); /* y = 3/2*x */ mpfr_sub (y, y, x, MPFR_RNDZ); if (mpfr_cmp_ui (y, 0)) { printf ("Error: y-x rounded to zero should give 0" " for y=3/2*2^(-1022), x=2^(-1022)\n"); printf ("y="); mpfr_print_binary (y); puts (""); exit (1); } set_emin (-1026); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_div_2exp (x, x, 1025, MPFR_RNDN); mpfr_set_double_range (); mpfr_check_range (x, 0, MPFR_RNDN); if (!MPFR_IS_ZERO (x) ) { printf ("Error: x rounded to nearest for x=2^-1024 should give Zero\n"); printf ("emin = %ld\n", (long) mpfr_get_emin ()); printf ("got "); mpfr_dump (x); exit (1); } mpfr_clear (x); mpfr_clear (y); set_emin (emin); set_emax (emax); check_emin_emax(); check_flags(); check_set_get_prec (); check_powerof2 (); check_set (); tests_end_mpfr (); return 0; }

static void underflow_up1 (void) { mpfr_t delta, x, y, z, z0; mpfr_exp_t n; int inex; int rnd; int i; n = mpfr_get_emin (); if (n < LONG_MIN) return; mpfr_init2 (delta, 2); inex = mpfr_set_ui_2exp (delta, 1, -2, MPFR_RNDN); MPFR_ASSERTN (inex == 0); mpfr_init2 (x, 8); inex = mpfr_set_ui (x, 2, MPFR_RNDN); MPFR_ASSERTN (inex == 0); mpfr_init2 (y, sizeof (long) * CHAR_BIT + 4); inex = mpfr_set_si (y, n, MPFR_RNDN); MPFR_ASSERTN (inex == 0); mpfr_init2 (z0, 2); mpfr_set_ui (z0, 0, MPFR_RNDN); mpfr_init2 (z, 32); for (i = 0; i <= 12; i++) { unsigned int flags = 0; char sy[16]; /* Test 2^(emin - i/4). * --> Underflow iff i > 4. * --> Zero in MPFR_RNDN iff i >= 8. */ if (i != 0 && i != 4) flags |= MPFR_FLAGS_INEXACT; if (i > 4) flags |= MPFR_FLAGS_UNDERFLOW; sprintf (sy, "emin - %d/4", i); RND_LOOP (rnd) { int zero; zero = (i > 4 && (rnd == MPFR_RNDZ || rnd == MPFR_RNDD)) || (i >= 8 && rnd == MPFR_RNDN); mpfr_clear_flags (); inex = mpfr_pow (z, x, y, (mpfr_rnd_t) rnd); cmpres (1, "2", sy, (mpfr_rnd_t) rnd, zero ? z0 : (mpfr_ptr) NULL, -1, z, inex, flags, "underflow_up1", "mpfr_pow"); test_others ("2", sy, (mpfr_rnd_t) rnd, x, y, z, inex, flags, "underflow_up1"); } inex = mpfr_sub (y, y, delta, MPFR_RNDN); MPFR_ASSERTN (inex == 0); } mpfr_clears (delta, x, y, z, z0, (mpfr_ptr) 0); }

int main (int argc, char *argv[]) { int b; mpfr_t x; mp_rnd_t r; char s[MAX_DIGITS + 2]; mp_exp_t e, f; size_t m; mp_prec_t p; int i; tests_start_mpfr (); check_small (); check_special (2, 2); for (i = 0; i < ITER; i++) { p = 2 + (randlimb () % (MAX_DIGITS - 1)); b = 2 + (randlimb () % 35); check_special (b, p); } mpfr_init2 (x, MAX_DIGITS); for (i = 0; i < ITER; i++) { m = 2 + (randlimb () % (MAX_DIGITS - 1)); mpfr_urandomb (x, RANDS); e = (mp_exp_t) (randlimb () % 21) - 10; mpfr_set_exp (x, (e == -10) ? mpfr_get_emin () : ((e == 10) ? mpfr_get_emax () : e)); b = 2 + (randlimb () % 35); r = RND_RAND (); mpfr_get_str (s, &f, b, m, x, r); } mpfr_clear (x); check_large (); check3 ("4.059650008e-83", GMP_RNDN, "40597"); check3 ("-6.606499965302424244461355e233", GMP_RNDN, "-66065"); check3 ("-7.4", GMP_RNDN, "-74000"); check3 ("0.997", GMP_RNDN, "99700"); check3 ("-4.53063926135729747564e-308", GMP_RNDN, "-45306"); check3 ("2.14478198760196000000e+16", GMP_RNDN, "21448"); check3 ("7.02293374921793516813e-84", GMP_RNDN, "70229"); check3 ("-6.7274500420134077e-87", GMP_RNDN, "-67275"); check3 ("-6.7274500420134077e-87", GMP_RNDZ, "-67274"); check3 ("-6.7274500420134077e-87", GMP_RNDU, "-67274"); check3 ("-6.7274500420134077e-87", GMP_RNDD, "-67275"); check3 ("6.7274500420134077e-87", GMP_RNDN, "67275"); check3 ("6.7274500420134077e-87", GMP_RNDZ, "67274"); check3 ("6.7274500420134077e-87", GMP_RNDU, "67275"); check3 ("6.7274500420134077e-87", GMP_RNDD, "67274"); check_bug_base2k (); check_reduced_exprange (); tests_end_mpfr (); return 0; }

static void check_special () { mpfr_t x, y, z; mp_exp_t emin, emax; mpfr_init (x); mpfr_init (y); mpfr_init (z); /* check exp(NaN) = NaN */ mpfr_set_nan (x); mpfr_exp (y, x, GMP_RNDN); if (!mpfr_nan_p (y)) { printf ("Error for exp(NaN)\n"); exit (1); } /* check exp(+inf) = +inf */ mpfr_set_inf (x, 1); mpfr_exp (y, x, GMP_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); mpfr_exp (y, x, GMP_RNDN); if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) { printf ("Error for exp(-inf)\n"); exit (1); } /* check overflow */ emax = mpfr_get_emax (); set_emax (10); mpfr_set_ui (x, 7, GMP_RNDN); mpfr_exp (y, x, GMP_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 */ emin = mpfr_get_emin (); set_emin (-10); mpfr_set_si (x, -9, GMP_RNDN); mpfr_exp (y, x, GMP_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"); mpfr_exp (y, x, GMP_RNDD); if (mpfr_cmp_ui_2exp (y, 3, -2)) { printf ("Error for exp(-1/16), prec=2, RNDD\n"); exit (1); } mpfr_exp (y, x, GMP_RNDZ); if (mpfr_cmp_ui (y, 1)) { printf ("Error for exp(-1/16), prec=2, RNDZ\n"); exit (1); } mpfr_set_str_binary (x, "0.1E-3"); mpfr_exp (y, x, GMP_RNDN); if (mpfr_cmp_ui (y, 1)) { printf ("Error for exp(1/16), prec=2, RNDN\n"); exit (1); } mpfr_exp (y, x, GMP_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"); mpfr_exp (y, x, GMP_RNDN); mpfr_set_prec (x, 153); mpfr_set_prec (z, 153); mpfr_set_str_binary (x, "1.1101011000111101011110000111010010101001101001110111e28"); mpfr_exp (z, x, GMP_RNDN); mpfr_prec_round (z, 53, GMP_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, GMP_RNDN); mpfr_set_prec (y, 2); mpfr_exp_3 (y, x, GMP_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"); mpfr_exp (y, x, GMP_RNDN); if (mpfr_cmp_str (y, "0.11000111100001100110010101111101011010010101010000" "1101110111100010111001011111111000110111001011001101010" "01E22", 2, GMP_RNDN)) { printf ("Special overflow error (1)\n"); mpfr_dump (y); exit (1); } set_emin (MPFR_EMIN_MIN); set_emax (MPFR_EMAX_MAX); /* Check for overflow producing a segfault with HUGE exponent */ mpfr_set_ui (x, 3, GMP_RNDN); mpfr_mul_2ui (x, x, 32, GMP_RNDN); mpfr_exp (y, x, GMP_RNDN); /* Can't test return value: May overflow or not*/ mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }

static void check1 (void) { mpfr_t x; int i, j, k, s, old_inex, tiny, expj; mpfr_exp_t emin, emax; unsigned int expflags, flags; emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_set_default_prec (9); mpfr_set_emin (-10); mpfr_set_emax (10); mpfr_init (x); for (i = 0; i < (sizeof (tab) / sizeof (tab[0])); i++) for (s = 0; s <= (tab[i].rnd == MPFR_RNDN); s++) for (k = 0; k <= 1; k++) { mpfr_set_str (x, tab[i].in, 2, MPFR_RNDN); old_inex = tab[i].i; expj = tab[i].j; if (s) { mpfr_neg (x, x, MPFR_RNDN); old_inex = - old_inex; expj = - expj; } if (k && old_inex) old_inex = old_inex < 0 ? INT_MIN : INT_MAX; tiny = MPFR_GET_EXP (x) <= -3; mpfr_clear_flags (); j = mpfr_subnormalize (x, old_inex, tab[i].rnd); expflags = (tiny ? MPFR_FLAGS_UNDERFLOW : 0) | (expj ? MPFR_FLAGS_INEXACT : 0); flags = __gmpfr_flags; if (s) mpfr_neg (x, x, MPFR_RNDN); if (mpfr_cmp_str (x, tab[i].out, 2, MPFR_RNDN) != 0 || flags != expflags || ! SAME_SIGN (j, expj)) { const char *sgn = s ? "-" : ""; printf ("Error for i = %d (old_inex = %d), k = %d, x = %s%s\n" "Expected: %s%s\nGot: ", i, old_inex, k, sgn, tab[i].in, sgn, tab[i].out); if (s) mpfr_neg (x, x, MPFR_RNDN); mpfr_dump (x); printf ("Expected flags = %u, got %u\n", expflags, flags); printf ("Expected ternary value = %d, got %d\n", expj, j); exit (1); } } mpfr_clear (x); MPFR_ASSERTN (mpfr_get_emin () == -10); MPFR_ASSERTN (mpfr_get_emax () == 10); set_emin (emin); set_emax (emax); }

int main (int argc, char *argv[]) { mpfr_t x; long k, z, d, N; unsigned long zl, dl; int inex; int r; mpfr_exp_t emin, emax; int flag; tests_start_mpfr (); mpfr_init2 (x, 100); N = (argc==1) ? 100000 : atol (argv[1]); for (k = 1; k <= N; k++) { z = (long) (randlimb () & LONG_MAX) + LONG_MIN / 2; inex = mpfr_set_si (x, z, MPFR_RNDZ); d = mpfr_get_si (x, MPFR_RNDZ); if (d != z) { printf ("Error in mpfr_set_si: expected %ld got %ld\n", z, d); exit (1); } if (inex) { printf ("Error in mpfr_set_si: inex value incorrect for %ld: %d\n", z, inex); exit (1); } } for (k = 1; k <= N; k++) { zl = randlimb (); inex = mpfr_set_ui (x, zl, MPFR_RNDZ); dl = mpfr_get_ui (x, MPFR_RNDZ); if (dl != zl) { printf ("Error in mpfr_set_ui: expected %lu got %lu\n", zl, dl); exit (1); } if (inex) { printf ("Error in mpfr_set_ui: inex value incorrect for %lu: %d\n", zl, inex); exit (1); } } mpfr_set_prec (x, 2); if (mpfr_set_si (x, 5, MPFR_RNDZ) >= 0) { printf ("Wrong inexact flag for x=5, rnd=MPFR_RNDZ\n"); exit (1); } mpfr_set_prec (x, 2); if (mpfr_set_si (x, -5, MPFR_RNDZ) <= 0) { printf ("Wrong inexact flag for x=-5, rnd=MPFR_RNDZ\n"); exit (1); } mpfr_set_prec (x, 3); inex = mpfr_set_si (x, 77617, MPFR_RNDD); /* should be 65536 */ if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1)) || inex >= 0) { printf ("Error in mpfr_set_si(x:3, 77617, MPFR_RNDD)\n"); mpfr_print_binary (x); puts (""); exit (1); } inex = mpfr_set_ui (x, 77617, MPFR_RNDD); /* should be 65536 */ if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1)) || inex >= 0) { printf ("Error in mpfr_set_ui(x:3, 77617, MPFR_RNDD)\n"); mpfr_print_binary (x); puts (""); exit (1); } mpfr_set_prec (x, 2); inex = mpfr_set_si (x, 33096, MPFR_RNDU); if (mpfr_get_si (x, MPFR_RNDZ) != 49152 || inex <= 0) { printf ("Error in mpfr_set_si, exp. 49152, got %ld, inex %d\n", mpfr_get_si (x, MPFR_RNDZ), inex); exit (1); } inex = mpfr_set_ui (x, 33096, MPFR_RNDU); if (mpfr_get_si (x, MPFR_RNDZ) != 49152) { printf ("Error in mpfr_set_ui, exp. 49152, got %ld, inex %d\n", mpfr_get_si (x, MPFR_RNDZ), inex); exit (1); } for (r = 0 ; r < MPFR_RND_MAX ; r++) { mpfr_set_si (x, -1, (mpfr_rnd_t) r); mpfr_set_ui (x, 0, (mpfr_rnd_t) r); if (MPFR_IS_NEG (x) || mpfr_get_ui (x, (mpfr_rnd_t) r) != 0) { printf ("mpfr_set_ui (x, 0) gives -0 for %s\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r)); exit (1); } mpfr_set_si (x, -1, (mpfr_rnd_t) r); mpfr_set_si (x, 0, (mpfr_rnd_t) r); if (MPFR_IS_NEG (x) || mpfr_get_si (x, (mpfr_rnd_t) r) != 0) { printf ("mpfr_set_si (x, 0) gives -0 for %s\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r)); exit (1); } } /* check potential bug in case mp_limb_t is unsigned */ emax = mpfr_get_emax (); set_emax (0); mpfr_set_si (x, -1, MPFR_RNDN); if (mpfr_sgn (x) >= 0) { printf ("mpfr_set_si (x, -1) fails\n"); exit (1); } set_emax (emax); emax = mpfr_get_emax (); set_emax (5); mpfr_set_prec (x, 2); mpfr_set_si (x, -31, MPFR_RNDN); if (mpfr_sgn (x) >= 0) { printf ("mpfr_set_si (x, -31) fails\n"); exit (1); } set_emax (emax); /* test for get_ui */ mpfr_set_ui (x, 0, MPFR_RNDN); MPFR_ASSERTN(mpfr_get_ui (x, MPFR_RNDN) == 0); mpfr_set_ui (x, ULONG_MAX, MPFR_RNDU); mpfr_nextabove (x); mpfr_get_ui (x, MPFR_RNDU); /* another test for get_ui */ mpfr_set_prec (x, 10); mpfr_set_str_binary (x, "10.101"); dl = mpfr_get_ui (x, MPFR_RNDN); MPFR_ASSERTN (dl == 3); mpfr_set_str_binary (x, "-1.0"); mpfr_get_ui (x, MPFR_RNDN); mpfr_set_str_binary (x, "0.1"); dl = mpfr_get_ui (x, MPFR_RNDN); MPFR_ASSERTN (dl == 0); dl = mpfr_get_ui (x, MPFR_RNDZ); MPFR_ASSERTN (dl == 0); dl = mpfr_get_ui (x, MPFR_RNDD); MPFR_ASSERTN (dl == 0); dl = mpfr_get_ui (x, MPFR_RNDU); MPFR_ASSERTN (dl == 1); /* coverage tests */ mpfr_set_prec (x, 2); mpfr_set_si (x, -7, MPFR_RNDD); MPFR_ASSERTN(mpfr_cmp_si (x, -8) == 0); mpfr_set_prec (x, 2); mpfr_set_ui (x, 7, MPFR_RNDU); MPFR_ASSERTN(mpfr_cmp_ui (x, 8) == 0); emax = mpfr_get_emax (); set_emax (3); mpfr_set_ui (x, 7, MPFR_RNDU); MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); set_emax (1); MPFR_ASSERTN( mpfr_set_ui (x, 7, MPFR_RNDU) ); MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); set_emax (emax); mpfr_set_ui_2exp (x, 17, -50, MPFR_RNDN); MPFR_ASSERTN (mpfr_get_ui (x, MPFR_RNDD) == 0); MPFR_ASSERTN (mpfr_get_si (x, MPFR_RNDD) == 0); /* Test for ERANGE flag + correct behaviour if overflow */ mpfr_set_prec (x, 256); mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN); mpfr_clear_erangeflag (); dl = mpfr_get_ui (x, MPFR_RNDN); if (dl != ULONG_MAX || mpfr_erangeflag_p ()) { printf ("ERROR for get_ui + ERANGE + ULONG_MAX (1)\n"); exit (1); } mpfr_add_ui (x, x, 1, MPFR_RNDN); dl = mpfr_get_ui (x, MPFR_RNDN); if (dl != ULONG_MAX || !mpfr_erangeflag_p ()) { printf ("ERROR for get_ui + ERANGE + ULONG_MAX (2)\n"); exit (1); } mpfr_set_si (x, -1, MPFR_RNDN); mpfr_clear_erangeflag (); dl = mpfr_get_ui (x, MPFR_RNDN); if (dl != 0 || !mpfr_erangeflag_p ()) { printf ("ERROR for get_ui + ERANGE + -1 \n"); exit (1); } mpfr_set_si (x, LONG_MAX, MPFR_RNDN); mpfr_clear_erangeflag (); d = mpfr_get_si (x, MPFR_RNDN); if (d != LONG_MAX || mpfr_erangeflag_p ()) { printf ("ERROR for get_si + ERANGE + LONG_MAX (1): %ld\n", d); exit (1); } mpfr_add_ui (x, x, 1, MPFR_RNDN); d = mpfr_get_si (x, MPFR_RNDN); if (d != LONG_MAX || !mpfr_erangeflag_p ()) { printf ("ERROR for get_si + ERANGE + LONG_MAX (2)\n"); exit (1); } mpfr_set_si (x, LONG_MIN, MPFR_RNDN); mpfr_clear_erangeflag (); d = mpfr_get_si (x, MPFR_RNDN); if (d != LONG_MIN || mpfr_erangeflag_p ()) { printf ("ERROR for get_si + ERANGE + LONG_MIN (1)\n"); exit (1); } mpfr_sub_ui (x, x, 1, MPFR_RNDN); d = mpfr_get_si (x, MPFR_RNDN); if (d != LONG_MIN || !mpfr_erangeflag_p ()) { printf ("ERROR for get_si + ERANGE + LONG_MIN (2)\n"); exit (1); } mpfr_set_nan (x); mpfr_clear_erangeflag (); d = mpfr_get_ui (x, MPFR_RNDN); if (d != 0 || !mpfr_erangeflag_p ()) { printf ("ERROR for get_ui + NaN\n"); exit (1); } mpfr_clear_erangeflag (); d = mpfr_get_si (x, MPFR_RNDN); if (d != 0 || !mpfr_erangeflag_p ()) { printf ("ERROR for get_si + NaN\n"); exit (1); } emin = mpfr_get_emin (); mpfr_set_prec (x, 2); mpfr_set_emin (4); mpfr_clear_flags (); mpfr_set_ui (x, 7, MPFR_RNDU); flag = mpfr_underflow_p (); mpfr_set_emin (emin); if (mpfr_cmp_ui (x, 8) != 0) { printf ("Error for mpfr_set_ui (x, 7, MPFR_RNDU), prec = 2, emin = 4\n"); exit (1); } if (flag) { printf ("mpfr_set_ui (x, 7, MPFR_RNDU) should not underflow " "with prec = 2, emin = 4\n"); exit (1); } mpfr_set_emin (4); mpfr_clear_flags (); mpfr_set_si (x, -7, MPFR_RNDD); flag = mpfr_underflow_p (); mpfr_set_emin (emin); if (mpfr_cmp_si (x, -8) != 0) { printf ("Error for mpfr_set_si (x, -7, MPFR_RNDD), prec = 2, emin = 4\n"); exit (1); } if (flag) { printf ("mpfr_set_si (x, -7, MPFR_RNDD) should not underflow " "with prec = 2, emin = 4\n"); exit (1); } mpfr_clear (x); test_2exp (); test_macros (); test_macros_keyword (); tests_end_mpfr (); return 0; }

static void test_generic (mpfr_prec_t p0, mpfr_prec_t p1, unsigned int N) { mpfr_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 mpfr_rnd_t rnd; int inexact, compare, compare2; unsigned int n; unsigned long ctrt = 0, ctrn = 0; mpfr_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, 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 } } 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 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); #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, MPFR_RNDN); #ifdef TWO_ARGS printf ("\nu="); mpfr_out_str (stdout, 2, xprec, u, MPFR_RNDN); #elif defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) printf ("\nu="); mpfr_out_str (stdout, 2, IEEE_DBL_MANT_DIG, u, MPFR_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, MPFR_RNDN); puts (""); printf ("expected "); mpfr_out_str (stdout, 2, prec, t, MPFR_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 main (int argc, char *argv[]) { mpfr_t x, y, r; long q[1]; if (argc == 3) /* usage: tremquo x y (rnd=MPFR_RNDN implicit) */ { mpfr_init2 (x, GMP_NUMB_BITS); mpfr_init2 (y, GMP_NUMB_BITS); mpfr_init2 (r, GMP_NUMB_BITS); mpfr_set_str (x, argv[1], 10, MPFR_RNDN); mpfr_set_str (y, argv[2], 10, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); printf ("r="); mpfr_out_str (stdout, 10, 0, r, MPFR_RNDN); printf (" q=%ld\n", q[0]); mpfr_clear (x); mpfr_clear (y); mpfr_clear (r); return 0; } tests_start_mpfr (); bug20090227 (); mpfr_init (x); mpfr_init (y); mpfr_init (r); /* special values */ mpfr_set_nan (x); mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (r)); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_set_nan (y); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (r)); mpfr_set_inf (x, 1); /* +Inf */ mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (r)); mpfr_set_inf (x, 1); /* +Inf */ mpfr_set_ui (y, 0, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (r)); mpfr_set_inf (x, 1); /* +Inf */ mpfr_set_inf (y, 1); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (r)); mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_set_inf (y, 1); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui (r, 0) == 0 && MPFR_IS_POS (r)); MPFR_ASSERTN (q[0] == (long) 0); mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); /* -0 */ mpfr_set_inf (y, 1); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui (r, 0) == 0 && MPFR_IS_NEG (r)); MPFR_ASSERTN (q[0] == (long) 0); mpfr_set_ui (x, 17, MPFR_RNDN); mpfr_set_inf (y, 1); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp (r, x) == 0); MPFR_ASSERTN (q[0] == (long) 0); mpfr_set_ui (x, 17, MPFR_RNDN); mpfr_set_ui (y, 0, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_nan_p (r)); mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_set_ui (y, 17, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui (r, 0) == 0 && MPFR_IS_POS (r)); MPFR_ASSERTN (q[0] == (long) 0); mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); mpfr_set_ui (y, 17, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui (r, 0) == 0 && MPFR_IS_NEG (r)); MPFR_ASSERTN (q[0] == (long) 0); mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); /* check four possible sign combinations */ mpfr_set_ui (x, 42, MPFR_RNDN); mpfr_set_ui (y, 17, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui (r, 8) == 0); MPFR_ASSERTN (q[0] == (long) 2); mpfr_set_si (x, -42, MPFR_RNDN); mpfr_set_ui (y, 17, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_si (r, -8) == 0); MPFR_ASSERTN (q[0] == (long) -2); mpfr_set_si (x, -42, MPFR_RNDN); mpfr_set_si (y, -17, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_si (r, -8) == 0); MPFR_ASSERTN (q[0] == (long) 2); mpfr_set_ui (x, 42, MPFR_RNDN); mpfr_set_si (y, -17, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui (r, 8) == 0); MPFR_ASSERTN (q[0] == (long) -2); mpfr_set_prec (x, 100); mpfr_set_prec (y, 50); mpfr_set_ui (x, 42, MPFR_RNDN); mpfr_nextabove (x); /* 42 + 2^(-94) */ mpfr_set_ui (y, 21, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui_2exp (r, 1, -94) == 0); MPFR_ASSERTN (q[0] == (long) 2); mpfr_set_prec (x, 50); mpfr_set_prec (y, 100); mpfr_set_ui (x, 42, MPFR_RNDN); mpfr_nextabove (x); /* 42 + 2^(-44) */ mpfr_set_ui (y, 21, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_ui_2exp (r, 1, -44) == 0); MPFR_ASSERTN (q[0] == (long) 2); mpfr_set_prec (x, 100); mpfr_set_prec (y, 50); mpfr_set_ui (x, 42, MPFR_RNDN); mpfr_set_ui (y, 21, MPFR_RNDN); mpfr_nextabove (y); /* 21 + 2^(-45) */ mpfr_remquo (r, q, x, y, MPFR_RNDN); /* r should be 42 - 2*(21 + 2^(-45)) = -2^(-44) */ MPFR_ASSERTN (mpfr_cmp_si_2exp (r, -1, -44) == 0); MPFR_ASSERTN (q[0] == (long) 2); mpfr_set_prec (x, 50); mpfr_set_prec (y, 100); mpfr_set_ui (x, 42, MPFR_RNDN); mpfr_set_ui (y, 21, MPFR_RNDN); mpfr_nextabove (y); /* 21 + 2^(-95) */ mpfr_remquo (r, q, x, y, MPFR_RNDN); /* r should be 42 - 2*(21 + 2^(-95)) = -2^(-94) */ MPFR_ASSERTN (mpfr_cmp_si_2exp (r, -1, -94) == 0); MPFR_ASSERTN (q[0] == (long) 2); /* exercise large quotient */ mpfr_set_ui_2exp (x, 1, 65, MPFR_RNDN); mpfr_set_ui (y, 1, MPFR_RNDN); /* quotient is 2^65 */ mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_si (r, 0) == 0); MPFR_ASSERTN (q[0] % 1073741824L == 0L); /* another large quotient */ mpfr_set_prec (x, 65); mpfr_set_prec (y, 65); mpfr_const_pi (x, MPFR_RNDN); mpfr_mul_2exp (x, x, 63, MPFR_RNDN); mpfr_const_log2 (y, MPFR_RNDN); mpfr_set_prec (r, 10); mpfr_remquo (r, q, x, y, MPFR_RNDN); /* q should be 41803643793084085130, r should be 605/2048 */ MPFR_ASSERTN (mpfr_cmp_ui_2exp (r, 605, -11) == 0); MPFR_ASSERTN ((q[0] > 0) && ((q[0] % 1073741824L) == 733836170L)); /* check cases where quotient is 1.5 +/- eps */ mpfr_set_prec (x, 65); mpfr_set_prec (y, 65); mpfr_set_prec (r, 63); mpfr_set_ui (x, 3, MPFR_RNDN); mpfr_set_ui (y, 2, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); /* x/y = 1.5, quotient should be 2 (even rule), remainder should be -1 */ MPFR_ASSERTN (mpfr_cmp_si (r, -1) == 0); MPFR_ASSERTN (q[0] == 2L); mpfr_set_ui (x, 3, MPFR_RNDN); mpfr_nextabove (x); /* 3 + 2^(-63) */ mpfr_set_ui (y, 2, MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); /* x/y = 1.5 + 2^(-64), quo should be 2, r should be -1 + 2^(-63) */ MPFR_ASSERTN (mpfr_add_ui (r, r, 1, MPFR_RNDN) == 0); MPFR_ASSERTN (mpfr_cmp_ui_2exp (r, 1, -63) == 0); MPFR_ASSERTN (q[0] == 2L); mpfr_set_ui (x, 3, MPFR_RNDN); mpfr_set_ui (y, 2, MPFR_RNDN); mpfr_nextabove (y); /* 2 + 2^(-63) */ mpfr_remquo (r, q, x, y, MPFR_RNDN); /* x/y = 1.5 - eps, quo should be 1, r should be 1 - 2^(-63) */ MPFR_ASSERTN (mpfr_sub_ui (r, r, 1, MPFR_RNDN) == 0); MPFR_ASSERTN (mpfr_cmp_si_2exp (r, -1, -63) == 0); MPFR_ASSERTN (q[0] == 1L); /* bug founds by Kaveh Ghazi, 3 May 2007 */ mpfr_set_ui (x, 2, MPFR_RNDN); mpfr_set_ui (y, 3, MPFR_RNDN); mpfr_remainder (r, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_si (r, -1) == 0); mpfr_set_si (x, -1, MPFR_RNDN); mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_remainder (r, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_si (r, 0) == 0 && MPFR_SIGN (r) < 0); /* check argument reuse */ mpfr_set_si (x, -1, MPFR_RNDN); mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_remainder (x, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_si (x, 0) == 0 && MPFR_SIGN (x) < 0); mpfr_set_ui_2exp (x, 1, mpfr_get_emax () - 1, MPFR_RNDN); mpfr_set_ui_2exp (y, 1, mpfr_get_emin (), MPFR_RNDN); mpfr_remquo (r, q, x, y, MPFR_RNDN); MPFR_ASSERTN (mpfr_zero_p (r) && MPFR_SIGN (r) > 0); MPFR_ASSERTN (q[0] == 0); mpfr_clear (x); mpfr_clear (y); mpfr_clear (r); tests_end_mpfr (); return 0; }

static void test_overflow2 (void) { mpfr_t x, y, z, r; int i, inex, rnd, err = 0; mpfr_inits2 (8, x, y, z, r, (mpfr_ptr) 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, (mpfr_ptr) 0); }

static void special_overflow (void) { mpfr_t x, y; int i; mp_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, GMP_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, GMP_RNDZ); if (mpfr_cmp_str (y, "0.111111111111111111111111E0", 2, GMP_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, GMP_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, GMP_RNDU); if (mpfr_cmp_str (y, "-0.111111111111111111111111E0", 2, GMP_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_underflow1 (void) { mpfr_t x, y, z, r; int inex, signy, signz, rnd, err = 0; mpfr_inits2 (8, x, y, z, r, (mpfr_ptr) 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, (mpfr_ptr) 0); }

static void check_underflow (void) { mpfr_t sum1, sum2, t[NUNFL]; mpfr_ptr p[NUNFL]; mpfr_prec_t precmax = 444; mpfr_exp_t emin, emax; unsigned int ex_flags, flags; int c, i; emin = mpfr_get_emin (); emax = mpfr_get_emax (); set_emin (MPFR_EMIN_MIN); set_emax (MPFR_EMAX_MAX); ex_flags = MPFR_FLAGS_UNDERFLOW | MPFR_FLAGS_INEXACT; mpfr_init2 (sum1, MPFR_PREC_MIN); mpfr_init2 (sum2, precmax); for (i = 0; i < NUNFL; i++) { mpfr_init2 (t[i], precmax); p[i] = t[i]; } for (c = 0; c < 8; c++) { mpfr_prec_t fprec; int n, neg, r; fprec = MPFR_PREC_MIN + (randlimb () % (precmax - MPFR_PREC_MIN + 1)); n = 3 + (randlimb () % (NUNFL - 2)); MPFR_ASSERTN (n <= NUNFL); mpfr_set_prec (sum2, (randlimb () & 1) ? MPFR_PREC_MIN : precmax); mpfr_set_prec (t[0], fprec + 64); mpfr_set_zero (t[0], 1); for (i = 1; i < n; i++) { int inex; mpfr_set_prec (t[i], MPFR_PREC_MIN + (randlimb () % (fprec - MPFR_PREC_MIN + 1))); do mpfr_urandomb (t[i], RANDS); while (MPFR_IS_ZERO (t[i])); mpfr_set_exp (t[i], MPFR_EMIN_MIN); inex = mpfr_sub (t[0], t[0], t[i], MPFR_RNDN); MPFR_ASSERTN (inex == 0); } neg = randlimb () & 1; if (neg) mpfr_nextbelow (t[0]); else mpfr_nextabove (t[0]); RND_LOOP(r) { int inex1, inex2; mpfr_set_zero (sum1, 1); if (neg) mpfr_nextbelow (sum1); else mpfr_nextabove (sum1); inex1 = mpfr_div_2ui (sum1, sum1, 2, (mpfr_rnd_t) r); mpfr_clear_flags (); inex2 = mpfr_sum (sum2, p, n, (mpfr_rnd_t) r); flags = __gmpfr_flags; MPFR_ASSERTN (mpfr_check (sum1)); MPFR_ASSERTN (mpfr_check (sum2)); if (flags != ex_flags) { printf ("Bad flags in check_underflow on %s, c = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), c); printf ("Expected flags:"); flags_out (ex_flags); printf ("Got flags: "); flags_out (flags); printf ("sum = "); mpfr_dump (sum2); exit (1); } if (!(mpfr_equal_p (sum1, sum2) && SAME_SIGN (inex1, inex2))) { printf ("Error in check_underflow on %s, c = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), c); printf ("Expected "); mpfr_dump (sum1); printf ("with inex = %d\n", inex1); printf ("Got "); mpfr_dump (sum2); printf ("with inex = %d\n", inex2); exit (1); } } } for (i = 0; i < NUNFL; i++) mpfr_clear (t[i]); mpfr_clears (sum1, sum2, (mpfr_ptr) 0); set_emin (emin); set_emax (emax); }

/* 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. * For debugging purpose (e.g. in case of crash or infinite loop), * you can set the MPFR_DEBUG_BADCASES environment variable to 1 in * order to output information about the tested worst cases. You can * also enable logging (when supported), but this may give too much * information. */ 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); }

/* Generic random tests with cancellations. * * In summary, we do 4000 tests of the following form: * 1. We set the first MPFR_NCANCEL members of an array to random values, * with a random exponent taken in 4 ranges, depending on the value of * i % 4 (see code below). * 2. For each of the next MPFR_NCANCEL iterations: * A. we randomly permute some terms of the array (to make sure that a * particular order doesn't have an influence on the result); * B. we compute the sum in a random rounding mode; * C. if this sum is zero, we end the current test (there is no longer * anything interesting to test); * D. we check that this sum is below some bound (chosen as infinite * for the first iteration of (2), i.e. this test will be useful * only for the next iterations, after cancellations); * E. we put the opposite of this sum in the array, the goal being to * introduce a chain of cancellations; * F. we compute the bound for the next iteration, derived from (E). * 3. We do another iteration like (2), but with reusing a random element * of the array. This last test allows one to check the support of * reused arguments. Before this support (r10467), it triggers an * assertion failure with (almost?) all seeds, and if assertions are * not checked, tsum fails in most cases but not all. */ static void cancel (void) { mpfr_t x[2 * MPFR_NCANCEL], bound; mpfr_ptr px[2 * MPFR_NCANCEL]; int i, j, k, n; mpfr_init2 (bound, 2); /* With 4000 tests, tsum will fail in most cases without support of reused arguments (before r10467). */ for (i = 0; i < 4000; i++) { mpfr_set_inf (bound, 1); for (n = 0; n <= numberof (x); n++) { mpfr_prec_t p; mpfr_rnd_t rnd; if (n < numberof (x)) { px[n] = x[n]; p = MPFR_PREC_MIN + (randlimb () % 256); mpfr_init2 (x[n], p); k = n; } else { /* Reuse of a random member of the array. */ k = randlimb () % n; } if (n < MPFR_NCANCEL) { mpfr_exp_t e; MPFR_ASSERTN (n < numberof (x)); e = (i & 1) ? 0 : mpfr_get_emin (); tests_default_random (x[n], 256, e, ((i & 2) ? e + 2000 : mpfr_get_emax ()), 0); } else { /* random permutation with n random transpositions */ for (j = 0; j < n; j++) { int k1, k2; k1 = randlimb () % (n-1); k2 = randlimb () % (n-1); mpfr_swap (x[k1], x[k2]); } rnd = RND_RAND (); #if DEBUG printf ("mpfr_sum cancellation test\n"); for (j = 0; j < n; j++) { printf (" x%d[%3ld] = ", j, mpfr_get_prec(x[j])); mpfr_out_str (stdout, 16, 0, x[j], MPFR_RNDN); printf ("\n"); } printf (" rnd = %s, output prec = %ld\n", mpfr_print_rnd_mode (rnd), mpfr_get_prec (x[n])); #endif mpfr_sum (x[k], px, n, rnd); if (mpfr_zero_p (x[k])) { if (k == n) n++; break; } if (mpfr_cmpabs (x[k], bound) > 0) { printf ("Error in cancel on i = %d, n = %d\n", i, n); printf ("Expected bound: "); mpfr_dump (bound); printf ("x[%d]: ", k); mpfr_dump (x[k]); exit (1); } if (k != n) break; /* For the bound, use MPFR_RNDU due to possible underflow. It would be nice to add some specific underflow checks, though there are already ones in check_underflow(). */ mpfr_set_ui_2exp (bound, 1, mpfr_get_exp (x[n]) - p - (rnd == MPFR_RNDN), MPFR_RNDU); /* The next sum will be <= bound in absolute value (the equality can be obtained in all rounding modes since the sum will be rounded). */ mpfr_neg (x[n], x[n], MPFR_RNDN); } } while (--n >= 0) mpfr_clear (x[n]); } mpfr_clear (bound); }

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_dump (y); 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 (MPFR_PREC_MIN, 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 consistency (void) { mpfr_t x, s1, s2, c1, c2; mpfr_exp_t emin, emax; mpfr_rnd_t rnd; unsigned int flags_sin, flags_cos, flags, flags_before, flags_ref; int inex_sin, is, inex_cos, ic, inex, inex_ref; int i; emin = mpfr_get_emin (); emax = mpfr_get_emax (); for (i = 0; i <= 10000; i++) { mpfr_init2 (x, MPFR_PREC_MIN + (randlimb () % 8)); mpfr_inits2 (MPFR_PREC_MIN + (randlimb () % 8), s1, s2, c1, c2, (mpfr_ptr) 0); if (i < 8 * MPFR_RND_MAX) { int j = i / MPFR_RND_MAX; if (j & 1) mpfr_set_emin (MPFR_EMIN_MIN); mpfr_set_si (x, (j & 2) ? 1 : -1, MPFR_RNDN); mpfr_set_exp (x, mpfr_get_emin ()); rnd = (mpfr_rnd_t) (i % MPFR_RND_MAX); flags_before = 0; if (j & 4) mpfr_set_emax (-17); } else { tests_default_random (x, 256, -5, 50); rnd = RND_RAND (); flags_before = (randlimb () & 1) ? (unsigned int) (MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE) : (unsigned int) 0; } __gmpfr_flags = flags_before; inex_sin = mpfr_sin (s1, x, rnd); is = inex_sin < 0 ? 2 : inex_sin > 0 ? 1 : 0; flags_sin = __gmpfr_flags; __gmpfr_flags = flags_before; inex_cos = mpfr_cos (c1, x, rnd); ic = inex_cos < 0 ? 2 : inex_cos > 0 ? 1 : 0; flags_cos = __gmpfr_flags; __gmpfr_flags = flags_before; inex = mpfr_sin_cos (s2, c2, x, rnd); flags = __gmpfr_flags; inex_ref = is + 4 * ic; flags_ref = flags_sin | flags_cos; if (!(mpfr_equal_p (s1, s2) && mpfr_equal_p (c1, c2)) || inex != inex_ref || flags != flags_ref) { printf ("mpfr_sin_cos and mpfr_sin/mpfr_cos disagree on %s," " i = %d\nx = ", mpfr_print_rnd_mode (rnd), i); mpfr_dump (x); printf ("s1 = "); mpfr_dump (s1); printf ("s2 = "); mpfr_dump (s2); printf ("c1 = "); mpfr_dump (c1); printf ("c2 = "); mpfr_dump (c2); printf ("inex_sin = %d (s = %d), inex_cos = %d (c = %d), " "inex = %d (expected %d)\n", inex_sin, is, inex_cos, ic, inex, inex_ref); printf ("flags_sin = 0x%x, flags_cos = 0x%x, " "flags = 0x%x (expected 0x%x)\n", flags_sin, flags_cos, flags, flags_ref); exit (1); } mpfr_clears (x, s1, s2, c1, c2, (mpfr_ptr) 0); mpfr_set_emin (emin); mpfr_set_emax (emax); } }

/* tgeneric(prec_min, prec_max, step, exp_max) checks rounding with random numbers: - with precision ranging from prec_min to prec_max with an increment of step, - with exponent between -exp_max and exp_max. It also checks parameter reuse (it is assumed here that either two mpc_t variables are equal or they are different, in the sense that the real part of one of them cannot be the imaginary part of the other). */ void tgeneric (mpc_function function, mpfr_prec_t prec_min, mpfr_prec_t prec_max, mpfr_prec_t step, mpfr_exp_t exp_max) { unsigned long ul1 = 0, ul2 = 0; long lo = 0; int i = 0; mpfr_t x1, x2, xxxx; mpc_t z1, z2, z3, z4, z5, zzzz, zzzz2; mpfr_rnd_t rnd_re, rnd_im, rnd2_re, rnd2_im; mpfr_prec_t prec; mpfr_exp_t exp_min; int special, special_cases; mpc_init2 (z1, prec_max); switch (function.type) { case C_CC: mpc_init2 (z2, prec_max); mpc_init2 (z3, prec_max); mpc_init2 (z4, prec_max); mpc_init2 (zzzz, 4*prec_max); special_cases = 8; break; case CCCC: mpc_init2 (z2, prec_max); mpc_init2 (z3, prec_max); mpc_init2 (z4, prec_max); mpc_init2 (z5, prec_max); mpc_init2 (zzzz, 4*prec_max); special_cases = 8; break; case FC: mpfr_init2 (x1, prec_max); mpfr_init2 (x2, prec_max); mpfr_init2 (xxxx, 4*prec_max); mpc_init2 (z2, prec_max); special_cases = 4; break; case CCF: case CFC: mpfr_init2 (x1, prec_max); mpc_init2 (z2, prec_max); mpc_init2 (z3, prec_max); mpc_init2 (zzzz, 4*prec_max); special_cases = 6; break; case CCI: case CCS: case CCU: case CUC: mpc_init2 (z2, prec_max); mpc_init2 (z3, prec_max); mpc_init2 (zzzz, 4*prec_max); special_cases = 5; break; case CUUC: mpc_init2 (z2, prec_max); mpc_init2 (z3, prec_max); mpc_init2 (zzzz, 4*prec_max); special_cases = 6; break; case CC_C: mpc_init2 (z2, prec_max); mpc_init2 (z3, prec_max); mpc_init2 (z4, prec_max); mpc_init2 (z5, prec_max); mpc_init2 (zzzz, 4*prec_max); mpc_init2 (zzzz2, 4*prec_max); special_cases = 4; break; case CC: default: mpc_init2 (z2, prec_max); mpc_init2 (z3, prec_max); mpc_init2 (zzzz, 4*prec_max); special_cases = 4; } exp_min = mpfr_get_emin (); if (exp_max <= 0 || exp_max > mpfr_get_emax ()) exp_max = mpfr_get_emax(); if (-exp_max > exp_min) exp_min = - exp_max; if (step < 1) step = 1; for (prec = prec_min, special = 0; prec <= prec_max || special <= special_cases; prec+=step, special += (prec > prec_max ? 1 : 0)) { /* In the end, test functions in special cases of purely real, purely imaginary or infinite arguments. */ /* probability of one zero part in 256th (25 is almost 10%) */ const unsigned int zero_probability = special != 0 ? 0 : 25; mpc_set_prec (z1, prec); test_default_random (z1, exp_min, exp_max, 128, zero_probability); switch (function.type) { case C_CC: mpc_set_prec (z2, prec); test_default_random (z2, exp_min, exp_max, 128, zero_probability); mpc_set_prec (z3, prec); mpc_set_prec (z4, prec); mpc_set_prec (zzzz, 4*prec); switch (special) { case 1: mpfr_set_ui (mpc_realref (z1), 0, MPFR_RNDN); break; case 2: mpfr_set_inf (mpc_realref (z1), +1); break; case 3: mpfr_set_ui (mpc_imagref (z1), 0, MPFR_RNDN); break; case 4: mpfr_set_inf (mpc_imagref (z1), -1); break; case 5: mpfr_set_ui (mpc_realref (z2), 0, MPFR_RNDN); break; case 6: mpfr_set_inf (mpc_realref (z2), -1); break; case 7: mpfr_set_ui (mpc_imagref (z2), 0, MPFR_RNDN); break; case 8: mpfr_set_inf (mpc_imagref (z2), +1); break; } break; case CCCC: mpc_set_prec (z2, prec); test_default_random (z2, exp_min, exp_max, 128, zero_probability); mpc_set_prec (z3, prec); mpc_set_prec (z4, prec); mpc_set_prec (z5, prec); mpc_set_prec (zzzz, 4*prec); switch (special) { case 1: mpfr_set_ui (mpc_realref (z1), 0, MPFR_RNDN); break; case 2: mpfr_set_inf (mpc_realref (z1), +1); break; case 3: mpfr_set_ui (mpc_imagref (z1), 0, MPFR_RNDN); break; case 4: mpfr_set_inf (mpc_imagref (z1), -1); break; case 5: mpfr_set_ui (mpc_realref (z2), 0, MPFR_RNDN); break; case 6: mpfr_set_inf (mpc_realref (z2), -1); break; case 7: mpfr_set_ui (mpc_imagref (z2), 0, MPFR_RNDN); break; case 8: mpfr_set_inf (mpc_imagref (z2), +1); break; } break; case FC: mpc_set_prec (z2, prec); mpfr_set_prec (x1, prec); mpfr_set_prec (x2, prec); mpfr_set_prec (xxxx, 4*prec); switch (special) { case 1: mpfr_set_ui (mpc_realref (z1), 0, MPFR_RNDN); break; case 2: mpfr_set_inf (mpc_realref (z1), +1); break; case 3: mpfr_set_ui (mpc_imagref (z1), 0, MPFR_RNDN); break; case 4: mpfr_set_inf (mpc_imagref (z1), -1); break; } break; case CCU: case CUC: mpc_set_prec (z2, 128); do { test_default_random (z2, 0, 64, 128, zero_probability); } while (!mpfr_fits_ulong_p (mpc_realref (z2), MPFR_RNDN)); ul1 = mpfr_get_ui (mpc_realref(z2), MPFR_RNDN); mpc_set_prec (z2, prec); mpc_set_prec (z3, prec); mpc_set_prec (zzzz, 4*prec); switch (special) { case 1: mpfr_set_ui (mpc_realref (z1), 0, MPFR_RNDN); break; case 2: mpfr_set_inf (mpc_realref (z1), +1); break; case 3: mpfr_set_ui (mpc_imagref (z1), 0, MPFR_RNDN); break; case 4: mpfr_set_inf (mpc_imagref (z1), -1); break; case 5: ul1 = 0; break; } break; case CUUC: mpc_set_prec (z2, 128); do { test_default_random (z2, 0, 64, 128, zero_probability); } while (!mpfr_fits_ulong_p (mpc_realref (z2), MPFR_RNDN) ||!mpfr_fits_ulong_p (mpc_imagref (z2), MPFR_RNDN)); ul1 = mpfr_get_ui (mpc_realref(z2), MPFR_RNDN); ul2 = mpfr_get_ui (mpc_imagref(z2), MPFR_RNDN); mpc_set_prec (z2, prec); mpc_set_prec (z3, prec); mpc_set_prec (zzzz, 4*prec); switch (special) { case 1: mpfr_set_ui (mpc_realref (z1), 0, MPFR_RNDN); break; case 2: mpfr_set_inf (mpc_realref (z1), +1); break; case 3: mpfr_set_ui (mpc_imagref (z1), 0, MPFR_RNDN); break; case 4: mpfr_set_inf (mpc_imagref (z1), -1); break; case 5: ul1 = 0; break; case 6: ul2 = 0; break; } break; case CCS: mpc_set_prec (z2, 128); do { test_default_random (z2, 0, 64, 128, zero_probability); } while (!mpfr_fits_slong_p (mpc_realref (z2), MPFR_RNDN)); lo = mpfr_get_si (mpc_realref(z2), MPFR_RNDN); mpc_set_prec (z2, prec); mpc_set_prec (z3, prec); mpc_set_prec (zzzz, 4*prec); switch (special) { case 1: mpfr_set_ui (mpc_realref (z1), 0, MPFR_RNDN); break; case 2: mpfr_set_inf (mpc_realref (z1), +1); break; case 3: mpfr_set_ui (mpc_imagref (z1), 0, MPFR_RNDN); break; case 4: mpfr_set_inf (mpc_imagref (z1), -1); break; case 5: lo = 0; break; } break; case CCI: mpc_set_prec (z2, 128); do { test_default_random (z2, 0, 64, 128, zero_probability); } while (!mpfr_fits_slong_p (mpc_realref (z2), MPFR_RNDN)); i = (int)mpfr_get_si (mpc_realref(z2), MPFR_RNDN); mpc_set_prec (z2, prec); mpc_set_prec (z3, prec); mpc_set_prec (zzzz, 4*prec); switch (special) { case 1: mpfr_set_ui (mpc_realref (z1), 0, MPFR_RNDN); break; case 2: mpfr_set_inf (mpc_realref (z1), +1); break; case 3: mpfr_set_ui (mpc_imagref (z1), 0, MPFR_RNDN); break; case 4: mpfr_set_inf (mpc_imagref (z1), -1); break; case 5: i = 0; break; } break; case CCF: case CFC: mpfr_set_prec (x1, prec); mpfr_set (x1, mpc_realref (z1), MPFR_RNDN); test_default_random (z1, exp_min, exp_max, 128, zero_probability); mpc_set_prec (z2, prec); mpc_set_prec (z3, prec); mpc_set_prec (zzzz, 4*prec); switch (special) { case 1: mpfr_set_ui (mpc_realref (z1), 0, MPFR_RNDN); break; case 2: mpfr_set_inf (mpc_realref (z1), +1); break; case 3: mpfr_set_ui (mpc_imagref (z1), 0, MPFR_RNDN); break; case 4: mpfr_set_inf (mpc_imagref (z1), -1); break; case 5: mpfr_set_ui (x1, 0, MPFR_RNDN); break; case 6: mpfr_set_inf (x1, +1); break; } break; case CC_C: mpc_set_prec (z2, prec); mpc_set_prec (z3, prec); mpc_set_prec (z4, prec); mpc_set_prec (z5, prec); mpc_set_prec (zzzz, 4*prec); mpc_set_prec (zzzz2, 4*prec); switch (special) { case 1: mpfr_set_ui (mpc_realref (z1), 0, MPFR_RNDN); break; case 2: mpfr_set_inf (mpc_realref (z1), +1); break; case 3: mpfr_set_ui (mpc_imagref (z1), 0, MPFR_RNDN); break; case 4: mpfr_set_inf (mpc_imagref (z1), -1); break; } break; case CC: default: mpc_set_prec (z2, prec); mpc_set_prec (z3, prec); mpc_set_prec (zzzz, 4*prec); switch (special) { case 1: mpfr_set_ui (mpc_realref (z1), 0, MPFR_RNDN); break; case 2: mpfr_set_inf (mpc_realref (z1), +1); break; case 3: mpfr_set_ui (mpc_imagref (z1), 0, MPFR_RNDN); break; case 4: mpfr_set_inf (mpc_imagref (z1), -1); break; } } for (rnd_re = first_rnd_mode (); is_valid_rnd_mode (rnd_re); rnd_re = next_rnd_mode (rnd_re)) switch (function.type) { case C_CC: for (rnd_im = first_rnd_mode (); is_valid_rnd_mode (rnd_im); rnd_im = next_rnd_mode (rnd_im)) tgeneric_c_cc (&function, z1, z2, z3, zzzz, z4, MPC_RND (rnd_re, rnd_im)); reuse_c_cc (&function, z1, z2, z3, z4); break; case CCCC: for (rnd_im = first_rnd_mode (); is_valid_rnd_mode (rnd_im); rnd_im = next_rnd_mode (rnd_im)) tgeneric_cccc (&function, z1, z2, z3, z4, zzzz, z5, MPC_RND (rnd_re, rnd_im)); reuse_cccc (&function, z1, z2, z3, z4, z5); break; case FC: tgeneric_fc (&function, z1, x1, xxxx, x2, rnd_re); reuse_fc (&function, z1, z2, x1); break; case CC: for (rnd_im = first_rnd_mode (); is_valid_rnd_mode (rnd_im); rnd_im = next_rnd_mode (rnd_im)) tgeneric_cc (&function, z1, z2, zzzz, z3, MPC_RND (rnd_re, rnd_im)); reuse_cc (&function, z1, z2, z3); break; case CC_C: for (rnd_im = first_rnd_mode (); is_valid_rnd_mode (rnd_im); rnd_im = next_rnd_mode (rnd_im)) for (rnd2_re = first_rnd_mode (); is_valid_rnd_mode (rnd2_re); rnd2_re = next_rnd_mode (rnd2_re)) for (rnd2_im = first_rnd_mode (); is_valid_rnd_mode (rnd2_im); rnd2_im = next_rnd_mode (rnd2_im)) tgeneric_cc_c (&function, z1, z2, z3, zzzz, zzzz2, z4, z5, MPC_RND (rnd_re, rnd_im), MPC_RND (rnd2_re, rnd2_im)); reuse_cc_c (&function, z1, z2, z3, z4, z5); break; case CFC: for (rnd_im = first_rnd_mode (); is_valid_rnd_mode (rnd_im); rnd_im = next_rnd_mode (rnd_im)) tgeneric_cfc (&function, x1, z1, z2, zzzz, z3, MPC_RND (rnd_re, rnd_im)); reuse_cfc (&function, z1, x1, z2, z3); break; case CCF: for (rnd_im = first_rnd_mode (); is_valid_rnd_mode (rnd_im); rnd_im = next_rnd_mode (rnd_im)) tgeneric_ccf (&function, z1, x1, z2, zzzz, z3, MPC_RND (rnd_re, rnd_im)); reuse_ccf (&function, z1, x1, z2, z3); break; case CCU: for (rnd_im = first_rnd_mode (); is_valid_rnd_mode (rnd_im); rnd_im = next_rnd_mode (rnd_im)) tgeneric_ccu (&function, z1, ul1, z2, zzzz, z3, MPC_RND (rnd_re, rnd_im)); reuse_ccu (&function, z1, ul1, z2, z3); break; case CUC: for (rnd_im = first_rnd_mode (); is_valid_rnd_mode (rnd_im); rnd_im = next_rnd_mode (rnd_im)) tgeneric_cuc (&function, ul1, z1, z2, zzzz, z3, MPC_RND (rnd_re, rnd_im)); reuse_cuc (&function, ul1, z1, z2, z3); break; case CCS: for (rnd_im = first_rnd_mode (); is_valid_rnd_mode (rnd_im); rnd_im = next_rnd_mode (rnd_im)) tgeneric_ccs (&function, z1, lo, z2, zzzz, z3, MPC_RND (rnd_re, rnd_im)); reuse_ccs (&function, z1, lo, z2, z3); break; case CCI: for (rnd_im = first_rnd_mode (); is_valid_rnd_mode (rnd_im); rnd_im = next_rnd_mode (rnd_im)) tgeneric_cci (&function, z1, i, z2, zzzz, z3, MPC_RND (rnd_re, rnd_im)); reuse_cci (&function, z1, i, z2, z3); break; case CUUC: for (rnd_im = first_rnd_mode (); is_valid_rnd_mode (rnd_im); rnd_im = next_rnd_mode (rnd_im)) tgeneric_cuuc (&function, ul1, ul2, z1, z2, zzzz, z3, MPC_RND (rnd_re, rnd_im)); reuse_cuuc (&function, ul1, ul2, z1, z2, z3); break; default: printf ("tgeneric not yet implemented for this kind of" "function\n"); exit (1); } } mpc_clear (z1); switch (function.type) { case C_CC: mpc_clear (z2); mpc_clear (z3); mpc_clear (z4); mpc_clear (zzzz); break; case CCCC: mpc_clear (z2); mpc_clear (z3); mpc_clear (z4); mpc_clear (z5); mpc_clear (zzzz); break; case FC: mpc_clear (z2); mpfr_clear (x1); mpfr_clear (x2); mpfr_clear (xxxx); break; case CCF: case CFC: mpfr_clear (x1); mpc_clear (z2); mpc_clear (z3); mpc_clear (zzzz); break; case CC_C: mpc_clear (z2); mpc_clear (z3); mpc_clear (z4); mpc_clear (z5); mpc_clear (zzzz); mpc_clear (zzzz2); break; case CUUC: case CCI: case CCS: case CCU: case CUC: case CC: default: mpc_clear (z2); mpc_clear (z3); mpc_clear (zzzz); } }

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; }

static void test_urandom (long nbtests, mpfr_prec_t prec, mpfr_rnd_t rnd, long bit_index, int verbose) { mpfr_t x; int *tab, size_tab, k, sh, xn; double d, av = 0, var = 0, chi2 = 0, th; mpfr_exp_t emin; mp_size_t limb_index = 0; mp_limb_t limb_mask = 0; long count = 0; int i; int inex = 1; size_tab = (nbtests >= 1000 ? nbtests / 50 : 20); tab = (int *) calloc (size_tab, sizeof(int)); if (tab == NULL) { fprintf (stderr, "trandom: can't allocate memory in test_urandom\n"); exit (1); } mpfr_init2 (x, prec); xn = 1 + (prec - 1) / mp_bits_per_limb; sh = xn * mp_bits_per_limb - prec; if (bit_index >= 0 && bit_index < prec) { /* compute the limb index and limb mask to fetch the bit #bit_index */ limb_index = (prec - bit_index) / mp_bits_per_limb; i = 1 + bit_index - (bit_index / mp_bits_per_limb) * mp_bits_per_limb; limb_mask = MPFR_LIMB_ONE << (mp_bits_per_limb - i); } for (k = 0; k < nbtests; k++) { i = mpfr_urandom (x, RANDS, rnd); inex = (i != 0) && inex; /* check that lower bits are zero */ if (MPFR_MANT(x)[0] & MPFR_LIMB_MASK(sh) && !MPFR_IS_ZERO (x)) { printf ("Error: mpfr_urandom() returns invalid numbers:\n"); mpfr_dump (x); exit (1); } /* check that the value is in [0,1] */ if (mpfr_cmp_ui (x, 0) < 0 || mpfr_cmp_ui (x, 1) > 0) { printf ("Error: mpfr_urandom() returns number outside [0, 1]:\n"); mpfr_dump (x); exit (1); } d = mpfr_get_d1 (x); av += d; var += d*d; i = (int)(size_tab * d); if (d == 1.0) i --; tab[i]++; if (limb_mask && (MPFR_MANT (x)[limb_index] & limb_mask)) count ++; } if (inex == 0) { /* one call in the loop pretended to return an exact number! */ printf ("Error: mpfr_urandom() returns a zero ternary value.\n"); exit (1); } /* coverage test */ emin = mpfr_get_emin (); for (k = 0; k < 5; k++) { set_emin (k+1); inex = mpfr_urandom (x, RANDS, rnd); if (( (rnd == MPFR_RNDZ || rnd == MPFR_RNDD) && (!MPFR_IS_ZERO (x) || inex != -1)) || ((rnd == MPFR_RNDU || rnd == MPFR_RNDA) && (mpfr_cmp_ui (x, 1 << k) != 0 || inex != +1)) || (rnd == MPFR_RNDN && (k > 0 || mpfr_cmp_ui (x, 1 << k) != 0 || inex != +1) && (!MPFR_IS_ZERO (x) || inex != -1))) { printf ("Error: mpfr_urandom() does not handle correctly" " a restricted exponent range.\nemin = %d\n" "rounding mode: %s\nternary value: %d\nrandom value: ", k+1, mpfr_print_rnd_mode (rnd), inex); mpfr_dump (x); exit (1); } } set_emin (emin); mpfr_clear (x); if (!verbose) { free(tab); return; } av /= nbtests; var = (var / nbtests) - av * av; th = (double)nbtests / size_tab; printf ("Average = %.5f\nVariance = %.5f\n", av, var); printf ("Repartition for urandom with rounding mode %s. " "Each integer should be close to %d.\n", mpfr_print_rnd_mode (rnd), (int)th); for (k = 0; k < size_tab; k++) { chi2 += (tab[k] - th) * (tab[k] - th) / th; printf("%d ", tab[k]); if (((k+1) & 7) == 0) printf("\n"); } printf("\nChi2 statistics value (with %d degrees of freedom) : %.5f\n", size_tab - 1, chi2); if (limb_mask) printf ("Bit #%ld is set %ld/%ld = %.1f %% of time\n", bit_index, count, nbtests, count * 100.0 / nbtests); puts (""); free(tab); return; }

static void reduced_expo_range (void) { mpfr_t x; mpz_t z; mpq_t q; mpfr_exp_t emin; int inex; emin = mpfr_get_emin (); set_emin (4); mpfr_init2 (x, 32); mpz_init (z); mpfr_clear_flags (); inex = mpfr_set_ui (x, 17, MPFR_RNDN); MPFR_ASSERTN (inex == 0); mpz_set_ui (z, 3); inex = mpfr_mul_z (x, x, z, MPFR_RNDN); if (inex != 0 || MPFR_IS_NAN (x) || mpfr_cmp_ui (x, 51) != 0) { printf ("Error 1 in reduce_expo_range: expected 51 with inex = 0," " got\n"); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); printf ("with inex = %d\n", inex); exit (1); } inex = mpfr_div_z (x, x, z, MPFR_RNDN); if (inex != 0 || MPFR_IS_NAN (x) || mpfr_cmp_ui (x, 17) != 0) { printf ("Error 2 in reduce_expo_range: expected 17 with inex = 0," " got\n"); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); printf ("with inex = %d\n", inex); exit (1); } inex = mpfr_add_z (x, x, z, MPFR_RNDN); if (inex != 0 || MPFR_IS_NAN (x) || mpfr_cmp_ui (x, 20) != 0) { printf ("Error 3 in reduce_expo_range: expected 20 with inex = 0," " got\n"); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); printf ("with inex = %d\n", inex); exit (1); } inex = mpfr_sub_z (x, x, z, MPFR_RNDN); if (inex != 0 || MPFR_IS_NAN (x) || mpfr_cmp_ui (x, 17) != 0) { printf ("Error 4 in reduce_expo_range: expected 17 with inex = 0," " got\n"); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); printf ("with inex = %d\n", inex); exit (1); } MPFR_ASSERTN (__gmpfr_flags == 0); if (mpfr_cmp_z (x, z) <= 0) { printf ("Error 5 in reduce_expo_range: expected a positive value.\n"); exit (1); } mpz_clear (z); mpq_init (q); mpq_set_ui (q, 1, 1); mpfr_set_ui (x, 16, MPFR_RNDN); inex = mpfr_add_q (x, x, q, MPFR_RNDN); if (inex != 0 || MPFR_IS_NAN (x) || mpfr_cmp_ui (x, 17) != 0) { printf ("Error in reduce_expo_range for 16 + 1/1," " got inex = %d and\nx = ", inex); mpfr_dump (x); exit (1); } inex = mpfr_sub_q (x, x, q, MPFR_RNDN); if (inex != 0 || MPFR_IS_NAN (x) || mpfr_cmp_ui (x, 16) != 0) { printf ("Error in reduce_expo_range for 17 - 1/1," " got inex = %d and\nx = ", inex); mpfr_dump (x); exit (1); } mpq_clear (q); mpfr_clear (x); set_emin (emin); }

static void test_urandomb (long nbtests, mpfr_prec_t prec, int verbose) { mpfr_t x; int *tab, size_tab, k, sh, xn; double d, av = 0, var = 0, chi2 = 0, th; mpfr_exp_t emin; size_tab = (nbtests >= 1000 ? nbtests / 50 : 20); tab = (int *) calloc (size_tab, sizeof(int)); if (tab == NULL) { fprintf (stderr, "trandom: can't allocate memory in test_urandomb\n"); exit (1); } mpfr_init2 (x, prec); xn = 1 + (prec - 1) / mp_bits_per_limb; sh = xn * mp_bits_per_limb - prec; for (k = 0; k < nbtests; k++) { mpfr_urandomb (x, RANDS); /* check that lower bits are zero */ if (MPFR_MANT(x)[0] & MPFR_LIMB_MASK(sh)) { printf ("Error: mpfr_urandomb() returns invalid numbers:\n"); mpfr_print_binary (x); puts (""); exit (1); } d = mpfr_get_d1 (x); av += d; var += d*d; tab[(int)(size_tab * d)]++; } /* coverage test */ emin = mpfr_get_emin (); set_emin (1); /* the generated number in [0,1[ is not in the exponent range, except if it is zero */ k = mpfr_urandomb (x, RANDS); if (MPFR_IS_ZERO(x) == 0 && (k == 0 || mpfr_nan_p (x) == 0)) { printf ("Error in mpfr_urandomb, expected NaN, got "); mpfr_dump (x); exit (1); } set_emin (emin); mpfr_clear (x); if (!verbose) { free(tab); return; } av /= nbtests; var = (var / nbtests) - av * av; th = (double)nbtests / size_tab; printf("Average = %.5f\nVariance = %.5f\n", av, var); printf("Repartition for urandomb. Each integer should be close to %d.\n", (int)th); for (k = 0; k < size_tab; k++) { chi2 += (tab[k] - th) * (tab[k] - th) / th; printf("%d ", tab[k]); if (((k+1) & 7) == 0) printf("\n"); } printf("\nChi2 statistics value (with %d degrees of freedom) : %.5f\n\n", size_tab - 1, chi2); free(tab); return; }

static void bug_mul_div_q_20100818 (void) { mpq_t qa, qb; mpfr_t x1, x2, y1, y2, y3; mpfr_exp_t emin, emax, e; int inex; int rnd; emin = mpfr_get_emin (); emax = mpfr_get_emax (); set_emin (MPFR_EMIN_MIN); set_emax (MPFR_EMAX_MAX); mpq_init (qa); mpq_init (qb); mpfr_inits2 (32, x1, x2, y1, y2, y3, (mpfr_ptr) 0); mpq_set_ui (qa, 3, 17); mpq_set_ui (qb, 17, 3); inex = mpfr_set_ui (x1, 7, MPFR_RNDN); MPFR_ASSERTN (inex == 0); e = MPFR_EMAX_MAX - 3; inex = mpfr_set_ui_2exp (x2, 7, e, MPFR_RNDN); /* x2 = x1 * 2^e */ MPFR_ASSERTN (inex == 0); RND_LOOP(rnd) { mpfr_mul_q (y1, x1, qa, (mpfr_rnd_t) rnd); mpfr_div_q (y3, x1, qb, (mpfr_rnd_t) rnd); MPFR_ASSERTN (mpfr_equal_p (y1, y3)); inex = mpfr_set_ui_2exp (y3, 1, e, MPFR_RNDN); MPFR_ASSERTN (inex == 0); inex = mpfr_mul (y3, y3, y1, MPFR_RNDN); /* y3 = y1 * 2^e */ MPFR_ASSERTN (inex == 0); mpfr_mul_q (y2, x2, qa, (mpfr_rnd_t) rnd); if (! mpfr_equal_p (y2, y3)) { printf ("Error 1 in bug_mul_div_q_20100818 (rnd = %d)\n", rnd); printf ("Expected "); mpfr_dump (y3); printf ("Got "); mpfr_dump (y2); exit (1); } mpfr_div_q (y2, x2, qb, (mpfr_rnd_t) rnd); if (! mpfr_equal_p (y2, y3)) { printf ("Error 2 in bug_mul_div_q_20100818 (rnd = %d)\n", rnd); printf ("Expected "); mpfr_dump (y3); printf ("Got "); mpfr_dump (y2); exit (1); } } e = MPFR_EMIN_MIN; inex = mpfr_set_ui_2exp (x2, 7, e, MPFR_RNDN); /* x2 = x1 * 2^e */ MPFR_ASSERTN (inex == 0); RND_LOOP(rnd) { mpfr_div_q (y1, x1, qa, (mpfr_rnd_t) rnd); mpfr_mul_q (y3, x1, qb, (mpfr_rnd_t) rnd); MPFR_ASSERTN (mpfr_equal_p (y1, y3)); inex = mpfr_set_ui_2exp (y3, 1, e, MPFR_RNDN); MPFR_ASSERTN (inex == 0); inex = mpfr_mul (y3, y3, y1, MPFR_RNDN); /* y3 = y1 * 2^e */ MPFR_ASSERTN (inex == 0); mpfr_div_q (y2, x2, qa, (mpfr_rnd_t) rnd); if (! mpfr_equal_p (y2, y3)) { printf ("Error 3 in bug_mul_div_q_20100818 (rnd = %d)\n", rnd); printf ("Expected "); mpfr_dump (y3); printf ("Got "); mpfr_dump (y2); exit (1); } mpfr_mul_q (y2, x2, qb, (mpfr_rnd_t) rnd); if (! mpfr_equal_p (y2, y3)) { printf ("Error 4 in bug_mul_div_q_20100818 (rnd = %d)\n", rnd); printf ("Expected "); mpfr_dump (y3); printf ("Got "); mpfr_dump (y2); exit (1); } } mpq_clear (qa); mpq_clear (qb); mpfr_clears (x1, x2, y1, y2, y3, (mpfr_ptr) 0); set_emin (emin); set_emax (emax); }

static void special (void) { mpfr_t x, y; int inex; mpfr_exp_t emin, emax; emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_init2 (x, 53); mpfr_init2 (y, 53); /* Check special case: An overflow in const_pi could occurs! */ set_emin (-125); set_emax (128); mpfr_set_prec (y, 24*2); mpfr_set_prec (x, 24); mpfr_set_str_binary (x, "0.111110101010101011110101E0"); test_log (y, x, MPFR_RNDN); set_emin (emin); set_emax (emax); mpfr_set_prec (y, 53); mpfr_set_prec (x, 53); mpfr_set_ui (x, 3, MPFR_RNDD); test_log (y, x, MPFR_RNDD); if (mpfr_cmp_str1 (y, "1.09861228866810956")) { printf ("Error in mpfr_log(3) for MPFR_RNDD\n"); exit (1); } /* check large precision */ mpfr_set_prec (x, 3322); mpfr_set_prec (y, 3322); mpfr_set_ui (x, 3, MPFR_RNDN); mpfr_sqrt (x, x, MPFR_RNDN); test_log (y, x, MPFR_RNDN); /* negative argument */ mpfr_set_si (x, -1, MPFR_RNDN); test_log (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (y)); /* infinite loop when */ set_emax (128); mpfr_set_prec (x, 251); mpfr_set_prec (y, 251); mpfr_set_str_binary (x, "0.10010111000000000001101E8"); /* x = 4947981/32768, log(x) ~ 5.017282... */ test_log (y, x, MPFR_RNDN); set_emax (emax); mpfr_set_ui (x, 0, MPFR_RNDN); inex = test_log (y, x, MPFR_RNDN); MPFR_ASSERTN (inex == 0); MPFR_ASSERTN (mpfr_inf_p (y)); MPFR_ASSERTN (mpfr_sgn (y) < 0); mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); inex = test_log (y, x, MPFR_RNDN); MPFR_ASSERTN (inex == 0); MPFR_ASSERTN (mpfr_inf_p (y)); MPFR_ASSERTN (mpfr_sgn (y) < 0); mpfr_clear (x); mpfr_clear (y); }

int main (void) { mpfr_t x; mpfr_exp_t emin; tests_start_mpfr (); emin = mpfr_get_emin (); mpfr_init2(x, IEEE_DBL_MANT_DIG); mpfr_set_d (x, 2.34763465, MPFR_RNDN); if (mpfr_cmp_d(x, 2.34763465)!=0) { printf("Error in mpfr_cmp_d 2.34763465 and "); mpfr_out_str(stdout, 10, 0, x, MPFR_RNDN); putchar('\n'); exit(1); } if (mpfr_cmp_d(x, 2.345)<=0) { printf("Error in mpfr_cmp_d 2.345 and "); mpfr_out_str(stdout, 10, 0, x, MPFR_RNDN); putchar('\n'); exit(1); } if (mpfr_cmp_d(x, 2.4)>=0) { printf("Error in mpfr_cmp_d 2.4 and "); mpfr_out_str(stdout, 10, 0, x, MPFR_RNDN); putchar('\n'); exit(1); } mpfr_set_ui (x, 0, MPFR_RNDZ); mpfr_neg (x, x, MPFR_RNDZ); if (mpfr_cmp_d (x, 0.0)) { printf("Error in mpfr_cmp_d 0.0 and "); mpfr_out_str(stdout, 10, 0, x, MPFR_RNDN); putchar('\n'); exit(1); } mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_ui_div (x, 1, x, MPFR_RNDU); if (mpfr_cmp_d (x, 0.0) == 0) { printf ("Error in mpfr_cmp_d (Inf, 0)\n"); exit (1); } /* Test in reduced exponent range. */ set_emin (1); mpfr_set_ui (x, 1, MPFR_RNDN); if (mpfr_cmp_d (x, 0.9) <= 0) { printf ("Error in reduced exponent range.\n"); exit (1); } set_emin (emin); #if !defined(MPFR_ERRDIVZERO) /* Check NAN */ { int c; mpfr_clear_flags (); c = mpfr_cmp_d (x, DBL_NAN); if (c != 0 || __gmpfr_flags != MPFR_FLAGS_ERANGE) { printf ("ERROR for NAN (1)\n"); printf ("Expected 0, got %d\n", c); printf ("Expected flags:"); flags_out (MPFR_FLAGS_ERANGE); printf ("Got flags: "); flags_out (__gmpfr_flags); #ifdef MPFR_NANISNAN printf ("The reason is that NAN == NAN. Please look at the configure " "output\nand Section \"In case of problem\" of the INSTALL " "file.\n"); #endif exit (1); } mpfr_set_nan (x); mpfr_clear_flags (); c = mpfr_cmp_d (x, 2.0); if (c != 0 || __gmpfr_flags != MPFR_FLAGS_ERANGE) { printf ("ERROR for NAN (2)\n"); printf ("Expected 0, got %d\n", c); printf ("Expected flags:"); flags_out (MPFR_FLAGS_ERANGE); printf ("Got flags: "); flags_out (__gmpfr_flags); #ifdef MPFR_NANISNAN printf ("The reason is that NAN == NAN. Please look at the configure " "output\nand Section \"In case of problem\" of the INSTALL " "file.\n"); #endif exit (1); } } #endif /* MPFR_ERRDIVZERO */ mpfr_clear(x); tests_end_mpfr (); return 0; }