int main (void) { mpfr_t x; tests_start_mpfr (); mpfr_init2 (x, 256); /* Check NAN */ mpfr_set_nan(x); if (mpfr_fits_ulong_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_slong_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_uint_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_sint_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_ushort_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_sshort_p(x, GMP_RNDN)) ERROR1; /* Check INF */ mpfr_set_inf(x, 1); if (mpfr_fits_ulong_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_slong_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_uint_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_sint_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_ushort_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_sshort_p(x, GMP_RNDN)) ERROR1; /* Check Zero */ MPFR_SET_ZERO(x); if (!mpfr_fits_ulong_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_slong_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_uint_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_sint_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_ushort_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_sshort_p(x, GMP_RNDN)) ERROR2; /* Check small op */ mpfr_set_str1 (x, "1@-1"); if (!mpfr_fits_ulong_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_slong_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_uint_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_sint_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_ushort_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_sshort_p(x, GMP_RNDN)) ERROR2; /* Check all other values */ mpfr_set_ui(x, ULONG_MAX, GMP_RNDN); mpfr_mul_2exp(x, x, 1, GMP_RNDN); if (mpfr_fits_ulong_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_slong_p(x, GMP_RNDN)) ERROR1; mpfr_mul_2exp(x, x, 40, GMP_RNDN); if (mpfr_fits_ulong_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_uint_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_sint_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_ushort_p(x, GMP_RNDN)) ERROR1; if (mpfr_fits_sshort_p(x, GMP_RNDN)) ERROR1; mpfr_set_ui(x, ULONG_MAX, GMP_RNDN); if (!mpfr_fits_ulong_p(x, GMP_RNDN)) ERROR2; mpfr_set_ui(x, LONG_MAX, GMP_RNDN); if (!mpfr_fits_slong_p(x, GMP_RNDN)) ERROR2; mpfr_set_ui(x, UINT_MAX, GMP_RNDN); if (!mpfr_fits_uint_p(x, GMP_RNDN)) ERROR2; mpfr_set_ui(x, INT_MAX, GMP_RNDN); if (!mpfr_fits_sint_p(x, GMP_RNDN)) ERROR2; mpfr_set_ui(x, USHRT_MAX, GMP_RNDN); if (!mpfr_fits_ushort_p(x, GMP_RNDN)) ERROR2; mpfr_set_ui(x, SHRT_MAX, GMP_RNDN); if (!mpfr_fits_sshort_p(x, GMP_RNDN)) ERROR2; mpfr_set_si(x, 1, GMP_RNDN); if (!mpfr_fits_sint_p(x, GMP_RNDN)) ERROR2; if (!mpfr_fits_sshort_p(x, GMP_RNDN)) ERROR2; mpfr_clear (x); check_intmax (); tests_end_mpfr (); return 0; }
/* bugs found by Alain Delplanque */ static void check_large (void) { mpfr_t x; char *s, s1[7]; const char xm[] = { '1', '1', '9', '1', '3', '2', '9', '3', '7', '3', '5', '8', '4', '4', '5', '4', '9', '0', '2', '9', '6', '3', '4', '4', '6', '9', '9', '1', '9', '5', '5', '7', '2', '0', '1', '7', '5', '2', '8', '6', '1', '2', '5', '2', '5', '2', '7', '4', '0', '2', '7', '9', '1', '1', '7', '4', '5', '6', '7', '5', '9', '3', '1', '4', '2', '5', '5', '6', '6', '6', '1', '6', '4', '3', '8', '1', '2', '8', '7', '6', '2', '9', '2', '0', '8', '8', '9', '4', '3', '9', '6', '2', '8', '4', '1', '1', '8', '1', '0', '6', '2', '3', '7', '6', '3', '8', '1', '5', '1', '7', '3', '4', '6', '1', '2', '4', '0', '1', '3', '0', '8', '4', '1', '3', '9', '3', '2', '0', '1', '6', '3', '6', '7', '1', '5', '1', '7', '5', '0', '1', '9', '8', '4', '0', '8', '2', '7', '9', '1', '3', '2', '2', '8', '3', '4', '1', '6', '2', '3', '9', '6', '2', '0', '7', '3', '5', '5', '5', '3', '4', '2', '1', '7', '0', '9', '7', '6', '2', '1', '0', '3', '3', '5', '4', '7', '6', '0', '9', '7', '6', '9', '3', '5', '1', '7', '8', '6', '8', '8', '2', '8', '1', '4', '3', '7', '4', '3', '3', '2', '4', '1', '5', '4', '7', '8', '1', '1', '4', '2', '1', '2', '4', '2', '7', '6', '5', '9', '5', '4', '5', '2', '6', '7', '3', '0', '3', '4', '0', '6', '9', '1', '8', '9', '9', '9', '8', '0', '5', '7', '0', '9', '3', '8', '7', '6', '2', '4', '6', '1', '6', '7', '2', '0', '3', '5', '9', '3', '5', '8', '8', '9', '7', '7', '9', '2', '7', '0', '8', '1', '6', '8', '7', '4', '8', '5', '3', '0', '8', '4', '3', '5', '6', '5', '1', '6', '6', '0', '9', '7', '9', '8', '9', '2', '7', '2', '6', '8', '5', '9', '4', '5', '8', '1', '3', '7', '2', '9', '3', '8', '3', '7', '9', '1', '7', '9', '9', '7', '7', '2', '8', '4', '6', '5', '5', '7', '3', '3', '8', '3', '6', '6', '9', '7', '1', '4', '3', '3', '7', '1', '4', '9', '4', '1', '2', '4', '9', '5', '1', '4', '7', '2', '6', '4', '4', '8', '0', '6', '2', '6', '0', '6', '9', '8', '1', '1', '7', '9', '9', '3', '9', '3', '8', '4', '7', '3', '1', '9', '0', '2', '3', '5', '3', '5', '4', '2', '1', '1', '7', '6', '7', '4', '3', '2', '2', '0', '6', '5', '9', '9', '3', '2', '6', '7', '1', '2', '0', '0', '3', '7', '3', '8', '7', '4', '3', '3', '3', '3', '3', '2', '3', '8', '2', '8', '6', '3', '1', '5', '5', '2', '2', '5', '9', '3', '3', '7', '0', '6', '2', '8', '1', '0', '3', '6', '7', '6', '9', '6', '5', '9', '0', '6', '6', '6', '3', '6', '9', '9', '3', '8', '7', '6', '5', '4', '5', '3', '5', '9', '4', '0', '0', '7', '5', '8', '5', '4', '1', '4', '3', '1', '5', '7', '6', '6', '3', '4', '4', '5', '0', '8', '7', '5', '7', '5', '0', '1', '0', '1', '8', '4', '7', '3', '1', '9', '9', '2', '7', '1', '1', '1', '2', '3', '9', '9', '6', '5', '9', '2', '3', '2', '8', '1', '5', '5', '1', '2', '6', '4', '9', '6', '6', '4', '5', '1', '1', '6', '0', '0', '3', '2', '8', '4', '8', '7', '1', '4', '9', '6', '8', '1', '6', '5', '9', '8', '3', '4', '2', '9', '7', '0', '1', '9', '2', '6', '6', '9', '1', '3', '5', '9', '3', '2', '9', '6', '2', '3', '0', '6', '0', '1', '1', '6', '5', '1', '7', '9', '0', '7', '5', '8', '6', '8', '4', '2', '1', '0', '3', '8', '6', '6', '4', '4', '9', '9', '7', '5', '8', '1', '7', '5', '7', '9', '6', '6', '8', '8', '5', '8', '6', '7', '4', '0', '7', '2', '0', '2', '9', '9', '4', '4', '1', '9', '5', '8', '6', '5', '0', '6', '7', '4', '2', '7', '3', '2', '3', '2', '7', '0', '2', '1', '3', '0', '5', '9', '0', '3', '9', '1', '4', '5', '3', '7', '2', '7', '0', '8', '5', '5', '4', '6', '1', '1', '0', '0', '9', '2', '0', '4', '1', '6', '6', '4', '6', '9', '1', '3', '2', '8', '5', '0', '3', '3', '8', '9', '8', '7', '8', '5', '9', '5', '5', '9', '1', '9', '3', '6', '5', '4', '1', '7', '4', '0', '2', '4', '7', '2', '9', '7', '1', '2', '4', '5', '8', '1', '4', '4', '6', '1', '8', '5', '8', '7', '6', '9', '7', '2', '1', '2', '0', '8', '9', '5', '9', '5', '5', '3', '8', '1', '2', '5', '4', '3', '0', '7', '6', '5', '1', '7', '8', '2', '0', '0', '7', '6', '7', '4', '8', '1', '0', '6', '3', '2', '3', '0', '5', '2', '5', '0', '1', '1', '4', '3', '8', '4', '5', '2', '3', '9', '5', '0', '9', '8', '2', '6', '4', '7', '4', '8', '0', '1', '1', '7', '1', '5', '4', '9', '0', '9', '2', '2', '3', '8', '1', '6', '9', '0', '4', '6', '4', '5', '4', '6', '3', '8', '7', '3', '6', '1', '7', '2', '3', '4', '5', '5', '2', '0', '2', '5', '8', '1', '4', '9', '3', '0', '7', '4', '1', '6', '8', '7', '8', '2', '6', '2', '5', '1', '0', '7', '4', '7', '3', '6', '6', '4', '5', '6', '6', '6', '6', '8', '5', '1', '3', '5', '7', '1', '6', '2', '0', '9', '2', '3', '2', '6', '0', '7', '9', '8', '1', '6', '2', '0', '3', '8', '8', '0', '2', '8', '7', '7', '5', '9', '3', '1', '0', '6', '7', '5', '7', '3', '1', '2', '7', '7', '2', '0', '0', '4', '1', '2', '8', '2', '0', '8', '4', '0', '5', '0', '5', '0', '1', '9', '3', '3', '6', '3', '6', '9', '6', '2', '8', '2', '9', '7', '5', '3', '8', '8', '9', '1', '1', '4', '5', '7', '7', '5', '6', '0', '2', '7', '9', '7', '2', '1', '7', '4', '3', '0', '3', '6', '7', '3', '7', '2', '2', '7', '5', '6', '2', '3', '1', '2', '1', '3', '1', '4', '2', '6', '9', '2', '3', '\0' }; mpfr_exp_t e; mpfr_init2 (x, 3322); mpfr_set_str (x, xm, 10, MPFR_RNDN); mpfr_div_2exp (x, x, 4343, MPFR_RNDN); s = mpfr_get_str (NULL, &e, 10, 1000, x, MPFR_RNDN); if (s[999] != '1') /* s must be 5.04383...689071e-309 */ { printf ("Error in check_large: expected '689071', got '%s'\n", s + 994); exit (1); } mpfr_free_str (s); mpfr_mul_2exp (x, x, 4343, MPFR_RNDN); s = mpfr_get_str (NULL, &e, 10, 2, x, MPFR_RNDN); if (strcmp (s, "12") || (e != 1000)) { printf ("Error in check_large: expected 0.12e1000\n"); printf ("got %se%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_nan (x); mpfr_clear_flags (); s = mpfr_get_str (NULL, &e, 10, 1000, x, MPFR_RNDN); if (strcmp (s, "@NaN@")) { printf ("Error for NaN (incorrect string)\n"); exit (1); } if (__gmpfr_flags != MPFR_FLAGS_NAN) { printf ("Error for NaN (incorrect flags)\n"); exit (1); } mpfr_free_str (s); mpfr_get_str (s1, &e, 10, 1000, x, MPFR_RNDN); mpfr_set_inf (x, 1); s = mpfr_get_str (NULL, &e, 10, 1000, x, MPFR_RNDN); if (strcmp (s, "@Inf@")) { printf ("Error for Inf\n"); exit (1); } mpfr_free_str (s); mpfr_get_str (s1, &e, 10, 1000, x, MPFR_RNDN); mpfr_set_inf (x, -1); s = mpfr_get_str (NULL, &e, 10, 1000, x, MPFR_RNDN); if (strcmp (s, "-@Inf@")) { printf ("Error for -Inf\n"); exit (1); } mpfr_free_str (s); mpfr_get_str (s1, &e, 10, 1000, x, MPFR_RNDN); mpfr_set_ui (x, 0, MPFR_RNDN); s = mpfr_get_str (NULL, &e, 10, 2, x, MPFR_RNDN); if (e != 0 || strcmp (s, "00")) { printf ("Error for 0.0\n"); exit (1); } mpfr_free_str (s); mpfr_get_str (s1, &e, 10, 2, x, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); /* -0.0 */ s = mpfr_get_str (NULL, &e, 10, 2, x, MPFR_RNDN); if (e != 0 || strcmp (s, "-00")) { printf ("Error for -0.0\ngot %se%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_get_str (s1, &e, 10, 2, x, MPFR_RNDN); mpfr_clear (x); }
int main (void) { mpfr_t x, y; int i, r; tests_start_mpfr (); mpfr_init2 (x, 256); mpfr_init2 (y, 8); RND_LOOP (r) { /* Check NAN */ mpfr_set_nan (x); if (mpfr_fits_ulong_p (x, (mpfr_rnd_t) r)) ERROR1 (1); if (mpfr_fits_slong_p (x, (mpfr_rnd_t) r)) ERROR1 (2); if (mpfr_fits_uint_p (x, (mpfr_rnd_t) r)) ERROR1 (3); if (mpfr_fits_sint_p (x, (mpfr_rnd_t) r)) ERROR1 (4); if (mpfr_fits_ushort_p (x, (mpfr_rnd_t) r)) ERROR1 (5); if (mpfr_fits_sshort_p (x, (mpfr_rnd_t) r)) ERROR1 (6); /* Check INF */ mpfr_set_inf (x, 1); if (mpfr_fits_ulong_p (x, (mpfr_rnd_t) r)) ERROR1 (7); if (mpfr_fits_slong_p (x, (mpfr_rnd_t) r)) ERROR1 (8); if (mpfr_fits_uint_p (x, (mpfr_rnd_t) r)) ERROR1 (9); if (mpfr_fits_sint_p (x, (mpfr_rnd_t) r)) ERROR1 (10); if (mpfr_fits_ushort_p (x, (mpfr_rnd_t) r)) ERROR1 (11); if (mpfr_fits_sshort_p (x, (mpfr_rnd_t) r)) ERROR1 (12); /* Check Zero */ MPFR_SET_ZERO (x); if (!mpfr_fits_ulong_p (x, (mpfr_rnd_t) r)) ERROR1 (13); if (!mpfr_fits_slong_p (x, (mpfr_rnd_t) r)) ERROR1 (14); if (!mpfr_fits_uint_p (x, (mpfr_rnd_t) r)) ERROR1 (15); if (!mpfr_fits_sint_p (x, (mpfr_rnd_t) r)) ERROR1 (16); if (!mpfr_fits_ushort_p (x, (mpfr_rnd_t) r)) ERROR1 (17); if (!mpfr_fits_sshort_p (x, (mpfr_rnd_t) r)) ERROR1 (18); /* Check small positive op */ mpfr_set_str1 (x, "1@-1"); if (!mpfr_fits_ulong_p (x, (mpfr_rnd_t) r)) ERROR1 (19); if (!mpfr_fits_slong_p (x, (mpfr_rnd_t) r)) ERROR1 (20); if (!mpfr_fits_uint_p (x, (mpfr_rnd_t) r)) ERROR1 (21); if (!mpfr_fits_sint_p (x, (mpfr_rnd_t) r)) ERROR1 (22); if (!mpfr_fits_ushort_p (x, (mpfr_rnd_t) r)) ERROR1 (23); if (!mpfr_fits_sshort_p (x, (mpfr_rnd_t) r)) ERROR1 (24); /* Check 17 */ mpfr_set_ui (x, 17, MPFR_RNDN); if (!mpfr_fits_ulong_p (x, (mpfr_rnd_t) r)) ERROR1 (25); if (!mpfr_fits_slong_p (x, (mpfr_rnd_t) r)) ERROR1 (26); if (!mpfr_fits_uint_p (x, (mpfr_rnd_t) r)) ERROR1 (27); if (!mpfr_fits_sint_p (x, (mpfr_rnd_t) r)) ERROR1 (28); if (!mpfr_fits_ushort_p (x, (mpfr_rnd_t) r)) ERROR1 (29); if (!mpfr_fits_sshort_p (x, (mpfr_rnd_t) r)) ERROR1 (30); /* Check all other values */ mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN); mpfr_mul_2exp (x, x, 1, MPFR_RNDN); if (mpfr_fits_ulong_p (x, (mpfr_rnd_t) r)) ERROR1 (31); if (mpfr_fits_slong_p (x, (mpfr_rnd_t) r)) ERROR1 (32); mpfr_mul_2exp (x, x, 40, MPFR_RNDN); if (mpfr_fits_ulong_p (x, (mpfr_rnd_t) r)) ERROR1 (33); if (mpfr_fits_uint_p (x, (mpfr_rnd_t) r)) ERROR1 (34); if (mpfr_fits_sint_p (x, (mpfr_rnd_t) r)) ERROR1 (35); if (mpfr_fits_ushort_p (x, (mpfr_rnd_t) r)) ERROR1 (36); if (mpfr_fits_sshort_p (x, (mpfr_rnd_t) r)) ERROR1 (37); mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN); if (!mpfr_fits_ulong_p (x, (mpfr_rnd_t) r)) ERROR1 (38); mpfr_set_ui (x, LONG_MAX, MPFR_RNDN); if (!mpfr_fits_slong_p (x, (mpfr_rnd_t) r)) ERROR1 (39); mpfr_set_ui (x, UINT_MAX, MPFR_RNDN); if (!mpfr_fits_uint_p (x, (mpfr_rnd_t) r)) ERROR1 (40); mpfr_set_ui (x, INT_MAX, MPFR_RNDN); if (!mpfr_fits_sint_p (x, (mpfr_rnd_t) r)) ERROR1 (41); mpfr_set_ui (x, USHRT_MAX, MPFR_RNDN); if (!mpfr_fits_ushort_p (x, (mpfr_rnd_t) r)) ERROR1 (42); mpfr_set_ui (x, SHRT_MAX, MPFR_RNDN); if (!mpfr_fits_sshort_p (x, (mpfr_rnd_t) r)) ERROR1 (43); mpfr_set_si (x, 1, MPFR_RNDN); if (!mpfr_fits_sint_p (x, (mpfr_rnd_t) r)) ERROR1 (44); if (!mpfr_fits_sshort_p (x, (mpfr_rnd_t) r)) ERROR1 (45); /* Check negative op */ for (i = 1; i <= 4; i++) { int inv; mpfr_set_si_2exp (x, -i, -2, MPFR_RNDN); mpfr_rint (y, x, (mpfr_rnd_t) r); inv = MPFR_NOTZERO (y); if (!mpfr_fits_ulong_p (x, (mpfr_rnd_t) r) ^ inv) ERROR1 (46); if (!mpfr_fits_slong_p (x, (mpfr_rnd_t) r)) ERROR1 (47); if (!mpfr_fits_uint_p (x, (mpfr_rnd_t) r) ^ inv) ERROR1 (48); if (!mpfr_fits_sint_p (x, (mpfr_rnd_t) r)) ERROR1 (49); if (!mpfr_fits_ushort_p (x, (mpfr_rnd_t) r) ^ inv) ERROR1 (50); if (!mpfr_fits_sshort_p (x, (mpfr_rnd_t) r)) ERROR1 (51); } } mpfr_clear (x); mpfr_clear (y); check_intmax (); tests_end_mpfr (); return 0; }
static void special (void) { mpfr_t x, y, z; int inexact; mpfr_prec_t p; mpfr_init (x); mpfr_init (y); mpfr_init (z); mpfr_set_prec (x, 64); mpfr_set_str_binary (x, "1010000010100011011001010101010010001100001101011101110001011001E-1"); mpfr_set_prec (y, 32); test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 2405743844UL)) { printf ("Error for n^2+n+1/2 with n=2405743843\n"); exit (1); } mpfr_set_prec (x, 65); mpfr_set_str_binary (x, "10100000101000110110010101010100100011000011010111011100010110001E-2"); mpfr_set_prec (y, 32); test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 2405743844UL)) { printf ("Error for n^2+n+1/4 with n=2405743843\n"); mpfr_dump (y); exit (1); } mpfr_set_prec (x, 66); mpfr_set_str_binary (x, "101000001010001101100101010101001000110000110101110111000101100011E-3"); mpfr_set_prec (y, 32); test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 2405743844UL)) { printf ("Error for n^2+n+1/4+1/8 with n=2405743843\n"); mpfr_dump (y); exit (1); } mpfr_set_prec (x, 66); mpfr_set_str_binary (x, "101000001010001101100101010101001000110000110101110111000101100001E-3"); mpfr_set_prec (y, 32); test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 2405743843UL)) { printf ("Error for n^2+n+1/8 with n=2405743843\n"); mpfr_dump (y); exit (1); } mpfr_set_prec (x, 27); mpfr_set_str_binary (x, "0.110100111010101000010001011"); if ((inexact = test_sqrt (x, x, MPFR_RNDZ)) >= 0) { printf ("Wrong inexact flag: expected -1, got %d\n", inexact); exit (1); } mpfr_set_prec (x, 2); for (p=2; p<1000; p++) { mpfr_set_prec (z, p); mpfr_set_ui (z, 1, MPFR_RNDN); mpfr_nexttoinf (z); test_sqrt (x, z, MPFR_RNDU); if (mpfr_cmp_ui_2exp(x, 3, -1)) { printf ("Error: sqrt(1+ulp(1), up) should give 1.5 (prec=%u)\n", (unsigned int) p); printf ("got "); mpfr_print_binary (x); puts (""); exit (1); } } /* check inexact flag */ mpfr_set_prec (x, 5); mpfr_set_str_binary (x, "1.1001E-2"); if ((inexact = test_sqrt (x, x, MPFR_RNDN))) { printf ("Wrong inexact flag: expected 0, got %d\n", inexact); exit (1); } mpfr_set_prec (x, 2); mpfr_set_prec (z, 2); /* checks the sign is correctly set */ mpfr_set_si (x, 1, MPFR_RNDN); mpfr_set_si (z, -1, MPFR_RNDN); test_sqrt (z, x, MPFR_RNDN); if (mpfr_cmp_ui (z, 0) < 0) { printf ("Error: square root of 1 gives "); mpfr_print_binary(z); putchar('\n'); exit (1); } mpfr_set_prec (x, 192); mpfr_set_prec (z, 160); mpfr_set_str_binary (z, "0.1011010100000100100100100110011001011100100100000011000111011001011101101101110000110100001000100001100001011000E1"); mpfr_set_prec (x, 160); test_sqrt(x, z, MPFR_RNDN); test_sqrt(z, x, MPFR_RNDN); mpfr_set_prec (x, 53); mpfr_set_str (x, "8093416094703476.0", 10, MPFR_RNDN); mpfr_div_2exp (x, x, 1075, MPFR_RNDN); test_sqrt (x, x, MPFR_RNDN); mpfr_set_str (z, "1e55596835b5ef@-141", 16, MPFR_RNDN); if (mpfr_cmp (x, z)) { printf ("Error: square root of 8093416094703476*2^(-1075)\n"); printf ("expected "); mpfr_dump (z); printf ("got "); mpfr_dump (x); exit (1); } mpfr_set_prec (x, 33); mpfr_set_str_binary (x, "0.111011011011110001100111111001000e-10"); mpfr_set_prec (z, 157); inexact = test_sqrt (z, x, MPFR_RNDN); mpfr_set_prec (x, 157); mpfr_set_str_binary (x, "0.11110110101100101111001011100011100011100001101010111011010000100111011000111110100001001011110011111100101110010110010110011001011011010110010000011001101E-5"); if (mpfr_cmp (x, z)) { printf ("Error: square root (1)\n"); exit (1); } if (inexact <= 0) { printf ("Error: wrong inexact flag (1)\n"); exit (1); } /* case prec(result) << prec(input) */ mpfr_set_prec (z, 2); for (p = 2; p < 1000; p++) { mpfr_set_prec (x, p); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_nextabove (x); /* 1.0 < x <= 1.5 thus 1 < sqrt(x) <= 1.23 */ inexact = test_sqrt (z, x, MPFR_RNDN); MPFR_ASSERTN(inexact < 0 && mpfr_cmp_ui (z, 1) == 0); inexact = test_sqrt (z, x, MPFR_RNDZ); MPFR_ASSERTN(inexact < 0 && mpfr_cmp_ui (z, 1) == 0); inexact = test_sqrt (z, x, MPFR_RNDU); MPFR_ASSERTN(inexact > 0 && mpfr_cmp_ui_2exp (z, 3, -1) == 0); inexact = test_sqrt (z, x, MPFR_RNDD); MPFR_ASSERTN(inexact < 0 && mpfr_cmp_ui (z, 1) == 0); inexact = test_sqrt (z, x, MPFR_RNDA); MPFR_ASSERTN(inexact > 0 && mpfr_cmp_ui_2exp (z, 3, -1) == 0); } /* corner case rw = 0 in rounding to nearest */ mpfr_set_prec (z, GMP_NUMB_BITS - 1); mpfr_set_prec (y, GMP_NUMB_BITS - 1); mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_mul_2exp (y, y, GMP_NUMB_BITS - 1, MPFR_RNDN); mpfr_nextabove (y); for (p = 2 * GMP_NUMB_BITS - 1; p <= 1000; p++) { mpfr_set_prec (x, p); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_set_exp (x, GMP_NUMB_BITS); mpfr_add_ui (x, x, 1, MPFR_RNDN); /* now x = 2^(GMP_NUMB_BITS - 1) + 1 (GMP_NUMB_BITS bits) */ inexact = mpfr_mul (x, x, x, MPFR_RNDN); MPFR_ASSERTN (inexact == 0); /* exact */ inexact = test_sqrt (z, x, MPFR_RNDN); /* even rule: z should be 2^(GMP_NUMB_BITS - 1) */ MPFR_ASSERTN (inexact < 0); inexact = mpfr_cmp_ui_2exp (z, 1, GMP_NUMB_BITS - 1); MPFR_ASSERTN (inexact == 0); mpfr_nextbelow (x); /* now x is just below [2^(GMP_NUMB_BITS - 1) + 1]^2 */ inexact = test_sqrt (z, x, MPFR_RNDN); MPFR_ASSERTN(inexact < 0 && mpfr_cmp_ui_2exp (z, 1, GMP_NUMB_BITS - 1) == 0); mpfr_nextabove (x); mpfr_nextabove (x); /* now x is just above [2^(GMP_NUMB_BITS - 1) + 1]^2 */ inexact = test_sqrt (z, x, MPFR_RNDN); if (mpfr_cmp (z, y)) { printf ("Error for sqrt(x) in rounding to nearest\n"); printf ("x="); mpfr_dump (x); printf ("Expected "); mpfr_dump (y); printf ("Got "); mpfr_dump (z); exit (1); } if (inexact <= 0) { printf ("Wrong inexact flag in corner case for p = %lu\n", (unsigned long) p); exit (1); } } mpfr_set_prec (x, 1000); mpfr_set_ui (x, 9, MPFR_RNDN); mpfr_set_prec (y, 10); inexact = test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 3) || inexact != 0) { printf ("Error in sqrt(9:1000) for prec=10\n"); exit (1); } mpfr_set_prec (y, GMP_NUMB_BITS); mpfr_nextabove (x); inexact = test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 3) || inexact >= 0) { printf ("Error in sqrt(9:1000) for prec=%d\n", (int) GMP_NUMB_BITS); exit (1); } mpfr_set_prec (x, 2 * GMP_NUMB_BITS); mpfr_set_prec (y, GMP_NUMB_BITS); mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_nextabove (y); mpfr_set (x, y, MPFR_RNDN); inexact = test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1) || inexact >= 0) { printf ("Error in sqrt(1) for prec=%d\n", (int) GMP_NUMB_BITS); mpfr_dump (y); exit (1); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
static void check_small (void) { mpfr_t x; char *s; mpfr_exp_t e; mpfr_prec_t p; mpfr_init (x); mpfr_set_prec (x, 20); mpfr_set_ui (x, 2, MPFR_RNDN); mpfr_nexttozero (x); s = mpfr_get_str (NULL, &e, 4, 2, x, MPFR_RNDU); if (strcmp (s, "20") || (e != 1)) { printf ("Error in mpfr_get_str: 2- rounded up with 2 digits" " in base 4\n"); exit (1); } mpfr_free_str (s); /* check n_digits=0 */ mpfr_set_prec (x, 5); mpfr_set_ui (x, 17, MPFR_RNDN); s = mpfr_get_str (NULL, &e, 3, 0, x, MPFR_RNDN); mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 36, 0, x, MPFR_RNDN); mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 62, 0, x, MPFR_RNDN); mpfr_free_str (s); mpfr_set_prec (x, 64); mpfr_set_si (x, -1, MPFR_RNDN); mpfr_div_2exp (x, x, 63, MPFR_RNDN); /* x = -2^(-63) */ mpfr_add_ui (x, x, 1, MPFR_RNDN); /* x = 1 - 2^(-63) */ mpfr_mul_2exp (x, x, 32, MPFR_RNDN); /* x = 2^32 - 2^(-31) */ s = mpfr_get_str (NULL, &e, 3, 21, x, MPFR_RNDU); if (strcmp (s, "102002022201221111211") || (e != 21)) { printf ("Error in mpfr_get_str: 2^32-2^(-31) rounded up with" " 21 digits in base 3\n"); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 3, 20, x, MPFR_RNDU); if (strcmp (s, "10200202220122111122") || (e != 21)) { printf ("Error in mpfr_get_str: 2^32-2^(-31) rounded up with" " 20 digits in base 3\n"); exit (1); } mpfr_free_str (s); /* check corner case ret!=0, j0!=0 in mpfr_get_str_aux */ mpfr_set_prec (x, 100); mpfr_set_str_binary (x, "0.1001011111010001101110010101010101111001010111111101101101100110100011110110000101110110001011110000E-9"); s = mpfr_get_str (NULL, &e, 3, 2, x, MPFR_RNDU); if (strcmp (s, "22") || (e != -6)) { printf ("Error in mpfr_get_str: 100-bit number rounded up with" " 2 digits in base 3\n"); exit (1); } mpfr_free_str (s); /* check corner case exact=0 in mpfr_get_str_aux */ mpfr_set_prec (x, 100); mpfr_set_str_binary (x, "0.1001001111101101111000101000110111111010101100000110010001111111011001101011101100001100110000000000E8"); s = mpfr_get_str (NULL, &e, 10, 2, x, MPFR_RNDZ); if (strcmp (s, "14") || (e != 3)) { printf ("Error in mpfr_get_str: 100-bit number rounded to zero with" " 2 digits in base 10\n"); exit (1); } mpfr_free_str (s); for (p=4; p<=200; p++) { mpfr_set_prec (x, p); mpfr_set_str (x, "6.5", 10, MPFR_RNDN); s = mpfr_get_str (NULL, &e, 6, 2, x, MPFR_RNDN); if (strcmp (s, "10") || (e != 2)) { printf ("Error in mpfr_get_str: 6.5 rounded to nearest with" " 2 digits in base 6\n"); exit (1); } mpfr_free_str (s); mpfr_nexttoinf (x); s = mpfr_get_str (NULL, &e, 6, 2, x, MPFR_RNDN); if (strcmp (s, "11") || (e != 2)) { printf ("Error in mpfr_get_str: 6.5+ rounded to nearest with" " 2 digits in base 6\ngot %se%d instead of 11e2\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str (x, "6.5", 10, MPFR_RNDN); mpfr_nexttozero (x); s = mpfr_get_str (NULL, &e, 6, 2, x, MPFR_RNDN); if (strcmp (s, "10") || (e != 2)) { printf ("Error in mpfr_get_str: 6.5- rounded to nearest with" " 2 digits in base 6\n"); exit (1); } mpfr_free_str (s); } mpfr_set_prec (x, 3); mpfr_set_ui (x, 7, MPFR_RNDN); s = mpfr_get_str (NULL, &e, 2, 2, x, MPFR_RNDU); if (strcmp (s, "10") || (e != 4)) { printf ("Error in mpfr_get_str: 7 rounded up with 2 bits should" " give 0.10e3 instead of 0.%s*2^%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* problem found by Fabrice Rouillier */ mpfr_set_prec (x, 63); mpfr_set_str (x, "5e14", 10, MPFR_RNDN); s = mpfr_get_str (NULL, &e, 10, 18, x, MPFR_RNDU); mpfr_free_str (s); /* bug found by Johan Vervloet */ mpfr_set_prec (x, 6); mpfr_set_str (x, "688.0", 10, MPFR_RNDN); s = mpfr_get_str (NULL, &e, 2, 4, x, MPFR_RNDU); if (strcmp (s, "1011") || (e != 10)) { printf ("Error in mpfr_get_str: 688 printed up to 4 bits should" " give 1.011e9\ninstead of "); mpfr_out_str (stdout, 2, 4, x, MPFR_RNDU); puts (""); exit (1); } mpfr_free_str (s); mpfr_set_prec (x, 38); mpfr_set_str_binary (x, "1.0001110111110100011010100010010100110e-6"); s = mpfr_get_str (NULL, &e, 8, 10, x, MPFR_RNDU); if (strcmp (s, "1073721522") || (e != -1)) { printf ("Error in mpfr_get_str (3): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_prec (x, 53); mpfr_set_str_binary (x, "0.11010111011101100010000100010101110001000000010111001E454"); s = mpfr_get_str (NULL, &e, 19, 12, x, MPFR_RNDU); if (strcmp (s, "b1cgfa4gha0h") || (e != 107)) { printf ("Error in mpfr_get_str (4): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_prec (x, 145); mpfr_set_str_binary (x, "-0.1000110011000001011000010101101010110110101100101110100011111100011110011001001001010000100001000011000011000000010111011001000111101001110100110e6"); s = mpfr_get_str (NULL, &e, 4, 53, x, MPFR_RNDU); if (strcmp (s, "-20303001120111222312230232203330132121021100201003003") || (e != 3)) { printf ("Error in mpfr_get_str (5): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_prec (x, 45); mpfr_set_str_binary (x, "-0.00100111010110010001011001110111010001010010010"); s = mpfr_get_str (NULL, &e, 32, 9, x, MPFR_RNDN); if (strcmp (s, "-4tchctq54") || (e != 0)) { printf ("Error in mpfr_get_str (6): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* worst case found by Vincent Lefe`vre */ mpfr_set_prec (x, 53); mpfr_set_str_binary (x, "10011110111100000000001011011110101100010000011011111E164"); s = mpfr_get_str (NULL, &e, 10, 17, x, MPFR_RNDN); if (strcmp (s, "13076622631878654") || (e != 66)) { printf ("Error in mpfr_get_str (7): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10000001001001001100011101010011011011111000011000100E93"); s = mpfr_get_str (NULL, &e, 10, 2, x, MPFR_RNDU); if (strcmp (s, "46") || e != 44) { printf ("Error in mpfr_get_str (8): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10010001111100000111001111010101001010000010111010101E55"); s = mpfr_get_str (NULL, &e, 10, 2, x, MPFR_RNDN); if (strcmp (s, "19") || e != 33) { printf ("Error in mpfr_get_str (9): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "11011001010010111110010101101100111110111000010110110E44"); s = mpfr_get_str (NULL, &e, 10, 3, x, MPFR_RNDN); if (strcmp (s, "135") || e != 30) { printf ("Error in mpfr_get_str (10): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "11101111101000001011100001111000011111101111011001100E72"); s = mpfr_get_str (NULL, &e, 10, 4, x, MPFR_RNDN); if (strcmp (s, "3981") || e != 38) { printf ("Error in mpfr_get_str (11): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10011001001100100010111100001101110101001001111110000E46"); s = mpfr_get_str (NULL, &e, 10, 5, x, MPFR_RNDN); if (strcmp (s, "37930") || e != 30) { printf ("Error in mpfr_get_str (12): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10001100110111001011011110011011011101100011010001011E-72"); s = mpfr_get_str (NULL, &e, 10, 6, x, MPFR_RNDN); if (strcmp (s, "104950") || e != -5) { printf ("Error in mpfr_get_str (13): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10100100001011001000011001101101000110100110000010111E89"); s = mpfr_get_str (NULL, &e, 10, 7, x, MPFR_RNDN); if (strcmp (s, "3575392") || e != 43) { printf ("Error in mpfr_get_str (14): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "11000011011110110010100110001010000001010011001011001E-73"); s = mpfr_get_str (NULL, &e, 10, 8, x, MPFR_RNDN); if (strcmp (s, "72822386") || e != -6) { printf ("Error in mpfr_get_str (15): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10101010001101000111001100001000100011100010010001010E78"); s = mpfr_get_str (NULL, &e, 10, 9, x, MPFR_RNDN); if (strcmp (s, "180992873") || e != 40) { printf ("Error in mpfr_get_str (16): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10110111001000100000001101111001100101101110011011101E91"); s = mpfr_get_str (NULL, &e, 10, 10, x, MPFR_RNDN); if (strcmp (s, "1595312255") || e != 44) { printf ("Error in mpfr_get_str (17): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10011101010111101111000100111011101011110100110110101E93"); s = mpfr_get_str (NULL, &e, 10, 11, x, MPFR_RNDN); if (strcmp (s, "54835744350") || e != 44) { printf ("Error in mpfr_get_str (18): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10011101010111101111000100111011101011110100110110101E92"); s = mpfr_get_str (NULL, &e, 10, 12, x, MPFR_RNDN); if (strcmp (s, "274178721752") || e != 44) { printf ("Error in mpfr_get_str (19): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10011101010111101111000100111011101011110100110110101E91"); s = mpfr_get_str (NULL, &e, 10, 13, x, MPFR_RNDN); if (strcmp (s, "1370893608762") || e != 44) { printf ("Error in mpfr_get_str (20): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10010011010110011100010010100101100011101000011111111E92"); s = mpfr_get_str (NULL, &e, 10, 14, x, MPFR_RNDN); if (strcmp (s, "25672105101864") || e != 44) { printf ("Error in mpfr_get_str (21): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "100110111110110001000101110100100101101000011111001E87"); s = mpfr_get_str (NULL, &e, 10, 15, x, MPFR_RNDN); if (strcmp (s, "212231308858721") || e != 42) { printf ("Error in mpfr_get_str (22): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10111010110000111000101100101111001011011100101001111E-128"); s = mpfr_get_str (NULL, &e, 10, 15, x, MPFR_RNDN); if (strcmp (s, "193109287087290") || e != -22) { printf ("Error in mpfr_get_str (22b): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10001101101011010001111110000111010111010000110101010E80"); s = mpfr_get_str (NULL, &e, 10, 16, x, MPFR_RNDN); if (strcmp (s, "6026241735727920") || e != 40) { printf ("Error in mpfr_get_str (23): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "100010001011101001110101000110011001001000110001001E-81"); s = mpfr_get_str (NULL, &e, 10, 17, x, MPFR_RNDN); if (strcmp (s, "49741483709103481") || e != -9) { printf ("Error in mpfr_get_str (24): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "11000100001001001110111010011001111001001010110101111E-101"); s = mpfr_get_str (NULL, &e, 10, 7, x, MPFR_RNDN); if (strcmp (s, "2722049") || e != -14) { printf ("Error in mpfr_get_str (25): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "11111001010011100101000001111111110001001001110110001E-135"); s = mpfr_get_str (NULL, &e, 10, 8, x, MPFR_RNDN); if (strcmp (s, "20138772") || e != -24) { printf ("Error in mpfr_get_str (26): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "11111001010011100101000001111111110001001001110110001E-136"); s = mpfr_get_str (NULL, &e, 10, 9, x, MPFR_RNDN); if (strcmp (s, "100693858") || e != -24) { printf ("Error in mpfr_get_str (27): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10001000001110010110001011111011111011011010000110001E-110"); s = mpfr_get_str (NULL, &e, 10, 14, x, MPFR_RNDN); if (strcmp (s, "36923634350619") || e != -17) { printf ("Error in mpfr_get_str (28): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "11001100010111000111100010000110011101110001000101111E-87"); s = mpfr_get_str (NULL, &e, 10, 16, x, MPFR_RNDN); if (strcmp (s, "4646636036100804") || e != -10) { printf ("Error in mpfr_get_str (29): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "10011111001111110100001001010111111011010101111111000E-99"); s = mpfr_get_str (NULL, &e, 10, 17, x, MPFR_RNDN); if (strcmp (s, "88399901882446712") || e != -14) { printf ("Error in mpfr_get_str (30): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 8116315218207718*2^(-293) ~ 0.5100000000000000000015*10^(-72) */ mpfr_set_str_binary (x, "11100110101011011111011100101011101110110001111100110E-293"); s = mpfr_get_str (NULL, &e, 10, 2, x, MPFR_RNDU); if (strcmp (s, "52") || e != -72) { printf ("Error in mpfr_get_str (31u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 2, x, MPFR_RNDD); if (strcmp (s, "51") || e != -72) { printf ("Error in mpfr_get_str (31d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 6712731423444934*2^536 ~ .151000000000000000000067*10^178 */ mpfr_set_str_binary (x, "10111110110010011000110010011111101111000111111000110E536"); s = mpfr_get_str (NULL, &e, 10, 3, x, MPFR_RNDU); if (strcmp (s, "152") || e != 178) { printf ("Error in mpfr_get_str (32u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 3, x, MPFR_RNDD); if (strcmp (s, "151") || e != 178) { printf ("Error in mpfr_get_str (32d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 3356365711722467*2^540 ~ .120800000000000000000054*10^179 */ mpfr_set_str_binary (x, "1011111011001001100011001001111110111100011111100011E540"); s = mpfr_get_str (NULL, &e, 10, 4, x, MPFR_RNDU); if (strcmp (s, "1209") || e != 179) { printf ("Error in mpfr_get_str (33u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 4, x, MPFR_RNDD); if (strcmp (s, "1208") || e != 179) { printf ("Error in mpfr_get_str (33d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 6475049196144587*2^100 ~ .8208099999999999999999988*10^46 */ mpfr_set_str_binary (x, "10111000000010000010111011111001111010100011111001011E100"); s = mpfr_get_str (NULL, &e, 10, 5, x, MPFR_RNDU); if (strcmp (s, "82081") || e != 46) { printf ("Error in mpfr_get_str (34u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 5, x, MPFR_RNDD); if (strcmp (s, "82080") || e != 46) { printf ("Error in mpfr_get_str (34d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 6722280709661868*2^364 ~ .25260100000000000000000012*10^126 */ mpfr_set_str_binary (x, "10111111000011110000011110001110001111010010010101100E364"); s = mpfr_get_str (NULL, &e, 10, 6, x, MPFR_RNDU); if (strcmp (s, "252602") || e != 126) { printf ("Error in mpfr_get_str (35u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 6, x, MPFR_RNDD); if (strcmp (s, "252601") || e != 126) { printf ("Error in mpfr_get_str (35d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 5381065484265332*2^(-455) ~ .578389299999999999999999982*10^(-121) */ mpfr_set_str_binary (x, "10011000111100000110011110000101100111110011101110100E-455"); s = mpfr_get_str (NULL, &e, 10, 7, x, MPFR_RNDU); if (strcmp (s, "5783893") || e != -121) { printf ("Error in mpfr_get_str (36u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 7, x, MPFR_RNDD); if (strcmp (s, "5783892") || e != -121) { printf ("Error in mpfr_get_str (36d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 8369123604277281*2^(-852) ~ .27869147000000000000000000056*10^(-240) */ mpfr_set_str_binary (x, "11101101110111010110001101111100000111010100000100001E-852"); s = mpfr_get_str (NULL, &e, 10, 8, x, MPFR_RNDU); if (strcmp (s, "27869148") || e != -240) { printf ("Error in mpfr_get_str (37u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 8, x, MPFR_RNDD); if (strcmp (s, "27869147") || e != -240) { printf ("Error in mpfr_get_str (37d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 7976538478610756*2^377 ~ .245540326999999999999999999982*10^130 */ mpfr_set_str_binary (x, "11100010101101001111010010110100011100000100101000100E377"); s = mpfr_get_str (NULL, &e, 10, 9, x, MPFR_RNDU); if (strcmp (s, "245540327") || e != 130) { printf ("Error in mpfr_get_str (38u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 9, x, MPFR_RNDD); if (strcmp (s, "245540326") || e != 130) { printf ("Error in mpfr_get_str (38d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 8942832835564782*2^(-382) ~ .9078555839000000000000000000038*10^(-99) */ mpfr_set_str_binary (x, "11111110001010111010110000110011100110001010011101110E-382"); s = mpfr_get_str (NULL, &e, 10, 10, x, MPFR_RNDU); if (strcmp (s, "9078555840") || e != -99) { printf ("Error in mpfr_get_str (39u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 10, x, MPFR_RNDD); if (strcmp (s, "9078555839") || e != -99) { printf ("Error in mpfr_get_str (39d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 4471416417782391*2^(-380) ~ .18157111678000000000000000000077*10^(-98) */ mpfr_set_str_binary (x, "1111111000101011101011000011001110011000101001110111E-380"); s = mpfr_get_str (NULL, &e, 10, 11, x, MPFR_RNDU); if (strcmp (s, "18157111679") || e != -98) { printf ("Error in mpfr_get_str (40u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 11, x, MPFR_RNDD); if (strcmp (s, "18157111678") || e != -98) { printf ("Error in mpfr_get_str (40d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 7225450889282194*2^711 ~ .778380362292999999999999999999971*10^230 */ mpfr_set_str_binary (x, "11001101010111000001001100001100110010000001010010010E711"); s = mpfr_get_str (NULL, &e, 10, 12, x, MPFR_RNDU); if (strcmp (s, "778380362293") || e != 230) { printf ("Error in mpfr_get_str (41u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 12, x, MPFR_RNDD); if (strcmp (s, "778380362292") || e != 230) { printf ("Error in mpfr_get_str (41d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 3612725444641097*2^713 ~ .1556760724585999999999999999999942*10^231 */ mpfr_set_str_binary (x, "1100110101011100000100110000110011001000000101001001E713"); s = mpfr_get_str (NULL, &e, 10, 13, x, MPFR_RNDU); if (strcmp (s, "1556760724586") || e != 231) { printf ("Error in mpfr_get_str (42u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 13, x, MPFR_RNDD); if (strcmp (s, "1556760724585") || e != 231) { printf ("Error in mpfr_get_str (42d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 6965949469487146*2^(-248) ~ .15400733123779000000000000000000016*10^(-58) */ mpfr_set_str_binary (x, "11000101111110111111001111111101001101111000000101010E-248"); s = mpfr_get_str (NULL, &e, 10, 14, x, MPFR_RNDU); if (strcmp (s, "15400733123780") || e != -58) { printf ("Error in mpfr_get_str (43u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 14, x, MPFR_RNDD); if (strcmp (s, "15400733123779") || e != -58) { printf ("Error in mpfr_get_str (43d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 3482974734743573*2^(-244) ~ .12320586499023200000000000000000013*10^(-57) */ mpfr_set_str_binary (x, "1100010111111011111100111111110100110111100000010101E-244"); s = mpfr_get_str (NULL, &e, 10, 15, x, MPFR_RNDU); if (strcmp (s, "123205864990233") || e != -57) { printf ("Error in mpfr_get_str (44u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 15, x, MPFR_RNDD); if (strcmp (s, "123205864990232") || e != -57) { printf ("Error in mpfr_get_str (44d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 7542952370752766*2^(-919) ~ .170206189963739699999999999999999974*10^(-260) */ mpfr_set_str_binary (x, "11010110011000100011001110100100111011100110011111110E-919"); s = mpfr_get_str (NULL, &e, 10, 16, x, MPFR_RNDU); if (strcmp (s, "1702061899637397") || e != -260) { printf ("Error in mpfr_get_str (45u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 16, x, MPFR_RNDD); if (strcmp (s, "1702061899637396") || e != -260) { printf ("Error in mpfr_get_str (45d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); /* 5592117679628511*2^165 ~ .26153245263757307000000000000000000074*10^66 */ mpfr_set_str_binary (x, "10011110111100000000001011011110101100010000011011111E165"); s = mpfr_get_str (NULL, &e, 10, 17, x, MPFR_RNDU); if (strcmp (s, "26153245263757308") || e != 66) { printf ("Error in mpfr_get_str (46u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 17, x, MPFR_RNDD); if (strcmp (s, "26153245263757307") || e != 66) { printf ("Error in mpfr_get_str (46d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "11010010110111100001011010000110010000100001011011101E1223"); s = mpfr_get_str (NULL, &e, 10, 17, x, MPFR_RNDN); if (strcmp (s, "10716284017294180") || e != 385) { printf ("Error in mpfr_get_str (47n): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 18, x, MPFR_RNDU); if (strcmp (s, "107162840172941805") || e != 385) { printf ("Error in mpfr_get_str (47u): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 18, x, MPFR_RNDD); if (strcmp (s, "107162840172941804") || e != 385) { printf ("Error in mpfr_get_str (47d): s=%s e=%d\n", s, (int) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "11111101111011000001010100001101101000010010001111E122620"); s = mpfr_get_str (NULL, &e, 10, 17, x, MPFR_RNDN); if (strcmp (s, "22183435284042374") || e != 36928) { printf ("Error in mpfr_get_str (48n): s=%s e=%ld\n", s, (long) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 18, x, MPFR_RNDU); if (strcmp (s, "221834352840423736") || e != 36928) { printf ("Error in mpfr_get_str (48u): s=%s e=%ld\n", s, (long) e); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 18, x, MPFR_RNDD); if (strcmp (s, "221834352840423735") || e != 36928) { printf ("Error in mpfr_get_str (48d): s=%s e=%ld\n", s, (long) e); exit (1); } mpfr_free_str (s); mpfr_set_prec (x, 45); mpfr_set_str_binary (x, "1E45"); s = mpfr_get_str (NULL, &e, 32, 9, x, MPFR_RNDN); mpfr_free_str (s); mpfr_set_prec (x, 7); mpfr_set_str_binary (x, "0.1010101E10"); s = mpfr_get_str (NULL, &e, 10, 2, x, MPFR_RNDU); mpfr_free_str (s); /* checks rounding of negative numbers */ mpfr_set_prec (x, 7); mpfr_set_str (x, "-11.5", 10, MPFR_RNDN); s = mpfr_get_str (NULL, &e, 10, 2, x, MPFR_RNDD); if (strcmp (s, "-12")) { printf ("Error in mpfr_get_str for x=-11.5 and rnd=MPFR_RNDD\n" "got %s instead of -12\n", s); exit (1); } mpfr_free_str (s); s = mpfr_get_str (NULL, &e, 10, 2, x, MPFR_RNDU); if (strcmp (s, "-11")) { printf ("Error in mpfr_get_str for x=-11.5 and rnd=MPFR_RNDU\n"); exit (1); } mpfr_free_str (s); /* bug found by Jean-Pierre Merlet, produced error in mpfr_get_str */ mpfr_set_prec (x, 128); mpfr_set_str_binary (x, "0.10111001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011010E3"); s = mpfr_get_str (NULL, &e, 10, 0, x, MPFR_RNDU); mpfr_free_str (s); mpfr_set_prec (x, 381); mpfr_set_str_binary (x, "0.111111111111111111111111111111111111111111111111111111111111111111101110110000100110011101101101001010111000101111000100100011110101010110101110100000010100001000110100000100011111001000010010000010001010111001011110000001110010111101100001111000101101100000010110000101100100000101010110010110001010100111001111100011100101100000100100111001100010010011110011011010110000001000010"); s = mpfr_get_str (NULL, &e, 10, 0, x, MPFR_RNDD); if (e != 0) { printf ("Error in mpfr_get_str for x=0.999999..., exponent is %d" " instead of 0\n", (int) e); exit (1); } mpfr_free_str (s); mpfr_set_prec (x, 5); mpfr_set_str_binary (x, "1101.1"); /* 13.5, or (16)_7 + 1/2 */ s = mpfr_get_str (NULL, &e, 7, 2, x, MPFR_RNDN); /* we are in the tie case: both surrounding numbers are (16)_7 and (20)_7: since (16)_7 = 13 is odd and (20)_7 = 14 is even, we should have s = "20" and e = 2 */ if (e != 2 || strcmp (s, "20")) { printf ("Error in mpfr_get_str for x=13.5, base 7\n"); printf ("Expected s=20, e=2, got s=%s, e=%ld\n", s, (long) e); exit (1); } mpfr_free_str (s); /* try the same example, with input just below or above 13.5 */ mpfr_set_prec (x, 1000); mpfr_set_str_binary (x, "1101.1"); mpfr_nextabove (x); s = mpfr_get_str (NULL, &e, 7, 2, x, MPFR_RNDN); if (e != 2 || strcmp (s, "20")) { printf ("Error in mpfr_get_str for x=13.5+tiny, base 7\n"); printf ("Expected s=20, e=2, got s=%s, e=%ld\n", s, (long) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "1101.1"); mpfr_nextbelow (x); s = mpfr_get_str (NULL, &e, 7, 2, x, MPFR_RNDN); if (e != 2 || strcmp (s, "16")) { printf ("Error in mpfr_get_str for x=13.5-tiny, base 7\n"); printf ("Expected s=16, e=2, got s=%s, e=%ld\n", s, (long) e); exit (1); } mpfr_free_str (s); mpfr_set_prec (x, 7); mpfr_set_str_binary (x, "110000.1"); /* 48.5, or (66)_7 + 1/2 */ s = mpfr_get_str (NULL, &e, 7, 2, x, MPFR_RNDN); /* we are in the tie case: both surrounding numbers are (66)_7 and (100)_7: since (66)_7 = 48 is even and (100)_7 is odd, we should hase s = "66" and e = 2 */ if (e != 2 || strcmp (s, "66")) { printf ("Error in mpfr_get_str for x=48.5, base 7\n"); printf ("Expected s=66, e=2, got s=%s, e=%ld\n", s, (long) e); exit (1); } mpfr_free_str (s); /* try the same example, with input just below or above 48.5 */ mpfr_set_prec (x, 1000); mpfr_set_str_binary (x, "110000.1"); mpfr_nextabove (x); s = mpfr_get_str (NULL, &e, 7, 2, x, MPFR_RNDN); if (e != 3 || strcmp (s, "10")) { printf ("Error in mpfr_get_str for x=48.5+tiny, base 7\n"); printf ("Expected s=10, e=3, got s=%s, e=%ld\n", s, (long) e); exit (1); } mpfr_free_str (s); mpfr_set_str_binary (x, "110000.1"); mpfr_nextbelow (x); s = mpfr_get_str (NULL, &e, 7, 2, x, MPFR_RNDN); if (e != 2 || strcmp (s, "66")) { printf ("Error in mpfr_get_str for x=48.5-tiny, base 7\n"); printf ("Expected s=66, e=2, got s=%s, e=%ld\n", s, (long) e); exit (1); } mpfr_free_str (s); mpfr_clear (x); }
int main (void) { mpfr_t x, y; float f, g; int i; #if !defined(MPFR_ERRDIVZERO) float infp; #endif tests_start_mpfr (); #if !defined(MPFR_ERRDIVZERO) /* The definition of DBL_POS_INF involves a division by 0. This makes "clang -O2 -fsanitize=undefined -fno-sanitize-recover" fail. */ infp = (float) DBL_POS_INF; if (infp * 0.5 != infp) { fprintf (stderr, "Error, FLT_MAX + FLT_MAX does not yield INFP\n"); fprintf (stderr, "(this is probably a compiler bug, please report)\n"); exit (1); } #endif mpfr_init2 (x, 24); mpfr_init2 (y, 24); #if !defined(MPFR_ERRDIVZERO) mpfr_set_nan (x); f = mpfr_get_flt (x, MPFR_RNDN); if (f == f) { printf ("Error for mpfr_get_flt(NaN)\n"); exit (1); } mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_nan_p (x) == 0) { printf ("Error for mpfr_set_flt(NaN)\n"); exit (1); } mpfr_set_inf (x, 1); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_inf_p (x) == 0 || mpfr_sgn (x) < 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(+Inf)):\n"); printf ("f=%f, expected -Inf\n", f); printf ("got "); mpfr_dump (x); exit (1); } mpfr_set_inf (x, -1); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_inf_p (x) == 0 || mpfr_sgn (x) > 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(-Inf)):\n"); printf ("f=%f, expected -Inf\n", f); printf ("got "); mpfr_dump (x); exit (1); } #endif mpfr_set_ui (x, 0, MPFR_RNDN); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_zero_p (x) == 0 || MPFR_SIGN (x) < 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(+0))\n"); exit (1); } #ifdef HAVE_SIGNEDZ mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_zero_p (x) == 0 || MPFR_SIGN (x) > 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(-0))\n"); exit (1); } #endif /* HAVE_SIGNEDZ */ mpfr_set_ui (x, 17, MPFR_RNDN); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_cmp_ui (x, 17) != 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(17))\n"); printf ("expected 17\n"); printf ("got "); mpfr_dump (x); exit (1); } mpfr_set_si (x, -42, MPFR_RNDN); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_cmp_si (x, -42) != 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(-42))\n"); printf ("expected -42\n"); printf ("got "); mpfr_dump (x); exit (1); } mpfr_set_si_2exp (x, 1, -126, MPFR_RNDN); for (i = -126; i < 128; i++) { f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (y, f, MPFR_RNDN); if (mpfr_cmp (x, y) != 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(x))\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } mpfr_mul_2exp (x, x, 1, MPFR_RNDN); } mpfr_set_prec (x, 53); mpfr_set_si_2exp (x, 1, -126, MPFR_RNDN); for (i = -126; i < 128; i++) { mpfr_nextbelow (x); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_nextabove (x); mpfr_set_flt (y, f, MPFR_RNDN); if (mpfr_cmp (x, y) != 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(x))\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } mpfr_mul_2exp (x, x, 1, MPFR_RNDN); } mpfr_set_prec (x, 53); mpfr_set_si_2exp (x, 1, -126, MPFR_RNDN); for (i = -126; i < 128; i++) { mpfr_nextabove (x); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_nextbelow (x); mpfr_set_flt (y, f, MPFR_RNDN); if (mpfr_cmp (x, y) != 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(x))\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } mpfr_mul_2exp (x, x, 1, MPFR_RNDN); } mpfr_set_si_2exp (x, 1, -150, MPFR_RNDN); g = 0.0; f = mpfr_get_flt (x, MPFR_RNDN); if (f != g) { printf ("Error for mpfr_get_flt(2^(-150),RNDN)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDZ); if (f != g) { printf ("Error for mpfr_get_flt(2^(-150),RNDZ)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDD); if (f != g) { printf ("Error for mpfr_get_flt(2^(-150),RNDD)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } g = FLT_MIN * FLT_EPSILON; f = mpfr_get_flt (x, MPFR_RNDU); if (f != g) { printf ("Error for mpfr_get_flt(2^(-150),RNDU)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDA); if (f != g) { printf ("Error for mpfr_get_flt(2^(-150),RNDA)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } mpfr_set_si_2exp (x, 1, -151, MPFR_RNDN); g = 0.0; f = mpfr_get_flt (x, MPFR_RNDN); if (f != g) { printf ("Error for mpfr_get_flt(2^(-151),RNDN)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDZ); if (f != g) { printf ("Error for mpfr_get_flt(2^(-151),RNDZ)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDD); if (f != g) { printf ("Error for mpfr_get_flt(2^(-151),RNDD)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } g = FLT_MIN * FLT_EPSILON; f = mpfr_get_flt (x, MPFR_RNDU); if (f != g) { printf ("Error for mpfr_get_flt(2^(-151),RNDU)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDA); if (f != g) { printf ("Error for mpfr_get_flt(2^(-151),RNDA)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } mpfr_set_si_2exp (x, 1, -149, MPFR_RNDN); g = FLT_MIN * FLT_EPSILON; f = mpfr_get_flt (x, MPFR_RNDN); if (f != g) { printf ("Error for mpfr_get_flt(2^(-149),RNDN)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDZ); if (f != g) { printf ("Error for mpfr_get_flt(2^(-149),RNDZ)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDD); if (f != g) { printf ("Error for mpfr_get_flt(2^(-149),RNDD)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDU); if (f != g) { printf ("Error for mpfr_get_flt(2^(-149),RNDU)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDA); if (f != g) { printf ("Error for mpfr_get_flt(2^(-149),RNDA)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } mpfr_set_si_2exp (x, 1, 128, MPFR_RNDN); g = FLT_MAX; f = mpfr_get_flt (x, MPFR_RNDZ); if (f != g) { printf ("Error for mpfr_get_flt(2^128,RNDZ)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDD); if (f != g) { printf ("Error for mpfr_get_flt(2^128,RNDD)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } #if !defined(MPFR_ERRDIVZERO) f = mpfr_get_flt (x, MPFR_RNDN); /* 2^128 rounds to itself with extended exponent range, we should get +Inf */ g = infp; if (f != g) { printf ("Error for mpfr_get_flt(2^128,RNDN)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDU); if (f != g) { printf ("Error for mpfr_get_flt(2^128,RNDU)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDA); if (f != g) { printf ("Error for mpfr_get_flt(2^128,RNDA)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } #endif /* corner case: take x with 25 bits just below 2^128 */ mpfr_set_prec (x, 25); mpfr_set_si_2exp (x, 1, 128, MPFR_RNDN); mpfr_nextbelow (x); g = FLT_MAX; f = mpfr_get_flt (x, MPFR_RNDZ); if (f != g) { printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDZ)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDD); if (f != g) { printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDD)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } #if !defined(MPFR_ERRDIVZERO) f = mpfr_get_flt (x, MPFR_RNDN); /* first round to 2^128 (even rule), thus we should get +Inf */ g = infp; if (f != g) { printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDN)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDU); if (f != g) { printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDU)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDA); if (f != g) { printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDA)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } #endif mpfr_clear (x); mpfr_clear (y); tests_end_mpfr (); return 0; }
static void check_large (void) { __float128 f, e; int i; mpfr_t x, y; mpfr_rnd_t r; mpfr_init2 (x, 113); mpfr_init2 (y, 113); /* check with the largest float128 number 2^16384*(1-2^(-113)) */ for (f = 1.0, i = 0; i < 113; i++) f = f + f; f = f - (__float128) 1.0; mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_mul_2exp (y, y, 113, MPFR_RNDN); mpfr_sub_ui (y, y, 1, MPFR_RNDN); for (i = 113; i < 16384; i++) { for (r = 0; r < MPFR_RND_MAX; r++) { mpfr_set_float128 (x, f, r); if (! mpfr_equal_p (x, y)) { printf ("mpfr_set_float128 failed for 2^%d*(1-2^(-113)) rnd=%s\n", i, mpfr_print_rnd_mode (r)); printf ("got "); mpfr_dump (x); exit (1); } e = mpfr_get_float128 (x, r); if (e != f) { printf ("mpfr_get_float128 failed for 2^%d*(1-2^(-113)) rnd=%s\n", i, mpfr_print_rnd_mode (r)); exit (1); } } /* check with opposite number */ f = -f; mpfr_neg (y, y, MPFR_RNDN); for (r = 0; r < MPFR_RND_MAX; r++) { mpfr_set_float128 (x, f, r); if (! mpfr_equal_p (x, y)) { printf ("mpfr_set_float128 failed for -2^%d*(1-2^(-113)) rnd=%s\n", i, mpfr_print_rnd_mode (r)); printf ("got "); mpfr_dump (x); exit (1); } e = mpfr_get_float128 (x, r); if (e != f) { printf ("mpfr_get_float128 failed for -2^%d*(1-2^(-113)) rnd=%s\n", i, mpfr_print_rnd_mode (r)); exit (1); } } f = -f; mpfr_neg (y, y, MPFR_RNDN); f = f + f; mpfr_add (y, y, y, MPFR_RNDN); } 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 (); check_groups (); tests_end_mpfr (); return 0; }
static void special (void) { mpfr_t x, y; mpfr_exp_t emax; mpfr_init (x); mpfr_init (y); mpfr_set_nan (x); mpfr_rint (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (y)); mpfr_set_inf (x, 1); mpfr_rint (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && mpfr_sgn (y) > 0); mpfr_set_inf (x, -1); mpfr_rint (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && mpfr_sgn (y) < 0); mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_rint (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS(y)); mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); mpfr_rint (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_NEG(y)); /* coverage test */ mpfr_set_prec (x, 2); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_mul_2exp (x, x, mp_bits_per_limb, MPFR_RNDN); mpfr_rint (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp (y, x) == 0); /* another coverage test */ emax = mpfr_get_emax (); set_emax (1); mpfr_set_prec (x, 3); mpfr_set_str_binary (x, "1.11E0"); mpfr_set_prec (y, 2); mpfr_rint (y, x, MPFR_RNDU); /* x rounds to 1.0E1=0.1E2 which overflows */ MPFR_ASSERTN(mpfr_inf_p (y) && mpfr_sgn (y) > 0); set_emax (emax); /* yet another */ mpfr_set_prec (x, 97); mpfr_set_prec (y, 96); mpfr_set_str_binary (x, "-0.1011111001101111000111011100011100000110110110110000000111010001000101001111101010101011010111100E97"); mpfr_rint (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp (y, x) == 0); mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_str_binary (x, "0.10101100000000101001010101111111000000011111010000010E-1"); mpfr_rint (y, x, MPFR_RNDU); MPFR_ASSERTN(mpfr_cmp_ui (y, 1) == 0); mpfr_rint (y, x, MPFR_RNDD); MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS(y)); mpfr_set_prec (x, 36); mpfr_set_prec (y, 2); mpfr_set_str_binary (x, "-11000110101010111111110111001.0000100"); mpfr_rint (y, x, MPFR_RNDN); mpfr_set_str_binary (x, "-11E27"); MPFR_ASSERTN(mpfr_cmp (y, x) == 0); mpfr_set_prec (x, 39); mpfr_set_prec (y, 29); mpfr_set_str_binary (x, "-0.100010110100011010001111001001001100111E39"); mpfr_rint (y, x, MPFR_RNDN); mpfr_set_str_binary (x, "-0.10001011010001101000111100101E39"); MPFR_ASSERTN(mpfr_cmp (y, x) == 0); mpfr_set_prec (x, 46); mpfr_set_prec (y, 32); mpfr_set_str_binary (x, "-0.1011100110100101000001011111101011001001101001E32"); mpfr_rint (y, x, MPFR_RNDN); mpfr_set_str_binary (x, "-0.10111001101001010000010111111011E32"); MPFR_ASSERTN(mpfr_cmp (y, x) == 0); /* coverage test for mpfr_round */ mpfr_set_prec (x, 3); mpfr_set_str_binary (x, "1.01E1"); /* 2.5 */ mpfr_set_prec (y, 2); mpfr_round (y, x); /* since mpfr_round breaks ties away, should give 3 and not 2 as with the "round to even" rule */ MPFR_ASSERTN(mpfr_cmp_ui (y, 3) == 0); /* same test for the function */ (mpfr_round) (y, x); MPFR_ASSERTN(mpfr_cmp_ui (y, 3) == 0); mpfr_set_prec (x, 6); mpfr_set_prec (y, 3); mpfr_set_str_binary (x, "110.111"); mpfr_round (y, x); if (mpfr_cmp_ui (y, 7)) { printf ("Error in round(110.111)\n"); exit (1); } /* Bug found by Mark J Watkins */ mpfr_set_prec (x, 84); mpfr_set_str_binary (x, "0.110011010010001000000111101101001111111100101110010000000000000" \ "000000000000000000000E32"); mpfr_round (x, x); if (mpfr_cmp_str (x, "0.1100110100100010000001111011010100000000000000" \ "00000000000000000000000000000000000000E32", 2, MPFR_RNDN)) { printf ("Rounding error when dest=src\n"); exit (1); } mpfr_clear (x); mpfr_clear (y); }
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; }
/* Use the reflection formula Digamma(1-x) = Digamma(x) + Pi * cot(Pi*x), i.e., Digamma(x) = Digamma(1-x) - Pi * cot(Pi*x). Assume x < 1/2. */ static int mpfr_digamma_reflection (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode) { mpfr_prec_t p = MPFR_PREC(y) + 10, q; mpfr_t t, u, v; mpfr_exp_t e1, expv; int inex; MPFR_ZIV_DECL (loop); MPFR_LOG_FUNC (("x[%Pu]=%.*Rg rnd=%d", mpfr_get_prec(x), mpfr_log_prec, x, rnd_mode), ("y[%Pu]=%.*Rg inexact=%d", mpfr_get_prec(y), mpfr_log_prec, y, inex)); /* we want that 1-x is exact with precision q: if 0 < x < 1/2, then q = PREC(x)-EXP(x) is ok, otherwise if -1 <= x < 0, q = PREC(x)-EXP(x) is ok, otherwise for x < -1, PREC(x) is ok if EXP(x) <= PREC(x), otherwise we need EXP(x) */ if (MPFR_EXP(x) < 0) q = MPFR_PREC(x) + 1 - MPFR_EXP(x); else if (MPFR_EXP(x) <= MPFR_PREC(x)) q = MPFR_PREC(x) + 1; else q = MPFR_EXP(x); mpfr_init2 (u, q); MPFR_DBGRES(inex = mpfr_ui_sub (u, 1, x, MPFR_RNDN)); MPFR_ASSERTN(inex == 0); /* if x is half an integer, cot(Pi*x) = 0, thus Digamma(x) = Digamma(1-x) */ mpfr_mul_2exp (u, u, 1, MPFR_RNDN); inex = mpfr_integer_p (u); mpfr_div_2exp (u, u, 1, MPFR_RNDN); if (inex) { inex = mpfr_digamma (y, u, rnd_mode); goto end; } mpfr_init2 (t, p); mpfr_init2 (v, p); MPFR_ZIV_INIT (loop, p); for (;;) { mpfr_const_pi (v, MPFR_RNDN); /* v = Pi*(1+theta) for |theta|<=2^(-p) */ mpfr_mul (t, v, x, MPFR_RNDN); /* (1+theta)^2 */ e1 = MPFR_EXP(t) - (mpfr_exp_t) p + 1; /* bound for t: err(t) <= 2^e1 */ mpfr_cot (t, t, MPFR_RNDN); /* cot(t * (1+h)) = cot(t) - theta * (1 + cot(t)^2) with |theta|<=t*h */ if (MPFR_EXP(t) > 0) e1 = e1 + 2 * MPFR_EXP(t) + 1; else e1 = e1 + 1; /* now theta * (1 + cot(t)^2) <= 2^e1 */ e1 += (mpfr_exp_t) p - MPFR_EXP(t); /* error is now 2^e1 ulps */ mpfr_mul (t, t, v, MPFR_RNDN); e1 ++; mpfr_digamma (v, u, MPFR_RNDN); /* error <= 1/2 ulp */ expv = MPFR_EXP(v); mpfr_sub (v, v, t, MPFR_RNDN); if (MPFR_EXP(v) < MPFR_EXP(t)) e1 += MPFR_EXP(t) - MPFR_EXP(v); /* scale error for t wrt new v */ /* now take into account the 1/2 ulp error for v */ if (expv - MPFR_EXP(v) - 1 > e1) e1 = expv - MPFR_EXP(v) - 1; else e1 ++; e1 ++; /* rounding error for mpfr_sub */ if (MPFR_CAN_ROUND (v, p - e1, MPFR_PREC(y), rnd_mode)) break; MPFR_ZIV_NEXT (loop, p); mpfr_set_prec (t, p); mpfr_set_prec (v, p); } MPFR_ZIV_FREE (loop); inex = mpfr_set (y, v, rnd_mode); mpfr_clear (t); mpfr_clear (v); end: mpfr_clear (u); return inex; }
static void special_overflow (void) { mpfr_t x, y; mp_exp_t emin, emax; int inex; emin = mpfr_get_emin (); emax = mpfr_get_emax (); set_emin (-125); set_emax (128); mpfr_init2 (x, 24); mpfr_init2 (y, 24); mpfr_set_str_binary (x, "0.101100100000000000110100E7"); mpfr_gamma (y, x, GMP_RNDN); if (!mpfr_inf_p (y)) { printf ("Overflow error.\n"); mpfr_dump (y); exit (1); } /* problem mentioned by Kenneth Wilder, 18 Aug 2005 */ mpfr_set_prec (x, 29); mpfr_set_prec (y, 29); mpfr_set_str (x, "-200000000.5", 10, GMP_RNDN); /* exact */ mpfr_gamma (y, x, GMP_RNDN); if (!(mpfr_zero_p (y) && MPFR_SIGN (y) < 0)) { printf ("Error for gamma(-200000000.5)\n"); printf ("expected -0"); printf ("got "); mpfr_dump (y); exit (1); } mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_str (x, "-200000000.1", 10, GMP_RNDN); mpfr_gamma (y, x, GMP_RNDN); if (!(mpfr_zero_p (y) && MPFR_SIGN (y) < 0)) { printf ("Error for gamma(-200000000.1), prec=53\n"); printf ("expected -0"); printf ("got "); mpfr_dump (y); exit (1); } /* another problem mentioned by Kenneth Wilder, 29 Aug 2005 */ mpfr_set_prec (x, 333); mpfr_set_prec (y, 14); mpfr_set_str (x, "-2.0000000000000000000000005", 10, GMP_RNDN); mpfr_gamma (y, x, GMP_RNDN); mpfr_set_prec (x, 14); mpfr_set_str_binary (x, "-11010011110001E66"); if (mpfr_cmp (x, y)) { printf ("Error for gamma(-2.0000000000000000000000005)\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } /* another tests from Kenneth Wilder, 31 Aug 2005 */ set_emax (200); set_emin (-200); mpfr_set_prec (x, 38); mpfr_set_prec (y, 54); mpfr_set_str_binary (x, "0.11101111011100111101001001010110101001E-166"); mpfr_gamma (y, x, GMP_RNDN); mpfr_set_prec (x, 54); mpfr_set_str_binary (x, "0.100010001101100001110110001010111111010000100101011E167"); if (mpfr_cmp (x, y)) { printf ("Error for gamma (test 1)\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } set_emax (1000); set_emin (-2000); mpfr_set_prec (x, 38); mpfr_set_prec (y, 71); mpfr_set_str_binary (x, "10101011011100001111111000010111110010E-1034"); /* 184083777010*2^(-1034) */ mpfr_gamma (y, x, GMP_RNDN); mpfr_set_prec (x, 71); mpfr_set_str_binary (x, "10111111001000011110010001000000000000110011110000000011101011111111100E926"); /* 1762885132679550982140*2^926 */ if (mpfr_cmp (x, y)) { printf ("Error for gamma (test 2)\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } mpfr_set_prec (x, 38); mpfr_set_prec (y, 88); mpfr_set_str_binary (x, "10111100111001010000100001100100100101E-104"); /* 202824096037*2^(-104) */ mpfr_gamma (y, x, GMP_RNDN); mpfr_set_prec (x, 88); mpfr_set_str_binary (x, "1010110101111000111010111100010110101010100110111000001011000111000011101100001101110010E-21"); /* 209715199999500283894743922*2^(-21) */ if (mpfr_cmp (x, y)) { printf ("Error for gamma (test 3)\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } mpfr_set_prec (x, 171); mpfr_set_prec (y, 38); mpfr_set_str (x, "-2993155353253689176481146537402947624254601559176535", 10, GMP_RNDN); mpfr_div_2exp (x, x, 170, GMP_RNDN); mpfr_gamma (y, x, GMP_RNDN); mpfr_set_prec (x, 38); mpfr_set_str (x, "201948391737", 10, GMP_RNDN); mpfr_mul_2exp (x, x, 92, GMP_RNDN); if (mpfr_cmp (x, y)) { printf ("Error for gamma (test 5)\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } set_emin (-500000); mpfr_set_prec (x, 337); mpfr_set_prec (y, 38); mpfr_set_str (x, "-30000.000000000000000000000000000000000000000000001", 10, GMP_RNDN); mpfr_gamma (y, x, GMP_RNDN); mpfr_set_prec (x, 38); mpfr_set_str (x, "-3.623795987425E-121243", 10, GMP_RNDN); if (mpfr_cmp (x, y)) { printf ("Error for gamma (test 7)\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } /* was producing infinite loop */ set_emin (emin); mpfr_set_prec (x, 71); mpfr_set_prec (y, 71); mpfr_set_str (x, "-200000000.1", 10, GMP_RNDN); mpfr_gamma (y, x, GMP_RNDN); if (!(mpfr_zero_p (y) && MPFR_SIGN (y) < 0)) { printf ("Error for gamma (test 8)\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } set_emax (1073741823); mpfr_set_prec (x, 29); mpfr_set_prec (y, 29); mpfr_set_str (x, "423786866", 10, GMP_RNDN); mpfr_gamma (y, x, GMP_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) { printf ("Error for gamma(423786866)\n"); exit (1); } /* check exact result */ mpfr_set_prec (x, 2); mpfr_set_ui (x, 3, GMP_RNDN); inex = mpfr_gamma (x, x, GMP_RNDN); if (inex != 0 || mpfr_cmp_ui (x, 2) != 0) { printf ("Error for gamma(3)\n"); exit (1); } mpfr_set_emax (1024); mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_str_binary (x, "101010110100110011111010000110001000111100000110101E-43"); mpfr_gamma (x, x, GMP_RNDU); mpfr_set_str_binary (y, "110000011110001000111110110101011110000100001111111E971"); if (mpfr_cmp (x, y) != 0) { printf ("Error for gamma(4)\n"); printf ("expected "); mpfr_dump (y); printf ("got "); mpfr_dump (x); exit (1); } set_emin (emin); set_emax (emax); /* bug found by Kevin Rauch, 26 Oct 2007 */ mpfr_set_str (x, "1e19", 10, GMP_RNDN); inex = mpfr_gamma (x, x, GMP_RNDN); MPFR_ASSERTN(mpfr_inf_p (x) && inex > 0); mpfr_clear (y); mpfr_clear (x); }