int main (int argc, char *argv[]) { mpfr_t x, z; int y; int i; tests_start_mpfr (); mpfr_inits2 (53, x, z, (mpfr_ptr) 0); for(i = 0 ; i < numberof (tab) ; i++) { mpfr_set_str (x, tab[i].op1, 16, MPFR_RNDN); y = tab[i].op2; mpfr_add_si (z, x, y, MPFR_RNDZ); if (mpfr_cmp_str (z, tab[i].res_add, 16, MPFR_RNDN)) ERROR1("add_si", i, z, tab[i].res_add); mpfr_sub_si (z, x, y, MPFR_RNDZ); if (mpfr_cmp_str (z, tab[i].res_sub, 16, MPFR_RNDN)) ERROR1("sub_si", i, z, tab[i].res_sub); mpfr_si_sub (z, y, x, MPFR_RNDZ); mpfr_neg (z, z, MPFR_RNDZ); if (mpfr_cmp_str (z, tab[i].res_sub, 16, MPFR_RNDN)) ERROR1("si_sub", i, z, tab[i].res_sub); mpfr_mul_si (z, x, y, MPFR_RNDZ); if (mpfr_cmp_str (z, tab[i].res_mul, 16, MPFR_RNDN)) ERROR1("mul_si", i, z, tab[i].res_mul); mpfr_div_si (z, x, y, MPFR_RNDZ); if (mpfr_cmp_str (z, tab[i].res_div, 16, MPFR_RNDN)) ERROR1("div_si", i, z, tab[i].res_div); } mpfr_set_str1 (x, "1"); mpfr_si_div (z, 1024, x, MPFR_RNDN); if (mpfr_cmp_str1 (z, "1024")) ERROR1("si_div", i, z, "1024"); mpfr_si_div (z, -1024, x, MPFR_RNDN); if (mpfr_cmp_str1 (z, "-1024")) ERROR1("si_div", i, z, "-1024"); mpfr_clears (x, z, (mpfr_ptr) 0); check_invert (); test_generic_add_si (2, 200, 17); test_generic_sub_si (2, 200, 17); test_generic_mul_si (2, 200, 17); test_generic_div_si (2, 200, 17); tests_end_mpfr (); return 0; }
static void check2b (const char *xs, int px, const char *ys, int py, const char *rs, int pz, mpfr_rnd_t rnd_mode) { mpfr_t xx, yy, zz; mpfr_init2 (xx,px); mpfr_init2 (yy,py); mpfr_init2 (zz,pz); mpfr_set_str_binary (xx, xs); mpfr_set_str_binary (yy, ys); test_add (zz, xx, yy, rnd_mode); if (mpfr_cmp_str (zz, rs, 2, MPFR_RNDN)) { printf ("(2) x=%s,%d y=%s,%d pz=%d,rnd=%s\n", xs, px, ys, py, pz, mpfr_print_rnd_mode (rnd_mode)); printf ("got "); mpfr_print_binary(zz); puts (""); mpfr_set_str(zz, rs, 2, MPFR_RNDN); printf ("instead of "); mpfr_print_binary(zz); puts (""); exit (1); } mpfr_clear(xx); mpfr_clear(yy); mpfr_clear(zz); }
static void test2 (void) { mpfr_t x, y; int i, n = numberof(val); mpfr_inits2 (55, x, y, (mpfr_ptr) 0); for(i = 0 ; i < n ; i+=2) { mpfr_set_str1 (x, val[i]); mpfr_zeta(y, x, MPFR_RNDZ); if (mpfr_cmp_str (y, val[i+1] , 2, MPFR_RNDZ)) { printf("Wrong result for zeta(%s=", val[i]); mpfr_print_binary (x); printf (").\nGot : "); mpfr_print_binary(y); putchar('\n'); printf("Expected: "); mpfr_set_str (y, val[i+1], 2, MPFR_RNDZ); mpfr_print_binary(y); putchar('\n'); mpfr_set_prec(y, 65); mpfr_zeta(y, x, MPFR_RNDZ); printf("+ Prec : "); mpfr_print_binary(y); putchar('\n'); exit(1); } } mpfr_clears (x, y, (mpfr_ptr) 0); }
static void check1 (void) { mpfr_t x; int i, j; 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++) { mpfr_set_str (x, tab[i].in, 2, GMP_RNDN); j = mpfr_subnormalize (x, tab[i].i, tab[i].rnd); if (mpfr_cmp_str (x, tab[i].out, 2, GMP_RNDN) != 0) { printf ("Error for i=%d\nFor:%s\nExpected:%s\nGot:", i, tab[i].in, tab[i].out); mpfr_dump (x); exit (1); } } mpfr_clear (x); }
static void check_cache (void) { mpfr_t x; int i; mpfr_init2 (x, 195); mpfr_free_cache (); i = mpfr_const_log2 (x, MPFR_RNDN); if (i == 0) { printf("Error for log2. Invalid ternary value (1).\n"); exit (1); } mpfr_set_prec (x, 194); i = mpfr_const_log2 (x, MPFR_RNDN); if (i == 0) { printf("Error for log2. Invalid ternary value (2).\n"); exit (1); } mpfr_free_cache (); mpfr_set_prec (x, 9); mpfr_const_log2 (x, MPFR_RNDN); mpfr_set_prec (x, 8); mpfr_const_log2 (x, MPFR_RNDN); if (mpfr_cmp_str (x, "0.10110001E0", 2, MPFR_RNDN)) { printf("Error for log2. Wrong rounding.\n"); exit (1); } mpfr_clear (x); }
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.101100100000000000110100E0"); mpfr_asin (y, x, MPFR_RNDN); if (mpfr_cmp_str (y, "0.110001001101001111110000010110001000111011001000E0", 2, MPFR_RNDN)) { printf("Special Overflow error.\n"); mpfr_dump (y); exit (1); } mpfr_clear (y); mpfr_clear (x); set_emin (emin); set_emax (emax); }
static void test1 (void) { mpfr_t x, y, z, a; int res1, res2; mpfr_init2 (x, 32); mpfr_init2 (y, 65); mpfr_init2 (z, 17); mpfr_init2 (a, 17); mpfr_set_str_binary (x, "-0.101110001001011011011e-9"); mpfr_ui_pow (y, 7, x, MPFR_RNDN); mpfr_set_prec (x, 40); mpfr_set_str_binary (x, "-0.1100101100101111011001010010110011110110E-1"); mpfr_set_prec (y, 74); mpfr_ui_pow (y, 8, x, MPFR_RNDN); mpfr_set_prec (x, 74); mpfr_set_str_binary (x, "0.11100000010100111101000011111011011010011000011000101011010011010101000011E-1"); if (mpfr_cmp (x, y)) { printf ("Error for input of 40 bits, output of 74 bits\n"); exit (1); } /* Check for ui_pow_ui */ mpfr_ui_pow_ui (x, 0, 1, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS (x)); mpfr_ui_pow_ui (x, 0, 4, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS (x)); res1 = mpfr_ui_pow_ui (z, 17, 42, MPFR_RNDD); mpfr_set_ui (x, 17, MPFR_RNDN); mpfr_set_ui (y, 42, MPFR_RNDN); res2 = mpfr_pow (a, x, y, MPFR_RNDD); if (mpfr_cmp (z, a) || res1 != res2) { printf ("Error for ui_pow_ui for 17^42\n" "Inexact1 = %d Inexact2 = %d\n", res1, res2); mpfr_dump (z); mpfr_dump (a); exit (1); } mpfr_set_prec (x, 2); mpfr_ui_pow_ui (x, 65537, 65535, MPFR_RNDN); if (mpfr_cmp_str (x, "0.11E1048562", 2, MPFR_RNDN) != 0) { printf ("Error for ui_pow_ui for 65537 ^65535 with 2 bits of precision\n"); mpfr_dump (x); exit (1); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (a); }
static void special_overflow (void) { /* Check for overflow in 3 cases: 1. cosh(x) is representable, but not exp(x) 2. cosh(x) is not representable in the selected range of exp. 3. cosh(x) exp overflow even with the largest range of exp */ mpfr_t x, y; mp_exp_t emin, emax; emin = mpfr_get_emin (); emax = mpfr_get_emax (); set_emin (-125); set_emax (128); mpfr_init2 (x, 24); mpfr_init2 (y, 24); mpfr_set_str_binary (x, "0.101100100000000000110100E7"); mpfr_cosh (y, x, GMP_RNDN); if (mpfr_cmp_str (y, "0.101010001111001010001110E128", 2, GMP_RNDN)) { printf("Special overflow error 1.\n"); mpfr_dump (y); exit (1); } mpfr_set_str_binary (x, "0.101100100000000000110100E8"); mpfr_cosh (y, x, GMP_RNDN); if (!mpfr_inf_p(y)) { printf("Special overflow error 2.\n"); mpfr_dump (y); exit (1); } set_emin (emin); set_emax (emax); mpfr_set_str_binary (x, "0.101100100000000000110100E1000000"); mpfr_cosh (y, x, GMP_RNDN); if (!mpfr_inf_p(y)) { printf("Special overflow error 3.\n"); mpfr_dump (y); exit (1); } mpfr_clear (y); mpfr_clear (x); }
static void check4 (const char *as, mpfr_rnd_t rnd_mode, const char *Qs) { mpfr_t q; mpfr_init2 (q, 53); mpfr_set_str1 (q, as); test_sqrt (q, q, rnd_mode); if (mpfr_cmp_str (q, Qs, 16, MPFR_RNDN)) { printf ("mpfr_sqrt failed for a=%s, rnd_mode=%s\n", as, mpfr_print_rnd_mode(rnd_mode)); printf ("expected "); mpfr_out_str (stdout, 16, 0, q, MPFR_RNDN); printf ("\ngot %s\n", Qs); mpfr_clear (q); exit (1); } mpfr_clear (q); }
static void check53b (const char *xs, const char *sin_xs, mpfr_rnd_t rnd_mode) { mpfr_t xx, s; mpfr_init2 (xx, 53); mpfr_init2 (s, 53); mpfr_set_str (xx, xs, 2, MPFR_RNDN); /* should be exact */ test_sin (s, xx, rnd_mode); if (mpfr_cmp_str (s, sin_xs, 2, MPFR_RNDN)) { printf ("mpfr_sin failed in rounding mode %s for\n x = %s\n", mpfr_print_rnd_mode (rnd_mode), xs); printf (" got "); mpfr_out_str (stdout, 2, 0, s, MPFR_RNDN); printf ("\nexpected %s\n", sin_xs); exit (1); } mpfr_clear (xx); mpfr_clear (s); }
static void x_near_one (void) { mpfr_t x, y; int inex; mpfr_init2 (x, 32); mpfr_init2 (y, 16); mpfr_set_ui (x, 1, GMP_RNDN); mpfr_nextbelow (x); inex = mpfr_log (y, x, GMP_RNDD); if (mpfr_cmp_str (y, "-0.1000000000000001E-31", 2, GMP_RNDN) || inex >= 0) { printf ("Failure in x_near_one, got inex = %d and\ny = ", inex); mpfr_dump (y); } mpfr_clears (x, y, (mpfr_ptr) 0); }
static void check4 (const char *Ns, const char *Ds, mpfr_rnd_t rnd_mode, int p, const char *Qs) { mpfr_t q, n, d; mpfr_inits2 (p, q, n, d, (mpfr_ptr) 0); mpfr_set_str1 (n, Ns); mpfr_set_str1 (d, Ds); test_div(q, n, d, rnd_mode); if (mpfr_cmp_str (q, Qs, ((p==53) ? 10 : 2), MPFR_RNDN) ) { printf ("mpfr_div failed for n=%s, d=%s, p=%d, rnd_mode=%s\n", Ns, Ds, p, mpfr_print_rnd_mode (rnd_mode)); printf ("got ");mpfr_print_binary(q); mpfr_set_str (q, Qs, ((p==53) ? 10 : 2), MPFR_RNDN); printf("\nexpected "); mpfr_print_binary(q); putchar('\n'); exit (1); } mpfr_clears (q, n, d, (mpfr_ptr) 0); }
static void check_regression (void) { mpfr_t x, y; mpfr_prec_t p; int i; p = strlen (xs) - 2 - 3; mpfr_inits2 (p, x, y, (mpfr_ptr) 0); mpfr_set_str (x, xs, 2, MPFR_RNDN); i = mpfr_sin (y, x, MPFR_RNDN); if (i >= 0 || mpfr_cmp_str (y, "0.111001110011110011110001010110011101110E-1", 2, MPFR_RNDN)) { printf ("Regression test failed (1) i=%d\ny=", i); mpfr_dump (y); exit (1); } mpfr_clears (x, y, (mpfr_ptr) 0); }
static void x_near_one (void) { mpfr_t x, y, z; int inex; mpfr_init2 (x, 32); mpfr_init2 (y, 4); mpfr_init2 (z, 33); mpfr_set_ui (x, 1, GMP_RNDN); mpfr_nextbelow (x); mpfr_set_ui_2exp (y, 11, -2, GMP_RNDN); inex = mpfr_pow (z, x, y, GMP_RNDN); if (mpfr_cmp_str (z, "0.111111111111111111111111111111011E0", 2, GMP_RNDN) || inex <= 0) { printf ("Failure in x_near_one, got inex = %d and\nz = ", inex); mpfr_dump (z); } mpfr_clears (x, y, z, (void *) 0); }
/* check sign of inexact flag */ static void check_inexact (void) { mpfr_t x, y; int inexact; mpfr_init2 (x, 53); mpfr_init2 (y, 53); mpfr_set_str_binary (x, "1.0000000000001001000110100100101000001101101011100101e2"); inexact = mpfr_exp (y, x, GMP_RNDN); if (inexact <= 0) { printf ("Wrong inexact flag\n"); exit (1); } /* Bug due to wrong approximation of (x)/log2 */ mpfr_set_prec (x, 163); mpfr_set_str (x, "-4.28ac8fceeadcda06bb56359017b1c81b85b392e7", 16, GMP_RNDN); mpfr_exp (x, x, GMP_RNDN); if (mpfr_cmp_str (x, "3.fffffffffffffffffffffffffffffffffffffffe8@-2", 16, GMP_RNDN)) { printf ("Error for x= -4.28ac8fceeadcda06bb56359017b1c81b85b392e7"); printf ("expected 3.fffffffffffffffffffffffffffffffffffffffe8@-2"); printf ("Got "); mpfr_out_str (stdout, 16, 0, x, GMP_RNDN); putchar ('\n'); } mpfr_clear (x); mpfr_clear (y); }
static void check_64(void) { mpfr_t x,y,z; mpfr_inits2 (64, x, y, z, (mpfr_ptr) 0); mpfr_set_str_binary(x, "1.00100100110110101001010010101111000001011100100101010000000000E54"); mpfr_set_str_binary(y, "1.00000000000000000000000000000000000000000000000000000000000000E584"); test_div(z, x, y, MPFR_RNDU); if (mpfr_cmp_str (z, "0.1001001001101101010010100101011110000010111001001010100000000000E-529", 2, MPFR_RNDN)) { printf("Error for tdiv for MPFR_RNDU and p=64\nx="); mpfr_print_binary(x); printf("\ny="); mpfr_print_binary(y); printf("\ngot "); mpfr_print_binary(z); printf("\nexpected 0.1001001001101101010010100101011110000010111001001010100000000000E-529\n"); exit(1); } mpfr_clears (x, y, z, (mpfr_ptr) 0); }
static void check_parse (void) { mpfr_t x; char *s; int res; mpfr_init (x); /* Invalid data */ mpfr_set_si (x, -1, MPFR_RNDN); res = mpfr_strtofr (x, " invalid", NULL, 10, MPFR_RNDN); if (MPFR_NOTZERO (x) || MPFR_IS_NEG (x)) { printf ("Failed parsing ' invalid' (1)\n X="); mpfr_dump (x); exit (1); } MPFR_ASSERTN (res == 0); mpfr_set_si (x, -1, MPFR_RNDN); res = mpfr_strtofr (x, " invalid", &s, 0, MPFR_RNDN); if (MPFR_NOTZERO (x) || MPFR_IS_NEG (x) || strcmp (s, " invalid")) { printf ("Failed parsing ' invalid' (2)\n S=%s\n X=", s); mpfr_dump (x); exit (1); } MPFR_ASSERTN (res == 0); /* Check if it stops correctly */ mpfr_strtofr (x, "15*x", &s, 10, MPFR_RNDN); if (mpfr_cmp_ui (x, 15) || strcmp (s, "*x")) { printf ("Failed parsing '15*x'\n S=%s\n X=", s); mpfr_dump (x); exit (1); } /* Check for leading spaces */ mpfr_strtofr (x, " 1.5E-10 *x^2", &s, 10, MPFR_RNDN); if (mpfr_cmp_str1 (x, "1.5E-10") || strcmp (s, " *x^2")) { printf ("Failed parsing '1.5E-10*x^2'\n S=%s\n X=", s); mpfr_dump (x); exit (1); } /* Check for leading sign */ mpfr_strtofr (x, " +17.5E-42E ", &s, 10, MPFR_RNDN); if (mpfr_cmp_str1 (x, "17.5E-42") || strcmp (s, "E ")) { printf ("Failed parsing '+17.5E-42E '\n S=%s\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-17.5E+42E\n", &s, 10, MPFR_RNDN); if (mpfr_cmp_str1 (x, "-17.5E42") || strcmp (s, "E\n")) { printf ("Failed parsing '-17.5E+42\\n'\n S=%s\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* P form */ mpfr_strtofr (x, "0x42P17", &s, 16, MPFR_RNDN); if (mpfr_cmp_str (x, "8650752", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '0x42P17' (base = 16)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-0X42p17", &s, 16, MPFR_RNDN); if (mpfr_cmp_str (x, "-8650752", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '-0x42p17' (base = 16)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "42p17", &s, 16, MPFR_RNDN); if (mpfr_cmp_str (x, "8650752", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '42p17' (base = 16)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-42P17", &s, 16, MPFR_RNDN); if (mpfr_cmp_str (x, "-8650752", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '-42P17' (base = 16)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "0b1001P17", &s, 2, MPFR_RNDN); if (mpfr_cmp_str (x, "1179648", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '0b1001P17' (base = 2)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-0B1001p17", &s, 2, MPFR_RNDN); if (mpfr_cmp_str (x, "-1179648", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '-0B1001p17' (base = 2)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "1001p17", &s, 2, MPFR_RNDN); if (mpfr_cmp_str (x, "1179648", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '1001p17' (base = 2)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-1001P17", &s, 2, MPFR_RNDN); if (mpfr_cmp_str (x, "-1179648", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '-1001P17' (base = 2)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Check for auto-detection of the base */ mpfr_strtofr (x, "+0x42P17", &s, 0, MPFR_RNDN); if (mpfr_cmp_str (x, "42P17", 16, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '+0x42P17'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-42E17", &s, 0, MPFR_RNDN); if (mpfr_cmp_str (x, "-42E17", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '-42E17'\n S=%s\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-42P17", &s, 0, MPFR_RNDN); if (mpfr_cmp_str (x, "-42", 10, MPFR_RNDN) || strcmp (s, "P17")) { printf ("Failed parsing '-42P17' (base = 0)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, " 0b0101.011@42", &s, 0, MPFR_RNDN); if (mpfr_cmp_str (x, "0101.011@42", 2, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '0101.011@42'\n S=%s\n X=", s); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, " 0b0101.011P42", &s, 0, MPFR_RNDN); if (mpfr_cmp_str (x, "0101.011@42", 2, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '0101.011@42'\n S=%s\n X=", s); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "+0x42@17", &s, 0, MPFR_RNDN); if (mpfr_cmp_str (x, "4.2@18", 16, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '+0x42P17'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Check for space inside the mantissa */ mpfr_strtofr (x, "+0x4 2@17", &s, 0, MPFR_RNDN); if (mpfr_cmp_ui (x, 4) || strcmp(s," 2@17")) { printf ("Failed parsing '+0x4 2@17'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "+0x42 P17", &s, 0, MPFR_RNDN); if (mpfr_cmp_ui (x, 0x42) || strcmp(s," P17")) { printf ("Failed parsing '+0x42 P17'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Space between mantissa and exponent */ mpfr_strtofr (x, " -0b0101P 17", &s, 0, MPFR_RNDN); if (mpfr_cmp_si (x, -5) || strcmp(s,"P 17")) { printf ("Failed parsing '-0b0101P 17'\n S=%s\n X=", s); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Check for Invalid exponent. */ mpfr_strtofr (x, " -0b0101PF17", &s, 0, MPFR_RNDN); if (mpfr_cmp_si (x, -5) || strcmp(s,"PF17")) { printf ("Failed parsing '-0b0101PF17'\n S=%s\n X=", s); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* At least one digit in the mantissa. */ mpfr_strtofr (x, " .E10", &s, 0, MPFR_RNDN); if (strcmp(s," .E10")) { printf ("Failed parsing ' .E10'\n S=%s\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Check 2 '.': 2.3.4 */ mpfr_strtofr (x, "-1.2.3E4", &s, 0, MPFR_RNDN); if (mpfr_cmp_str1 (x, "-1.2") || strcmp(s,".3E4")) { printf ("Failed parsing '-1.2.3E4'\n S=%s\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Check for 0x and 0b */ mpfr_strtofr (x, " 0xG", &s, 0, MPFR_RNDN); if (mpfr_cmp_ui (x, 0) || strcmp(s,"xG")) { printf ("Failed parsing ' 0xG'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, " 0b2", &s, 0, MPFR_RNDN); if (mpfr_cmp_ui (x, 0) || strcmp(s,"b2")) { printf ("Failed parsing ' 0b2'\n S=%s\n X=", s); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-0x.23@2Z33", &s, 0, MPFR_RNDN); if (mpfr_cmp_si (x, -0x23) || strcmp(s,"Z33")) { printf ("Failed parsing '-0x.23@2Z33'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, " 0x", &s, 0, MPFR_RNDN); if (mpfr_cmp_ui (x, 0) || strcmp(s,"x")) { printf ("Failed parsing ' 0x'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_clear (x); }
int main (int argc, char *argv[]) { long nbtests; mpfr_prec_t prec; int verbose = 0; tests_start_mpfr (); if (argc > 1) verbose = 1; nbtests = 10000; if (argc > 1) { long a = atol(argv[1]); if (a != 0) nbtests = a; } if (argc <= 2) prec = 1000; else prec = atol(argv[2]); test_urandomb (nbtests, prec, verbose); if (argc == 1) /* check also small precision */ { test_urandomb (nbtests, 2, 0); } #ifndef MPFR_USE_MINI_GMP /* Since these tests assume a deterministic random generator, and this is not implemented in mini-gmp, we omit them with mini-gmp. */ bug20100914 (); #if __MPFR_GMP(4,2,0) /* Get a non-zero fixed-point number whose first 32 bits are 0 with the default GMP PRNG. This corresponds to the case cnt == 0 && k != 0 in src/urandomb.c (fixed in r8762) with the 32-bit ABI. */ { gmp_randstate_t s; mpfr_t x; char *str = "0.1010111100000000000000000000000000000000E-32"; int k; gmp_randinit_default (s); gmp_randseed_ui (s, 4518); mpfr_init2 (x, 40); for (k = 0; k < 575123; k++) { mpfr_urandomb (x, s); MPFR_ASSERTN (MPFR_IS_FP (x)); } if (mpfr_cmp_str (x, str, 2, MPFR_RNDN) != 0) { printf ("Error in test_urandomb:\n"); printf ("Expected %s\n", str); printf ("Got "); mpfr_dump (x); exit (1); } mpfr_clear (x); gmp_randclear (s); } #endif #endif tests_end_mpfr (); return 0; }
static void test_2exp (void) { mpfr_t x; int res; mpfr_init2 (x, 32); mpfr_set_ui_2exp (x, 1, 0, MPFR_RNDN); if (mpfr_cmp_ui(x, 1)) ERROR("(1U,0)"); mpfr_set_ui_2exp (x, 1024, -10, MPFR_RNDN); if (mpfr_cmp_ui(x, 1)) ERROR("(1024U,-10)"); mpfr_set_ui_2exp (x, 1024, 10, MPFR_RNDN); if (mpfr_cmp_ui(x, 1024*1024)) ERROR("(1024U,+10)"); mpfr_set_si_2exp (x, -1024L * 1024L, -10, MPFR_RNDN); if (mpfr_cmp_si(x, -1024)) ERROR("(1M,-10)"); mpfr_set_ui_2exp (x, 0x92345678, 16, MPFR_RNDN); if (mpfr_cmp_str (x, "92345678@4", 16, MPFR_RNDN)) ERROR("(x92345678U,+16)"); mpfr_set_si_2exp (x, -0x1ABCDEF0, -256, MPFR_RNDN); if (mpfr_cmp_str (x, "-1ABCDEF0@-64", 16, MPFR_RNDN)) ERROR("(-x1ABCDEF0,-256)"); mpfr_set_prec (x, 2); res = mpfr_set_si_2exp (x, 7, 10, MPFR_RNDU); if (mpfr_cmp_ui (x, 1<<13) || res <= 0) ERROR ("Prec 2 + si_2exp"); res = mpfr_set_ui_2exp (x, 7, 10, MPFR_RNDU); if (mpfr_cmp_ui (x, 1<<13) || res <= 0) ERROR ("Prec 2 + ui_2exp"); mpfr_clear_flags (); mpfr_set_ui_2exp (x, 17, MPFR_EMAX_MAX, MPFR_RNDN); if (!mpfr_inf_p (x) || MPFR_IS_NEG (x)) ERROR ("mpfr_set_ui_2exp and overflow (bad result)"); if (!mpfr_overflow_p ()) ERROR ("mpfr_set_ui_2exp and overflow (overflow flag not set)"); mpfr_clear_flags (); mpfr_set_si_2exp (x, 17, MPFR_EMAX_MAX, MPFR_RNDN); if (!mpfr_inf_p (x) || MPFR_IS_NEG (x)) ERROR ("mpfr_set_si_2exp (pos) and overflow (bad result)"); if (!mpfr_overflow_p ()) ERROR ("mpfr_set_si_2exp (pos) and overflow (overflow flag not set)"); mpfr_clear_flags (); mpfr_set_si_2exp (x, -17, MPFR_EMAX_MAX, MPFR_RNDN); if (!mpfr_inf_p (x) || MPFR_IS_POS (x)) ERROR ("mpfr_set_si_2exp (neg) and overflow (bad result)"); if (!mpfr_overflow_p ()) ERROR ("mpfr_set_si_2exp (neg) and overflow (overflow flag not set)"); mpfr_clear (x); }
static void special_atan2 (void) { mpfr_t x, y, z; mpfr_inits2 (4, x, y, z, (mpfr_ptr) 0); /* Anything with NAN should be set to NAN */ mpfr_set_ui (y, 0, MPFR_RNDN); mpfr_set_nan (x); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (MPFR_IS_NAN (z)); mpfr_swap (x, y); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (MPFR_IS_NAN (z)); /* 0+ 0+ --> 0+ */ mpfr_set_ui (y, 0, MPFR_RNDN); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_POS (z)); /* 0- 0+ --> 0- */ MPFR_CHANGE_SIGN (y); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_NEG (z)); /* 0- 0- --> -PI */ MPFR_CHANGE_SIGN (x); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "-3.1415", 10, MPFR_RNDN) == 0); /* 0+ 0- --> +PI */ MPFR_CHANGE_SIGN (y); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "3.1415", 10, MPFR_RNDN) == 0); /* 0+ -1 --> PI */ mpfr_set_si (x, -1, MPFR_RNDN); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "3.1415", 10, MPFR_RNDN) == 0); /* 0- -1 --> -PI */ MPFR_CHANGE_SIGN (y); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "-3.1415", 10, MPFR_RNDN) == 0); /* 0- +1 --> 0- */ mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_NEG (z)); /* 0+ +1 --> 0+ */ MPFR_CHANGE_SIGN (y); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_POS (z)); /* +1 0+ --> PI/2 */ mpfr_swap (x, y); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "1.57075", 10, MPFR_RNDN) == 0); /* +1 0- --> PI/2 */ MPFR_CHANGE_SIGN (x); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "1.57075", 10, MPFR_RNDN) == 0); /* -1 0- --> -PI/2 */ MPFR_CHANGE_SIGN (y); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "-1.57075", 10, MPFR_RNDN) == 0); /* -1 0+ --> -PI/2 */ MPFR_CHANGE_SIGN (x); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "-1.57075", 10, MPFR_RNDN) == 0); /* -1 +INF --> -0 */ MPFR_SET_INF (x); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_NEG (z)); /* +1 +INF --> +0 */ MPFR_CHANGE_SIGN (y); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (MPFR_IS_ZERO (z) && MPFR_IS_POS (z)); /* +1 -INF --> +PI */ MPFR_CHANGE_SIGN (x); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "3.1415", 10, MPFR_RNDN) == 0); /* -1 -INF --> -PI */ MPFR_CHANGE_SIGN (y); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "-3.1415", 10, MPFR_RNDN) == 0); /* -INF -1 --> -PI/2 */ mpfr_swap (x, y); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "-1.57075", 10, MPFR_RNDN) == 0); /* +INF -1 --> PI/2 */ MPFR_CHANGE_SIGN (y); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "1.57075", 10, MPFR_RNDN) == 0); /* +INF -INF --> 3*PI/4 */ MPFR_SET_INF (x); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "2.356194490192344928", 10, MPFR_RNDN) == 0); /* +INF +INF --> PI/4 */ MPFR_CHANGE_SIGN (x); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "0.785375", 10, MPFR_RNDN) == 0); /* -INF +INF --> -PI/4 */ MPFR_CHANGE_SIGN (y); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "-0.785375", 10, MPFR_RNDN) == 0); /* -INF -INF --> -3*PI/4 */ MPFR_CHANGE_SIGN (x); mpfr_atan2 (z, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_str (z, "-2.356194490192344928", 10, MPFR_RNDN) == 0); mpfr_set_prec (z, 905); /* exercises Ziv's loop */ mpfr_atan2 (z, y, x, MPFR_RNDZ); MPFR_ASSERTN (mpfr_cmp_str (z, "-2.35619449019234492884698253745962716314787704953132936573120844423086230471465674897102611900658780098661106488496172998532038345716293667379401955609636083808771307702645389082916973346721171619778647332160823174945008459635673617534008737395340143185923642519259526145784", 10, MPFR_RNDN) == 0); mpfr_clears (x, y, z, (mpfr_ptr) 0); }
static void check_special (void) { mpfr_t x, y, z; mpfr_exp_t emin, emax; emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_init (x); mpfr_init (y); mpfr_init (z); /* check exp(NaN) = NaN */ mpfr_set_nan (x); test_exp (y, x, MPFR_RNDN); if (!mpfr_nan_p (y)) { printf ("Error for exp(NaN)\n"); exit (1); } /* check exp(+inf) = +inf */ mpfr_set_inf (x, 1); test_exp (y, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) { printf ("Error for exp(+inf)\n"); exit (1); } /* check exp(-inf) = +0 */ mpfr_set_inf (x, -1); test_exp (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) { printf ("Error for exp(-inf)\n"); exit (1); } /* Check overflow. Corner case of mpfr_exp_2 */ mpfr_set_prec (x, 64); mpfr_set_emax (MPFR_EMAX_DEFAULT); mpfr_set_emin (MPFR_EMIN_DEFAULT); mpfr_set_str (x, "0.1011000101110010000101111111010100001100000001110001100111001101E30", 2, MPFR_RNDN); mpfr_exp (x, x, MPFR_RNDD); if (mpfr_cmp_str (x, ".1111111111111111111111111111111111111111111111111111111111111111E1073741823", 2, MPFR_RNDN) != 0) { printf ("Wrong overflow detection in mpfr_exp\n"); mpfr_dump (x); exit (1); } /* Check underflow. Corner case of mpfr_exp_2 */ mpfr_set_str (x, "-0.1011000101110010000101111111011111010001110011110111100110101100E30", 2, MPFR_RNDN); mpfr_exp (x, x, MPFR_RNDN); if (mpfr_cmp_str (x, "0.1E-1073741823", 2, MPFR_RNDN) != 0) { printf ("Wrong underflow (1) detection in mpfr_exp\n"); mpfr_dump (x); exit (1); } mpfr_set_str (x, "-0.1011001101110010000101111111011111010001110011110111100110111101E30", 2, MPFR_RNDN); mpfr_exp (x, x, MPFR_RNDN); if (mpfr_cmp_ui (x, 0) != 0) { printf ("Wrong underflow (2) detection in mpfr_exp\n"); mpfr_dump (x); exit (1); } /* Check overflow. Corner case of mpfr_exp_3 */ if (MPFR_PREC_MAX >= MPFR_EXP_THRESHOLD + 10 && MPFR_PREC_MAX >= 64) { /* this ensures that for small MPFR_EXP_THRESHOLD, the following mpfr_set_str conversion is exact */ mpfr_set_prec (x, (MPFR_EXP_THRESHOLD + 10 > 64) ? MPFR_EXP_THRESHOLD + 10 : 64); mpfr_set_str (x, "0.1011000101110010000101111111010100001100000001110001100111001101E30", 2, MPFR_RNDN); mpfr_clear_overflow (); mpfr_exp (x, x, MPFR_RNDD); if (!mpfr_overflow_p ()) { printf ("Wrong overflow detection in mpfr_exp_3\n"); mpfr_dump (x); exit (1); } /* Check underflow. Corner case of mpfr_exp_3 */ mpfr_set_str (x, "-0.1011000101110010000101111111011111010001110011110111100110101100E30", 2, MPFR_RNDN); mpfr_clear_underflow (); mpfr_exp (x, x, MPFR_RNDN); if (!mpfr_underflow_p ()) { printf ("Wrong underflow detection in mpfr_exp_3\n"); mpfr_dump (x); exit (1); } mpfr_set_prec (x, 53); } /* check overflow */ set_emax (10); mpfr_set_ui (x, 7, MPFR_RNDN); test_exp (y, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) { printf ("Error for exp(7) for emax=10\n"); exit (1); } set_emax (emax); /* check underflow */ set_emin (-10); mpfr_set_si (x, -9, MPFR_RNDN); test_exp (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) { printf ("Error for exp(-9) for emin=-10\n"); printf ("Expected +0\n"); printf ("Got "); mpfr_print_binary (y); puts (""); exit (1); } set_emin (emin); /* check case EXP(x) < -precy */ mpfr_set_prec (y, 2); mpfr_set_str_binary (x, "-0.1E-3"); test_exp (y, x, MPFR_RNDD); if (mpfr_cmp_ui_2exp (y, 3, -2)) { printf ("Error for exp(-1/16), prec=2, RNDD\n"); printf ("expected 0.11, got "); mpfr_dump (y); exit (1); } test_exp (y, x, MPFR_RNDZ); if (mpfr_cmp_ui_2exp (y, 3, -2)) { printf ("Error for exp(-1/16), prec=2, RNDZ\n"); printf ("expected 0.11, got "); mpfr_dump (y); exit (1); } mpfr_set_str_binary (x, "0.1E-3"); test_exp (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1)) { printf ("Error for exp(1/16), prec=2, RNDN\n"); exit (1); } test_exp (y, x, MPFR_RNDU); if (mpfr_cmp_ui_2exp (y, 3, -1)) { printf ("Error for exp(1/16), prec=2, RNDU\n"); exit (1); } /* bug reported by Franky Backeljauw, 28 Mar 2003 */ mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_str_binary (x, "1.1101011000111101011110000111010010101001101001110111e28"); test_exp (y, x, MPFR_RNDN); mpfr_set_prec (x, 153); mpfr_set_prec (z, 153); mpfr_set_str_binary (x, "1.1101011000111101011110000111010010101001101001110111e28"); test_exp (z, x, MPFR_RNDN); mpfr_prec_round (z, 53, MPFR_RNDN); if (mpfr_cmp (y, z)) { printf ("Error in mpfr_exp for large argument\n"); exit (1); } /* corner cases in mpfr_exp_3 */ mpfr_set_prec (x, 2); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_set_prec (y, 2); mpfr_exp_3 (y, x, MPFR_RNDN); /* Check some little things about overflow detection */ set_emin (-125); set_emax (128); mpfr_set_prec (x, 107); mpfr_set_prec (y, 107); mpfr_set_str_binary (x, "0.11110000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000" "00000000E4"); test_exp (y, x, MPFR_RNDN); if (mpfr_cmp_str (y, "0.11000111100001100110010101111101011010010101010000" "1101110111100010111001011111111000110111001011001101010" "01E22", 2, MPFR_RNDN)) { printf ("Special overflow error (1)\n"); mpfr_dump (y); exit (1); } set_emin (emin); set_emax (emax); /* Check for overflow producing a segfault with HUGE exponent */ mpfr_set_ui (x, 3, MPFR_RNDN); mpfr_mul_2ui (x, x, 32, MPFR_RNDN); test_exp (y, x, MPFR_RNDN); /* Can't test return value: May overflow or not*/ /* Bug due to wrong approximation of (x)/log2 */ mpfr_set_prec (x, 163); mpfr_set_str (x, "-4.28ac8fceeadcda06bb56359017b1c81b85b392e7", 16, MPFR_RNDN); mpfr_exp (x, x, MPFR_RNDN); if (mpfr_cmp_str (x, "3.fffffffffffffffffffffffffffffffffffffffe8@-2", 16, MPFR_RNDN)) { printf ("Error for x= -4.28ac8fceeadcda06bb56359017b1c81b85b392e7"); printf ("expected 3.fffffffffffffffffffffffffffffffffffffffe8@-2"); printf ("Got "); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); } /* bug found by Guillaume Melquiond, 13 Sep 2005 */ mpfr_set_prec (x, 53); mpfr_set_str_binary (x, "-1E-400"); mpfr_exp (x, x, MPFR_RNDZ); if (mpfr_cmp_ui (x, 1) == 0) { printf ("Error for exp(-2^(-400))\n"); exit (1); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
static void special_overflow (void) { mpfr_t x, y; mpfr_exp_t emin, emax; emin = mpfr_get_emin (); emax = mpfr_get_emax (); set_emin (-125); set_emax (128); mpfr_init2 (x, 24); mpfr_init2 (y, 48); mpfr_set_str_binary (x, "0.101101010001001101111010E0"); mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_str (y, "0.100111011001100111000010111101000111010101011110E0", 2, MPFR_RNDN)) { printf("Special Overflow error.\n"); mpfr_dump (y); exit (1); } /* intermediate Pi overflows while atan(+Inf) = Pi/2 is representable */ set_emax (1); mpfr_set_inf (x, +1); mpfr_clear_flags (); mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_str (y, "C90FDAA22169p-47", 16, MPFR_RNDN) || mpfr_overflow_p ()) { printf("atan(+Inf) = Pi/2 should not overflow when emax = %ld\n", (long int) mpfr_get_emax ()); mpfr_dump (y); exit (1); } /* atan(+Inf) = Pi/2 underflows */ set_emax (128); set_emin (3); mpfr_clear_flags (); mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || !mpfr_underflow_p ()) { printf("atan(+Inf) = Pi/2 should underflow when emin = %ld\n", (long int) mpfr_get_emin ()); mpfr_dump (y); exit (1); } /* intermediate Pi overflows while atan(+1) = Pi/4 is representable */ set_emax (1); set_emin (-128); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_clear_flags (); mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_str (y, "C90FDAA22169p-48", 16, MPFR_RNDN) || mpfr_overflow_p ()) { printf("atan(+1) = Pi/4 should not overflow when emax = %ld\n", (long int) mpfr_get_emax ()); mpfr_dump (y); exit (1); } /* atan(+1) = Pi/4 underflows and is rounded up to 1 */ set_emax (128); set_emin (1); mpfr_set_prec (y, 2); mpfr_clear_flags (); mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1) || !mpfr_underflow_p ()) { printf("atan(+1) = Pi/4 should underflow when emin = %+ld\n", (long int) mpfr_get_emin ()); mpfr_dump (y); exit (1); } /* atan(+1) = Pi/4 underflows and is rounded down to 0 */ mpfr_clear_flags (); mpfr_atan (y, x, MPFR_RNDD); if (mpfr_cmp_ui (y, 0) || !mpfr_underflow_p ()) { printf("atan(+1) = Pi/4 should underflow when emin = %+ld\n", (long int) mpfr_get_emin ()); mpfr_dump (y); exit (1); } mpfr_clear (y); mpfr_clear (x); set_emin (emin); set_emax (emax); }
static void special (void) { mpfr_t x, y, z; int r; int i; mpfr_init2 (x, 53); mpfr_init2 (y, 53); mpfr_init2 (z, 53); mpfr_set_str_binary (x, "1.0000100110000001100111100011001110101110100111011101"); mpfr_set_str_binary (y, "1.1001101101110100101100110011011101101000011010111110e-1"); mpfr_atan (z, x, MPFR_RNDN); if (mpfr_cmp (y, z)) { printf ("Error in mpfr_atan for prec=53, rnd=MPFR_RNDN\n"); printf ("x="); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); printf ("\nexpected "); mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN); printf ("\ngot "); mpfr_out_str (stdout, 2, 0, z, MPFR_RNDN); printf ("\n"); exit (1); } /* atan(+Inf) = Pi/2 */ for (r = 0; r < MPFR_RND_MAX ; r++) { mpfr_set_inf (x, 1); mpfr_atan (y, x, (mpfr_rnd_t) r); mpfr_const_pi (x, (mpfr_rnd_t) r); mpfr_div_2exp (x, x, 1, (mpfr_rnd_t) r); if (mpfr_cmp (x, y)) { printf ("Error: mpfr_atan(+Inf), rnd=%s\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r)); exit (1); } } /* atan(-Inf) = - Pi/2 */ for (r = 0; r < MPFR_RND_MAX ; r++) { mpfr_set_inf (x, -1); mpfr_atan (y, x, (mpfr_rnd_t) r); mpfr_const_pi (x, MPFR_INVERT_RND((mpfr_rnd_t) r)); mpfr_neg (x, x, (mpfr_rnd_t) r); mpfr_div_2exp (x, x, 1, (mpfr_rnd_t) r); if (mpfr_cmp (x, y)) { printf ("Error: mpfr_atan(-Inf), rnd=%s\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r)); exit (1); } } /* atan(NaN) = NaN */ mpfr_set_nan (x); mpfr_atan (y, x, MPFR_RNDN); if (!mpfr_nan_p (y)) { printf ("Error: mpfr_atan(NaN) <> NaN\n"); exit (1); } /* atan(+/-0) = +/-0 */ mpfr_set_ui (x, 0, MPFR_RNDN); MPFR_SET_NEG (y); mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || MPFR_IS_NEG (y)) { printf ("Error: mpfr_atan (+0) <> +0\n"); exit (1); } mpfr_atan (x, x, MPFR_RNDN); if (mpfr_cmp_ui (x, 0) || MPFR_IS_NEG (x)) { printf ("Error: mpfr_atan (+0) <> +0 (in place)\n"); exit (1); } mpfr_neg (x, x, MPFR_RNDN); MPFR_SET_POS (y); mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || MPFR_IS_POS (y)) { printf ("Error: mpfr_atan (-0) <> -0\n"); exit (1); } mpfr_atan (x, x, MPFR_RNDN); if (mpfr_cmp_ui (x, 0) || MPFR_IS_POS (x)) { printf ("Error: mpfr_atan (-0) <> -0 (in place)\n"); exit (1); } mpfr_set_prec (x, 32); mpfr_set_prec (y, 32); /* test one random positive argument */ mpfr_set_str_binary (x, "0.10000100001100101001001001011001"); mpfr_atan (x, x, MPFR_RNDN); mpfr_set_str_binary (y, "0.1111010000001111001111000000011E-1"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_atan (1)\n"); exit (1); } /* test one random negative argument */ mpfr_set_str_binary (x, "-0.1100001110110000010101011001011"); mpfr_atan (x, x, MPFR_RNDN); mpfr_set_str_binary (y, "-0.101001110001010010110001110001"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_atan (2)\n"); mpfr_print_binary (x); printf ("\n"); mpfr_print_binary (y); printf ("\n"); exit (1); } mpfr_set_prec (x, 3); mpfr_set_prec (y, 192); mpfr_set_prec (z, 192); mpfr_set_str_binary (x, "-0.100e1"); mpfr_atan (z, x, MPFR_RNDD); mpfr_set_str_binary (y, "-0.110010010000111111011010101000100010000101101000110000100011010011000100110001100110001010001011100000001101110000011100110100010010100100000010010011100000100010001010011001111100110001110101"); if (mpfr_cmp (z, y)) { printf ("Error in mpfr_atan (3)\n"); printf ("Expected "); mpfr_print_binary (y); printf ("\n"); printf ("Got "); mpfr_print_binary (z); printf ("\n"); exit (1); } /* Test regression */ mpfr_set_prec (x, 51); mpfr_set_prec (y, 51); mpfr_set_str_binary (x, "0.101100100000101111111010001111111000001000000000000E-11"); i = mpfr_atan (y, x, MPFR_RNDN); if (mpfr_cmp_str (y, "1.01100100000101111111001110011001010110100100000000e-12", 2, MPFR_RNDN) || i >= 0) { printf ("Wrong Regression test (%d)\n", i); mpfr_dump (y); exit (1); } mpfr_set_si (x, -1, MPFR_RNDN); mpfr_atan (x, x, MPFR_RNDN); MPFR_ASSERTN (MPFR_IS_NEG (x)); /* Test regression */ mpfr_set_prec (x, 48); mpfr_set_prec (y, 48); mpfr_set_str_binary (x, "1.11001110010000011111100000010000000000000000000e-19"); mpfr_atan (y, x, MPFR_RNDD); if (mpfr_cmp_str (y, "0.111001110010000011111100000001111111110000010011E-18", 2, MPFR_RNDN)) { printf ("Error in mpfr_atan (4)\n"); printf ("Input 1.11001110010000011111100000010000000000000000000e-19 [prec=48]\n"); printf ("Expected 0.111001110010000011111100000001111111110000010011E-18\n"); printf ("Got "); mpfr_dump (y); exit (1); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
static void normal (void) { int inexact; mpfr_t x, y; mpfr_init (x); mpfr_init (y); /* x1 = 2^-3 */ mpfr_set_str (x, "1p-3", 2, MPFR_RNDD); mpfr_li2 (x, x, MPFR_RNDN); if (mpfr_cmp_str (x, "0x1087a7a9e42141p-55", 16, MPFR_RNDN) != 0) { printf ("Error for li2(x1)\n"); exit (1); } /* check MPFR_FAST_COMPUTE_IF_SMALL_INPUT */ mpfr_set_prec (x, 2); mpfr_set_prec (y, 20); mpfr_set_ui_2exp (x, 1, -21, MPFR_RNDN); mpfr_li2 (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp (y, x) == 0); mpfr_set_si_2exp (x, -1, -21, MPFR_RNDN); mpfr_li2 (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp (y, x) == 0); /* worst case */ /* x2 = 0x7F18EA6537E00E983196CDDC6EFAC57Fp-129 Li2(x2) = 2^-2 + 2^-6 + 2^-120 */ mpfr_set_prec (x, 128); mpfr_set_str (x, "7F18EA6537E00E983196CDDC6EFAC57Fp-129", 16, MPFR_RNDN); /* round to nearest mode and 4 bits of precision, it should be rounded to 2^-2 + 2^-5 and */ mpfr_set_prec (y, 4); inexact = mpfr_li2 (y, x, MPFR_RNDN); if (inexact != 1 || mpfr_cmp_str (y, "0.1001p-1", 2, MPFR_RNDN) != 0) { printf ("Error for li2(x2, RNDN)\n"); exit (1); } /* round toward zero mode and 5 bits of precision, it should be rounded to 2^-2 + 2^-6 */ mpfr_set_prec (y, 5); inexact = mpfr_li2 (y, x, MPFR_RNDZ); if (inexact != -1 || mpfr_cmp_str (y, "0.10001p-1", 2, MPFR_RNDN) != 0) { printf ("Error for li2(x2, RNDZ)\n"); exit (1); } /* round away from zero mode and 5 bits of precision, it should be rounded to 2^-2 + 2^-5 */ inexact = mpfr_li2 (y, x, MPFR_RNDU); if (inexact != 1 || mpfr_cmp_str (y, "0.10010p-1", 2, MPFR_RNDN) != 0) { printf ("Error for li2(x2, RNDU)\n"); exit (1); } mpfr_clear (x); mpfr_clear (y); }
static void check_regression (void) { mpfr_t x, y, z; int i; FILE *fp; char s[BUFSIZE]; mpfr_inits2 (6177, x, y, z, (mpfr_ptr) 0); /* we read long strings from a file since ISO C90 does not support strings of length > 509 */ fp = src_fopen ("tmul.dat", "r"); if (fp == NULL) { fprintf (stderr, "Error, cannot open tmul.dat in srcdir\n"); exit (1); } get_string (s, fp); mpfr_set_str (y, s, 16, MPFR_RNDN); get_string (s, fp); mpfr_set_str (z, s, 16, MPFR_RNDN); i = mpfr_mul (x, y, z, MPFR_RNDN); get_string (s, fp); if (mpfr_cmp_str (x, s, 16, MPFR_RNDN) != 0 || i != -1) { printf ("Regression test 1 failed (i=%d, expected -1)\nx=", i); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } fclose (fp); mpfr_set_prec (x, 606); mpfr_set_prec (y, 606); mpfr_set_prec (z, 606); mpfr_set_str (y, "-f.ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92daefc3f8052ca9f58736564d9e93e62d324@-1", 16, MPFR_RNDN); mpfr_set_str (z, "-f.ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92daefc3f8052ca9f58736564d9e93e62d324@-1", 16, MPFR_RNDN); i = mpfr_mul (x, y, z, MPFR_RNDU); mpfr_set_str (y, "f.ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff25b5df87f00a5953eb0e6cac9b3d27cc5a64c@-1", 16, MPFR_RNDN); if (mpfr_cmp (x, y) || i <= 0) { printf ("Regression test (2) failed! (i=%d - Expected 1)\n", i); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_set_prec (x, 184); mpfr_set_prec (y, 92); mpfr_set_prec (z, 1023); mpfr_set_str (y, "6.9b8c8498882770d8038c3b0@-1", 16, MPFR_RNDN); mpfr_set_str (z, "7.44e24b986e7fb296f1e936ce749fec3504cbf0d5ba769466b1c9f1578115efd5d29b4c79271191a920a99280c714d3a657ad6e3afbab77ffce9d697e9bb9110e26d676069afcea8b69f1d1541f2365042d80a97c21dcccd8ace4f1bb58b49922003e738e6f37bb82ef653cb2e87f763974e6ae50ae54e7724c38b80653e3289@255", 16, MPFR_RNDN); i = mpfr_mul (x, y, z, MPFR_RNDU); mpfr_set_prec (y, 184); mpfr_set_str (y, "3.0080038f2ac5054e3e71ccbb95f76aaab2221715025a28@255", 16, MPFR_RNDN); if (mpfr_cmp (x, y) || i <= 0) { printf ("Regression test (4) failed! (i=%d - expected 1)\n", i); printf ("Ref: 3.0080038f2ac5054e3e71ccbb95f76aaab2221715025a28@255\n" "Got: "); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); printf ("\n"); exit (1); } mpfr_set_prec (x, 908); mpfr_set_prec (y, 908); mpfr_set_prec (z, 908); mpfr_set_str (y, "-f.fffffffffffffffffffffffffffffffffffffffffffffffffffffff" "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" "ffffffffffffffffffffffffffffffffffffffffffffffffffffff99be91f83ec6f0ed28a3d42" "e6e9a327230345ea6@-1", 16, MPFR_RNDN); mpfr_set_str (z, "-f.fffffffffffffffffffffffffffffffffffffffffffffffffffffff" "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" "ffffffffffffffffffffffffffffffffffffffffffffffffffffff99be91f83ec6f0ed28a3d42" "e6e9a327230345ea6@-1", 16, MPFR_RNDN); i = mpfr_mul (x, y, z, MPFR_RNDU); mpfr_set_str (y, "f.ffffffffffffffffffffffffffffffffffffffffffffffffffffffff" "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" "fffffffffffffffffffffffffffffffffffffffffffffffffffff337d23f07d8de1da5147a85c" "dd3464e46068bd4d@-1", 16, MPFR_RNDN); if (mpfr_cmp (x, y) || i <= 0) { printf ("Regression test (5) failed! (i=%d - expected 1)\n", i); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); printf ("\n"); exit (1); } mpfr_set_prec (x, 50); mpfr_set_prec (y, 40); mpfr_set_prec (z, 53); mpfr_set_str (y, "4.1ffffffff8", 16, MPFR_RNDN); mpfr_set_str (z, "4.2000000ffe0000@-4", 16, MPFR_RNDN); i = mpfr_mul (x, y, z, MPFR_RNDN); if (mpfr_cmp_str (x, "1.104000041d6c0@-3", 16, MPFR_RNDN) != 0 || i <= 0) { printf ("Regression test (6) failed! (i=%d - expected 1)\nx=", i); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); printf ("\nMore prec="); mpfr_set_prec (x, 93); mpfr_mul (x, y, z, MPFR_RNDN); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); printf ("\n"); exit (1); } mpfr_set_prec (x, 439); mpfr_set_prec (y, 393); mpfr_set_str (y, "-1.921fb54442d18469898cc51701b839a252049c1114cf98e804177d" "4c76273644a29410f31c6809bbdf2a33679a748636600", 16, MPFR_RNDN); i = mpfr_mul (x, y, y, MPFR_RNDU); if (mpfr_cmp_str (x, "2.77a79937c8bbcb495b89b36602306b1c2159a8ff834288a19a08" "84094f1cda3dc426da61174c4544a173de83c2500f8bfea2e0569e3698", 16, MPFR_RNDN) != 0 || i <= 0) { printf ("Regression test (7) failed! (i=%d - expected 1)\nx=", i); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); printf ("\n"); exit (1); } mpfr_set_prec (x, 1023); mpfr_set_prec (y, 1023); mpfr_set_prec (z, 511); mpfr_set_ui (x, 17, MPFR_RNDN); mpfr_set_ui (y, 42, MPFR_RNDN); i = mpfr_mul (z, x, y, MPFR_RNDN); if (mpfr_cmp_ui (z, 17*42) != 0 || i != 0) { printf ("Regression test (8) failed! (i=%d - expected 0)\nz=", i); mpfr_out_str (stdout, 16, 0, z, MPFR_RNDN); printf ("\n"); exit (1); } mpfr_clears (x, y, z, (mpfr_ptr) 0); }
static void special_overflow (void) { mpfr_t x, y; int i; mpfr_exp_t emin, emax; emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_clear_overflow (); set_emin (-125); set_emax (128); mpfr_init2 (x, 24); mpfr_init2 (y, 24); mpfr_set_str_binary (x, "0.101100100000000000110100E7"); i = mpfr_tanh (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1) || i != 1) { printf("Overflow error (1). i=%d\ny=", i); mpfr_dump (y); exit (1); } MPFR_ASSERTN (!mpfr_overflow_p ()); i = mpfr_tanh (y, x, MPFR_RNDZ); if (mpfr_cmp_str (y, "0.111111111111111111111111E0", 2, MPFR_RNDN) || i != -1) { printf("Overflow error (2).i=%d\ny=", i); mpfr_dump (y); exit (1); } MPFR_ASSERTN (!mpfr_overflow_p ()); set_emin (emin); set_emax (emax); mpfr_set_str_binary (x, "0.1E1000000000"); i = mpfr_tanh (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1) || i != 1) { printf("Overflow error (3). i=%d\ny=", i); mpfr_dump (y); exit (1); } MPFR_ASSERTN (!mpfr_overflow_p ()); mpfr_set_str_binary (x, "-0.1E1000000000"); i = mpfr_tanh (y, x, MPFR_RNDU); if (mpfr_cmp_str (y, "-0.111111111111111111111111E0", 2, MPFR_RNDN) || i != 1) { printf("Overflow error (4). i=%d\ny=", i); mpfr_dump (y); exit (1); } mpfr_clear (y); mpfr_clear (x); }
static void test_sign (void) { mpfr_t pid, piu, x, y; int p, k; mpfr_init2 (pid, 4096); mpfr_const_pi (pid, MPFR_RNDD); mpfr_init2 (piu, 4096); mpfr_const_pi (piu, MPFR_RNDU); mpfr_init (x); mpfr_init2 (y, 2); for (p = 8; p <= 128; p++) for (k = 2; k <= 6; k += 2) { mpfr_set_prec (x, p); mpfr_mul_ui (x, pid, k, MPFR_RNDD); test_sin (y, x, MPFR_RNDN); if (MPFR_SIGN(y) > 0) { printf ("Error in test_sign for sin(%dpi-epsilon), prec = %d" " for argument.\nResult should have been negative.\n", k, p); exit (1); } mpfr_mul_ui (x, piu, k, MPFR_RNDU); test_sin (y, x, MPFR_RNDN); if (MPFR_SIGN(y) < 0) { printf ("Error in test_sign for sin(%dpi+epsilon), prec = %d" " for argument.\nResult should have been positive.\n", k, p); exit (1); } } /* worst case on 53 bits */ mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_str (x, "6134899525417045", 10, MPFR_RNDN); test_sin (y, x, MPFR_RNDN); mpfr_set_str_binary (x, "11011010111101011110111100010101010101110000000001011E-106"); MPFR_ASSERTN(mpfr_cmp (x, y) == 0); /* Bug on Special cases */ mpfr_set_str_binary (x, "0.100011011010111101E-32"); test_sin (y, x, MPFR_RNDN); if (mpfr_cmp_str (y, "0.10001101101011110100000000000000000000000000000000000E-32", 2, MPFR_RNDN)) { printf("sin special 97 error:\nx="); mpfr_dump (x); printf("y="); mpfr_dump (y); exit (1); } mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_str_binary (x, "1.1001001000011111101101010100010001000010110100010011"); mpfr_set_str_binary (y, "1.1111111111111111111111111111111111111111111111111111e-1"); test_sin (x, x, MPFR_RNDZ); MPFR_ASSERTN(mpfr_cmp (x, y) == 0); mpfr_clear (pid); mpfr_clear (piu); mpfr_clear (x); mpfr_clear (y); }
int main (int argc, char *argv[]) { mpfr_t w,z; unsigned long k; int i; tests_start_mpfr (); mpfr_inits2 (53, w, z, (mpfr_ptr) 0); for (i = 0; i < 3; i++) { mpfr_set_inf (w, 1); test_mul (i, 0, w, w, 10, MPFR_RNDZ); if (!MPFR_IS_INF(w)) { printf ("Result is not Inf (i = %d)\n", i); exit (1); } mpfr_set_nan (w); test_mul (i, 0, w, w, 10, MPFR_RNDZ); if (!MPFR_IS_NAN(w)) { printf ("Result is not NaN (i = %d)\n", i); exit (1); } for (k = 0 ; k < numberof(val) ; k+=3) { mpfr_set_str (w, val[k], 16, MPFR_RNDN); test_mul (i, 0, z, w, 10, MPFR_RNDZ); if (mpfr_cmp_str (z, val[k+1], 16, MPFR_RNDN)) { printf ("ERROR for x * 2^n (i = %d) for %s\n", i, val[k]); printf ("Expected: %s\n" "Got : ", val[k+1]); mpfr_out_str (stdout, 16, 0, z, MPFR_RNDN); putchar ('\n'); exit (1); } test_mul (i, 1, z, w, 10, MPFR_RNDZ); if (mpfr_cmp_str (z, val[k+2], 16, MPFR_RNDN)) { printf ("ERROR for x / 2^n (i = %d) for %s\n", i, val[k]); printf ("Expected: %s\n" "Got : ", val[k+2]); mpfr_out_str (stdout, 16, 0, z, MPFR_RNDN); putchar ('\n'); exit (1); } } mpfr_set_inf (w, 1); mpfr_nextbelow (w); test_mul (i, 0, w, w, 1, MPFR_RNDN); if (!mpfr_inf_p (w)) { printf ("Overflow error (i = %d)!\n", i); exit (1); } mpfr_set_ui (w, 0, MPFR_RNDN); mpfr_nextabove (w); test_mul (i, 1, w, w, 1, MPFR_RNDN); if (mpfr_cmp_ui (w, 0)) { printf ("Underflow error (i = %d)!\n", i); exit (1); } } if (MPFR_EXP_MAX >= LONG_MAX/2 && MPFR_EXP_MIN <= LONG_MAX/2-LONG_MAX-1) { unsigned long lmp1 = (unsigned long) LONG_MAX + 1; mpfr_set_ui (w, 1, MPFR_RNDN); mpfr_mul_2ui (w, w, LONG_MAX/2, MPFR_RNDZ); mpfr_div_2ui (w, w, lmp1, MPFR_RNDZ); mpfr_mul_2ui (w, w, lmp1 - LONG_MAX/2, MPFR_RNDZ); if (!mpfr_cmp_ui (w, 1)) { printf ("Underflow LONG_MAX error!\n"); exit (1); } } mpfr_clears (w, z, (mpfr_ptr) 0); underflow0 (); large0 (); tests_end_mpfr (); return 0; }
int main (int argc, char *argv[]) { mpfr_t x, c, s, c2, s2; tests_start_mpfr (); check_regression (); check_nans (); /* worst case from PhD thesis of Vincent Lefe`vre: x=8980155785351021/2^54 */ check53 ("4.984987858808754279e-1", "4.781075595393330379e-1", MPFR_RNDN); check53 ("4.984987858808754279e-1", "4.781075595393329824e-1", MPFR_RNDD); check53 ("4.984987858808754279e-1", "4.781075595393329824e-1", MPFR_RNDZ); check53 ("4.984987858808754279e-1", "4.781075595393330379e-1", MPFR_RNDU); check53 ("1.00031274099908640274", "8.416399183372403892e-1", MPFR_RNDN); check53 ("1.00229256850978698523", "8.427074524447979442e-1", MPFR_RNDZ); check53 ("1.00288304857059840103", "8.430252033025980029e-1", MPFR_RNDZ); check53 ("1.00591265847407274059", "8.446508805292128885e-1", MPFR_RNDN); /* Other worst cases showing a bug introduced on 2005-01-29 in rev 3248 */ check53b ("1.0111001111010111010111111000010011010001110001111011e-21", "1.0111001111010111010111111000010011010001101001110001e-21", MPFR_RNDU); check53b ("1.1011101111111010000001010111000010000111100100101101", "1.1111100100101100001111100000110011110011010001010101e-1", MPFR_RNDU); mpfr_init2 (x, 2); mpfr_set_str (x, "0.5", 10, MPFR_RNDN); test_sin (x, x, MPFR_RNDD); if (mpfr_cmp_ui_2exp (x, 3, -3)) /* x != 0.375 = 3/8 */ { printf ("mpfr_sin(0.5, MPFR_RNDD) failed with precision=2\n"); exit (1); } /* bug found by Kevin Ryde */ mpfr_const_pi (x, MPFR_RNDN); mpfr_mul_ui (x, x, 3L, MPFR_RNDN); mpfr_div_ui (x, x, 2L, MPFR_RNDN); test_sin (x, x, MPFR_RNDN); if (mpfr_cmp_ui (x, 0) >= 0) { printf ("Error: wrong sign for sin(3*Pi/2)\n"); exit (1); } /* Can fail on an assert */ mpfr_set_prec (x, 53); mpfr_set_str (x, "77291789194529019661184401408", 10, MPFR_RNDN); mpfr_init2 (c, 4); mpfr_init2 (s, 42); mpfr_init2 (c2, 4); mpfr_init2 (s2, 42); test_sin (s, x, MPFR_RNDN); mpfr_cos (c, x, MPFR_RNDN); mpfr_sin_cos (s2, c2, x, MPFR_RNDN); if (mpfr_cmp (c2, c)) { printf("cos differs for x=77291789194529019661184401408"); exit (1); } if (mpfr_cmp (s2, s)) { printf("sin differs for x=77291789194529019661184401408"); exit (1); } mpfr_set_str_binary (x, "1.1001001000011111101101010100010001000010110100010011"); test_sin (x, x, MPFR_RNDZ); if (mpfr_cmp_str (x, "1.1111111111111111111111111111111111111111111111111111e-1", 2, MPFR_RNDN)) { printf ("Error for x= 1.1001001000011111101101010100010001000010110100010011\nGot "); mpfr_dump (x); exit (1); } mpfr_set_prec (s, 9); mpfr_set_prec (x, 190); mpfr_const_pi (x, MPFR_RNDN); mpfr_sin (s, x, MPFR_RNDZ); if (mpfr_cmp_str (s, "0.100000101e-196", 2, MPFR_RNDN)) { printf ("Error for x ~= pi\n"); mpfr_dump (s); exit (1); } mpfr_clear (s2); mpfr_clear (c2); mpfr_clear (s); mpfr_clear (c); mpfr_clear (x); test_generic (2, 100, 15); test_generic (MPFR_SINCOS_THRESHOLD-1, MPFR_SINCOS_THRESHOLD+1, 2); test_sign (); check_tiny (); data_check ("data/sin", mpfr_sin, "mpfr_sin"); bad_cases (mpfr_sin, mpfr_asin, "mpfr_sin", 256, -40, 0, 4, 128, 800, 50); tests_end_mpfr (); return 0; }
static void special (void) { mpfr_t x, y; int i; mpfr_init (x); mpfr_init (y); mpfr_set_nan (x); test_expm1 (y, x, MPFR_RNDN); if (!mpfr_nan_p (y)) { printf ("Error for expm1(NaN)\n"); exit (1); } mpfr_set_inf (x, 1); test_expm1 (y, x, MPFR_RNDN); if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) { printf ("Error for expm1(+Inf)\n"); exit (1); } mpfr_set_inf (x, -1); test_expm1 (y, x, MPFR_RNDN); if (mpfr_cmp_si (y, -1)) { printf ("Error for expm1(-Inf)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); test_expm1 (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) { printf ("Error for expm1(+0)\n"); exit (1); } mpfr_neg (x, x, MPFR_RNDN); test_expm1 (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) > 0) { printf ("Error for expm1(-0)\n"); exit (1); } /* Check overflow of expm1(x) */ mpfr_clear_flags (); mpfr_set_str_binary (x, "1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDN); MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_SIGN (x) > 0); MPFR_ASSERTN (mpfr_overflow_p ()); MPFR_ASSERTN (i == 1); mpfr_clear_flags (); mpfr_set_str_binary (x, "1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDU); MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_SIGN (x) > 0); MPFR_ASSERTN (mpfr_overflow_p ()); MPFR_ASSERTN (i == 1); mpfr_clear_flags (); mpfr_set_str_binary (x, "1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDD); MPFR_ASSERTN (!MPFR_IS_INF (x) && MPFR_SIGN (x) > 0); MPFR_ASSERTN (mpfr_overflow_p ()); MPFR_ASSERTN (i == -1); /* Check internal underflow of expm1 (x) */ mpfr_set_prec (x, 2); mpfr_clear_flags (); mpfr_set_str_binary (x, "-1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_cmp_si (x, -1) == 0); MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ()); MPFR_ASSERTN (i == -1); mpfr_set_str_binary (x, "-1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDD); MPFR_ASSERTN (mpfr_cmp_si (x, -1) == 0); MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ()); MPFR_ASSERTN (i == -1); mpfr_set_str_binary (x, "-1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDZ); MPFR_ASSERTN (mpfr_cmp_str (x, "-0.11", 2, MPFR_RNDN) == 0); MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ()); MPFR_ASSERTN (i == 1); mpfr_set_str_binary (x, "-1.1E1000000000"); i = test_expm1 (x, x, MPFR_RNDU); MPFR_ASSERTN (mpfr_cmp_str (x, "-0.11", 2, MPFR_RNDN) == 0); MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ()); MPFR_ASSERTN (i == 1); mpfr_clear (x); mpfr_clear (y); }