int main (void) { mpfr_t x, y, z; double d; int inexact; tests_start_mpfr (); /* check with enough precision */ mpfr_init2 (x, IEEE_DBL_MANT_DIG); mpfr_init2 (y, IEEE_DBL_MANT_DIG); mpfr_init2 (z, IEEE_DBL_MANT_DIG); mpfr_set_str (y, "4096", 10, MPFR_RNDN); d = 0.125; mpfr_clear_flags (); inexact = mpfr_mul_d (x, y, d, MPFR_RNDN); if (inexact != 0) { printf ("Inexact flag error in mpfr_mul_d\n"); exit (1); } mpfr_set_str (z, "512", 10, MPFR_RNDN); if (mpfr_cmp (z, x)) { printf ("Error in mpfr_mul_d ("); mpfr_out_str (stdout, 10, 0, y, MPFR_RNDN); printf (" + %.20g)\nexpected ", d); mpfr_out_str (stdout, 10, 0, z, MPFR_RNDN); printf ("\ngot "); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); printf ("\n"); exit (1); } mpfr_clears (x, y, z, (mpfr_ptr) 0); check_nans (); test_generic (2, 1000, 100); tests_end_mpfr (); return 0; }
static void check_reftable (void) { int i, base; mpfr_t x, y; mpfr_prec_t p; char *s; mpfr_init2 (x, 200); mpfr_init2 (y, 200); for (i = 0 ; i < numberof (RefTable) ; i++) { base = RefTable[i].base; p = RefTable[i].prec; mpfr_set_prec (x, p); mpfr_set_prec (y, p); mpfr_set_str_binary (x, RefTable[i].binstr); mpfr_strtofr (y, RefTable[i].str, &s, base, MPFR_RNDN); if (s == NULL || *s != 0) { printf ("strtofr didn't parse entire input for i=%d:\n" " Str=%s", i, RefTable[i].str); exit (1); } if (mpfr_cmp (x, y)) { printf ("Results differ between strtofr and set_binary for i=%d:\n" " Set binary gives: ", i); mpfr_dump (x); printf (" strtofr gives: "); mpfr_dump (y); printf (" setstr gives: "); mpfr_set_str (x, RefTable[i].str, base, MPFR_RNDN); mpfr_dump (x); mpfr_set_prec (x, 2*p); mpfr_set_str (x, RefTable[i].str, base, MPFR_RNDN); printf (" setstr ++ gives: "); mpfr_dump (x); exit (1); } } mpfr_clear (y); mpfr_clear (x); }
/* check adding n random numbers, iterated k times */ static void check_random (int n, int k, mpfr_prec_t prec, mpfr_rnd_t rnd) { mpfr_t *x, s, ref_s; mpfr_ptr *y; int i, st, ret = 0, ref_ret = 0; gmp_randstate_t state; gmp_randinit_default (state); mpfr_init2 (s, prec); mpfr_init2 (ref_s, prec); x = (mpfr_t *) tests_allocate (n * sizeof (mpfr_t)); y = (mpfr_ptr *) tests_allocate (n * sizeof (mpfr_ptr)); for (i = 0; i < n; i++) { y[i] = x[i]; mpfr_init2 (x[i], prec); mpfr_urandom (x[i], state, rnd); } st = cputime (); for (i = 0; i < k; i++) ref_ret = mpfr_sum_naive (ref_s, x, n, rnd); printf ("mpfr_sum_naive took %dms\n", cputime () - st); st = cputime (); for (i = 0; i < k; i++) ret = mpfr_sum (s, y, n, rnd); printf ("mpfr_sum took %dms\n", cputime () - st); if (n <= 2) { MPFR_ASSERTN (mpfr_cmp (ref_s, s) == 0); MPFR_ASSERTN (ref_ret == ret); } for (i = 0; i < n; i++) mpfr_clear (x[i]); tests_free (x, n * sizeof (mpfr_t)); tests_free (y, n * sizeof (mpfr_ptr)); mpfr_clear (s); mpfr_clear (ref_s); gmp_randclear (state); }
int mpfr_root (mpfr_ptr y, mpfr_srcptr x, unsigned long k, mp_rnd_t rnd_mode) { mpz_t m; mp_exp_t e, r, sh; mp_prec_t n, size_m, tmp; int inexact, negative; MPFR_SAVE_EXPO_DECL (expo); if (MPFR_UNLIKELY (k <= 1)) { if (k < 1) /* k==0 => y=x^(1/0)=x^(+Inf) */ #if 0 /* For 0 <= x < 1 => +0. For x = 1 => 1. For x > 1, => +Inf. For x < 0 => NaN. */ { if (MPFR_IS_NEG (x) && !MPFR_IS_ZERO (x)) { MPFR_SET_NAN (y); MPFR_RET_NAN; } inexact = mpfr_cmp (x, __gmpfr_one); if (inexact == 0) return mpfr_set_ui (y, 1, rnd_mode); /* 1 may be Out of Range */ else if (inexact < 0) return mpfr_set_ui (y, 0, rnd_mode); /* 0+ */ else { mpfr_set_inf (y, 1); return 0; } } #endif { MPFR_SET_NAN (y); MPFR_RET_NAN; } else /* y =x^(1/1)=x */ return mpfr_set (y, x, rnd_mode); }
static void test_small (void) { mpfr_t x, y, z1, z2; int inex1, inex2; unsigned int flags; /* Test hypot(x,x) with x = 2^(emin-1). Result is x * sqrt(2). */ mpfr_inits2 (8, x, y, z1, z2, (mpfr_ptr) 0); mpfr_set_si_2exp (x, 1, mpfr_get_emin () - 1, MPFR_RNDN); mpfr_set_si_2exp (y, 1, mpfr_get_emin () - 1, MPFR_RNDN); mpfr_set_ui (z1, 2, MPFR_RNDN); inex1 = mpfr_sqrt (z1, z1, MPFR_RNDN); inex2 = mpfr_mul (z1, z1, x, MPFR_RNDN); MPFR_ASSERTN (inex2 == 0); mpfr_clear_flags (); inex2 = mpfr_hypot (z2, x, y, MPFR_RNDN); flags = __gmpfr_flags; if (mpfr_cmp (z1, z2) != 0) { printf ("Error in test_small%s\nExpected ", ext ? ", extended exponent range" : ""); mpfr_out_str (stdout, 2, 0, z1, MPFR_RNDN); printf ("\nGot "); mpfr_out_str (stdout, 2, 0, z2, MPFR_RNDN); printf ("\n"); exit (1); } if (! SAME_SIGN (inex1, inex2)) { printf ("Bad ternary value in test_small%s\nExpected %d, got %d\n", ext ? ", extended exponent range" : "", inex1, inex2); exit (1); } if (flags != MPFR_FLAGS_INEXACT) { printf ("Bad flags in test_small%s\nExpected %u, got %u\n", ext ? ", extended exponent range" : "", (unsigned int) MPFR_FLAGS_INEXACT, flags); exit (1); } mpfr_clears (x, y, z1, z2, (mpfr_ptr) 0); }
static void check_min(void) { mpfr_t xx, yy, zz; mpfr_init2(xx, 4); mpfr_init2(yy, 4); mpfr_init2(zz, 3); mpfr_set_str1(xx, "0.9375"); mpfr_mul_2si(xx, xx, MPFR_EMIN_DEFAULT/2, MPFR_RNDN); mpfr_set_str1(yy, "0.9375"); mpfr_mul_2si(yy, yy, MPFR_EMIN_DEFAULT - MPFR_EMIN_DEFAULT/2 - 1, MPFR_RNDN); test_mul(zz, xx, yy, MPFR_RNDD); if (mpfr_sgn(zz) != 0) { printf("check_min failed: got "); mpfr_out_str(stdout, 2, 0, zz, MPFR_RNDZ); printf(" instead of 0\n"); exit(1); } test_mul(zz, xx, yy, MPFR_RNDU); mpfr_set_str1 (xx, "0.5"); mpfr_mul_2si(xx, xx, MPFR_EMIN_DEFAULT, MPFR_RNDN); if (mpfr_sgn(xx) <= 0) { printf("check_min failed (internal error)\n"); exit(1); } if (mpfr_cmp(xx, zz) != 0) { printf("check_min failed: got "); mpfr_out_str(stdout, 2, 0, zz, MPFR_RNDZ); printf(" instead of "); mpfr_out_str(stdout, 2, 0, xx, MPFR_RNDZ); printf("\n"); exit(1); } mpfr_clear(xx); mpfr_clear(yy); mpfr_clear(zz); }
static void test_set_overflow (void) { /* static to allow non-constant initialiers in r */ mpfr_t x, inf, max; mpfr_ptr r[4]; int t[4] = { 1, -1, 1, -1 }; int i; int s; mpfr_inits2 (32, x, inf, max, (mpfr_ptr) 0); mpfr_set_inf (inf, 1); mpfr_set_inf (max, 1); mpfr_nextbelow (max); r[0] = r[2] = inf; r[1] = r[3] = max; for (s = 1; s > 0; s = -1) { for (i = 0; i < GMP_RND_MAX ; i++) { int j; int inex; j = s < 0 && i > 1 ? 5 - i : i; inex = mpfr_set_overflow (x, (mp_rnd_t) i, s); if (mpfr_cmp (x, r[j]) || inex * t[j] <= 0) { printf ("Error in test_set_overflow, sign = %d," " rnd_mode = %s\n", s, mpfr_print_rnd_mode ((mp_rnd_t) i)); printf ("Got\n"); mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); printf (", inex = %d\ninstead of\n", inex); mpfr_out_str (stdout, 2, 0, r[j], GMP_RNDN); printf (", inex = %d\n", t[j]); exit (1); } } mpfr_neg (inf, inf, GMP_RNDN); mpfr_neg (max, max, GMP_RNDN); } mpfr_clears (x, inf, max, (mpfr_ptr) 0); }
static void test_erfc (void) { mpfr_t x, y, z; int inex; mpfr_exp_t emin; mpfr_inits2 (40, x, y, z, (mpfr_ptr) 0); mpfr_set_si_2exp (x, -1, -10, MPFR_RNDN); mpfr_set_str_binary (z, "0.1000000000100100000110111010110111100000E1"); mpfr_erfc (y, x, MPFR_RNDN); if (mpfr_cmp (y, z) != 0) { printf ("mpfr_erfc failed for x = "); mpfr_dump (x); printf ("got "); mpfr_dump (y); printf ("instead of "); mpfr_dump (z); exit (1); } /* slowness detected by Kevin Rauch on 26 Oct 2007 */ mpfr_set_prec (x, 128); mpfr_set_si (x, -256, MPFR_RNDN); inex = mpfr_erfc (x, x, MPFR_RNDN); MPFR_ASSERTN(inex > 0 && mpfr_cmp_ui (x, 2) == 0); /* bug found by Pascal Molin on March 10, 2011 */ emin = mpfr_get_emin (); if (! mpfr_set_emin (-1073808789)) { /* Typically, a 64-bit machine. */ mpfr_set_si (x, 27282, MPFR_RNDN); mpfr_erfc (y, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (y, 0) != 0); mpfr_set_emin (emin); } mpfr_clears (x, y, z, (mpfr_ptr) 0); }
void mpfr_taylor_cos_bounded(mpfr_t R, mpfr_t x, unsigned int N) { assert(mpfr_cmp_ui(x, 0) >= 0 && mpfr_cmp(x, MPFR_HALF_PI) <= 0); mpfr_t t, x_2; mpfr_init_set_ui(R, 1, MPFR_RNDN); mpfr_init_set_ui(t, 1, MPFR_RNDN); mpfr_init(x_2); mpfr_mul(x_2, x, x, MPFR_RNDN); for(int n = 1; n < N; n++) { mpfr_div_ui(t, t, (2*n-1)*(2*(n++)), MPFR_RNDN); mpfr_mul(t, t, x_2, MPFR_RNDN); mpfr_sub(R, R, t, MPFR_RNDN); mpfr_div_ui(t, t, (2*n-1)*(2*n), MPFR_RNDN); mpfr_mul(t, t, x_2, MPFR_RNDN); mpfr_add(R, R, t, MPFR_RNDN); } }
slong hadamard_2arg(mpfr_t b,const fmpz_mat_t m) /* upper bound on log2( 2*abs(m det) ) returns -1 if zero row found, smallest row index otherwise b on entry is uninitialized b on exit is initialized iff no zero row found */ { const slong n=m->r; slong smallest=0,j; // gcc warning: initialization from incompatible pointer type --- don't know // how to fix const fmpz** const rows=m->rows; mpfr_t v,u; if(log2_L2_fmpz_3arg( v, rows[0], n )) return -1; mpfr_copy_bound(b, v); // v and b must be freed for(j=1;j<n;j++) { if(log2_L2_fmpz_3arg( u, rows[j], n )) { mpfr_clear(b); mpfr_clear(v); return -1; } mpfr_add_bound(b, u); if( mpfr_cmp(u, v)<0 ) { smallest=j; mpfr_swap(v, u); } mpfr_clear(u); // v and b must be freed } mpfr_clear(v); mpfr_div_ui( b, b, 2, MPFR_RNDU ); // instead of taking root mpfr_add_ui( b, b, 1, MPFR_RNDU ); // instead of multiplying by 2 return smallest; }
static void test_set_overflow (void) { mpfr_t x, inf, max; mpfr_ptr r[MPFR_RND_MAX]; int t[MPFR_RND_MAX] = { 1, -1, 1, -1, 1 }; /* RNDN, RNDZ, RNDU, RNDD, RNDA */ int i; int s; mpfr_inits2 (32, x, inf, max, (mpfr_ptr) 0); mpfr_set_inf (inf, 1); mpfr_set_inf (max, 1); mpfr_nextbelow (max); r[0] = r[2] = r[4] = inf; /* RNDN, RNDU, RNDA */ r[1] = r[3] = max; /* RNDZ, RNDD */ for (s = 1; s > 0; s = -1) { for (i = 0; i < MPFR_RND_MAX ; i++) { int j; int inex; j = s < 0 && i > 1 ? 5 - i : i; inex = mpfr_overflow (x, (mpfr_rnd_t) i, s); if (mpfr_cmp (x, r[j]) || inex * t[j] <= 0) { printf ("Error in test_set_overflow, sign = %d," " rnd_mode = %s\n", s, mpfr_print_rnd_mode ((mpfr_rnd_t) i)); printf ("Got\n"); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); printf (", inex = %d\ninstead of\n", inex); mpfr_out_str (stdout, 2, 0, r[j], MPFR_RNDN); printf (", inex = %d\n", t[j]); exit (1); } } mpfr_neg (inf, inf, MPFR_RNDN); mpfr_neg (max, max, MPFR_RNDN); } mpfr_clears (x, inf, max, (mpfr_ptr) 0); }
static void compare_exp2_exp3 (mpfr_prec_t p0, mpfr_prec_t p1) { mpfr_t x, y, z; mpfr_prec_t prec; mpfr_rnd_t rnd; mpfr_init (x); mpfr_init (y); mpfr_init (z); for (prec = p0; prec <= p1; prec ++) { mpfr_set_prec (x, prec); mpfr_set_prec (y, prec); mpfr_set_prec (z, prec); do mpfr_urandomb (x, RANDS); while (MPFR_IS_ZERO (x)); /* 0 is handled by mpfr_exp only */ rnd = RND_RAND (); mpfr_exp_2 (y, x, rnd); mpfr_exp_3 (z, x, rnd); if (mpfr_cmp (y,z)) { printf ("mpfr_exp_2 and mpfr_exp_3 disagree for rnd=%s and\nx=", mpfr_print_rnd_mode (rnd)); mpfr_print_binary (x); puts (""); printf ("mpfr_exp_2 gives "); mpfr_print_binary (y); puts (""); printf ("mpfr_exp_3 gives "); mpfr_print_binary (z); puts (""); exit (1); } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
int mpfr_cmp_d (mpfr_srcptr b, double d) { mpfr_t tmp; int res; mp_limb_t tmp_man[MPFR_LIMBS_PER_DOUBLE]; MPFR_SAVE_EXPO_DECL (expo); MPFR_SAVE_EXPO_MARK (expo); MPFR_TMP_INIT1(tmp_man, tmp, IEEE_DBL_MANT_DIG); res = mpfr_set_d (tmp, d, MPFR_RNDN); MPFR_ASSERTD (res == 0); MPFR_CLEAR_FLAGS (); res = mpfr_cmp (b, tmp); MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags); MPFR_SAVE_EXPO_FREE (expo); return res; }
void check2a (double x, int px, double y, int py, int pz, mp_rnd_t rnd_mode, char *res) { mpfr_t xx, yy, zz; mpfr_init2(xx,px); mpfr_init2(yy,py); mpfr_init2(zz,pz); mpfr_set_d(xx, x, rnd_mode); mpfr_set_d(yy, y, rnd_mode); mpfr_add(zz, xx, yy, rnd_mode); mpfr_set_prec(xx, pz); mpfr_set_str(xx, res, 16, GMP_RNDN); if (mpfr_cmp(xx, zz)) { printf("x=%1.20e,%d y=%1.20e,%d pz=%d,rnd=%s\n", x,px,y,py,pz,mpfr_print_rnd_mode(rnd_mode)); printf("got "); mpfr_print_binary(zz); putchar('\n'); printf("instead of "); mpfr_print_binary(xx); putchar('\n'); exit(1); } mpfr_clear(xx); mpfr_clear(yy); mpfr_clear(zz); }
void mpfr_taylor_sin_bounded(mpfr_t R, mpfr_t x, unsigned int N) { mpfr_printf("%.20RNF\n", x); assert(mpfr_cmp_ui(x, 0) >= 0 && mpfr_cmp(x, MPFR_HALF_PI) <= 0); mpfr_t t, x_2; mpfr_init_set(R, x, MPFR_RNDN); mpfr_init_set(t, x, MPFR_RNDN); mpfr_init(x_2); mpfr_mul(x_2, x, x, MPFR_RNDN); for(int n = 1; n < N; n++) { mpfr_div_ui(t, t, (2*n+1)*(2*(n++)), MPFR_RNDN); mpfr_mul(t, t, x_2, MPFR_RNDN); mpfr_sub(R, R, t, MPFR_RNDN); mpfr_div_ui(t, t, (2*n+1)*(2*n), MPFR_RNDN); mpfr_mul(t, t, x_2, MPFR_RNDN); mpfr_add(R, R, t, MPFR_RNDN); } }
int mpfr_add1sp (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode) { mpfr_t tmpa, tmpb, tmpc; int inexb, inexc, inexact, inexact2; mpfr_init2 (tmpa, MPFR_PREC (a)); mpfr_init2 (tmpb, MPFR_PREC (b)); mpfr_init2 (tmpc, MPFR_PREC (c)); inexb = mpfr_set (tmpb, b, MPFR_RNDN); MPFR_ASSERTN (inexb == 0); inexc = mpfr_set (tmpc, c, MPFR_RNDN); MPFR_ASSERTN (inexc == 0); inexact2 = mpfr_add1 (tmpa, tmpb, tmpc, rnd_mode); inexact = mpfr_add1sp2 (a, b, c, rnd_mode); if (mpfr_cmp (tmpa, a) || inexact != inexact2) { fprintf (stderr, "add1 & add1sp return different values for %s\n" "Prec_a = %lu, Prec_b = %lu, Prec_c = %lu\nB = ", mpfr_print_rnd_mode (rnd_mode), (unsigned long) MPFR_PREC (a), (unsigned long) MPFR_PREC (b), (unsigned long) MPFR_PREC (c)); mpfr_fprint_binary (stderr, tmpb); fprintf (stderr, "\nC = "); mpfr_fprint_binary (stderr, tmpc); fprintf (stderr, "\n\nadd1 : "); mpfr_fprint_binary (stderr, tmpa); fprintf (stderr, "\nadd1sp: "); mpfr_fprint_binary (stderr, a); fprintf (stderr, "\nInexact sp = %d | Inexact = %d\n", inexact, inexact2); MPFR_ASSERTN (0); } mpfr_clears (tmpa, tmpb, tmpc, (mpfr_ptr) 0); return inexact; }
static void check_large (void) { mpfr_t x, y; mpfr_init2 (x, 25000); mpfr_init2 (y, 26000); (mpfr_const_log2) (x, MPFR_RNDN); /* First one ! */ (mpfr_const_log2) (y, MPFR_RNDN); /* Then the other - cache - */ mpfr_prec_round (y, 25000, MPFR_RNDN); if (mpfr_cmp (x, y)) { printf ("const_log2: error for large prec\n"); exit (1); } /* worst-case with 15 successive ones after last bit, to exercise can_round loop */ mpfr_set_prec (x, 26249); mpfr_const_log2 (x, MPFR_RNDZ); mpfr_clears (x, y, (mpfr_ptr) 0); }
static void bug_ddefour(void) { mpfr_t ex, ex1, ex2, ex3, tot, tot1; mpfr_init2(ex, 53); mpfr_init2(ex1, 53); mpfr_init2(ex2, 53); mpfr_init2(ex3, 53); mpfr_init2(tot, 150); mpfr_init2(tot1, 150); mpfr_set_ui( ex, 1, MPFR_RNDN); mpfr_mul_2exp( ex, ex, 906, MPFR_RNDN); mpfr_log( tot, ex, MPFR_RNDN); mpfr_set( ex1, tot, MPFR_RNDN); /* ex1 = high(tot) */ test_sub( ex2, tot, ex1, MPFR_RNDN); /* ex2 = high(tot - ex1) */ test_sub( tot1, tot, ex1, MPFR_RNDN); /* tot1 = tot - ex1 */ mpfr_set( ex3, tot1, MPFR_RNDN); /* ex3 = high(tot - ex1) */ if (mpfr_cmp(ex2, ex3)) { printf ("Error in ddefour test.\n"); printf ("ex2="); mpfr_print_binary (ex2); puts (""); printf ("ex3="); mpfr_print_binary (ex3); puts (""); exit (1); } mpfr_clear (ex); mpfr_clear (ex1); mpfr_clear (ex2); mpfr_clear (ex3); mpfr_clear (tot); mpfr_clear (tot1); }
void check (mp_prec_t p0, mp_prec_t p1) { mpfr_t x, y, z; mp_rnd_t rnd; mpfr_init (x); mpfr_init (y); mpfr_init2 (z, p1 + 10); mpfr_const_log2 (z, GMP_RNDN); __mpfr_const_log2_prec = 1; for (; p0<=p1; p0++) { mpfr_set_prec (x, p0); mpfr_set_prec (y, p0); for (rnd = 0; rnd < 4; rnd++) { mpfr_const_log2 (x, rnd); mpfr_set (y, z, rnd); if (mpfr_cmp (x, y) && mpfr_can_round (z, mpfr_get_prec(z), GMP_RNDN, rnd, p0)) { fprintf (stderr, "mpfr_const_log2 fails for prec=%u, rnd=%s\n", (unsigned int) p0, mpfr_print_rnd_mode (rnd)); fprintf (stderr, "expected "); mpfr_out_str (stderr, 2, 0, y, GMP_RNDN); fprintf (stderr, "\ngot "); mpfr_out_str (stderr, 2, 0, x, GMP_RNDN); fprintf (stderr, "\n"); exit (1); } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
static void check (mp_prec_t p0, mp_prec_t p1) { mpfr_t x, y, z; mp_rnd_t rnd; mpfr_init (x); mpfr_init (y); mpfr_init2 (z, p1 + 10); mpfr_const_log2 (z, GMP_RNDN); mpfr_clear_cache (__gmpfr_cache_const_log2); for (; p0<=p1; p0++) { mpfr_set_prec (x, p0); mpfr_set_prec (y, p0); { rnd = (mp_rnd_t) RND_RAND (); mpfr_const_log2 (x, rnd); mpfr_set (y, z, rnd); if (mpfr_cmp (x, y) && mpfr_can_round (z, mpfr_get_prec(z), GMP_RNDN, rnd, p0)) { printf ("mpfr_const_log2 fails for prec=%u, rnd=%s\n", (unsigned int) p0, mpfr_print_rnd_mode (rnd)); printf ("expected "); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf ("\ngot "); mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); printf ("\n"); exit (1); } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
void check_pow_ui (void) { mpfr_t a, b; mpfr_init2 (a, 53); mpfr_init2 (b, 53); /* check in-place operations */ mpfr_set_d (b, 0.6926773, GMP_RNDN); mpfr_pow_ui (a, b, 10, GMP_RNDN); mpfr_pow_ui (b, b, 10, GMP_RNDN); if (mpfr_cmp (a, b)) { fprintf (stderr, "Error for mpfr_pow_ui (b, b, ...)\n"); exit (1); } /* check large exponents */ mpfr_set_d (b, 1, GMP_RNDN); mpfr_pow_ui (a, b, 4294967295UL, GMP_RNDN); mpfr_set_inf (a, -1); mpfr_pow_ui (a, a, 4049053855UL, GMP_RNDN); if (!mpfr_inf_p (a) || (mpfr_sgn (a) >= 0)) { fprintf (stderr, "Error for (-Inf)^4049053855\n"); exit (1); } mpfr_set_inf (a, -1); mpfr_pow_ui (a, a, (unsigned long) 30002752, GMP_RNDN); if (!mpfr_inf_p (a) || (mpfr_sgn (a) <= 0)) { fprintf (stderr, "Error for (-Inf)^30002752\n"); exit (1); } mpfr_clear (a); mpfr_clear (b); }
static void check (mpfr_prec_t p0, mpfr_prec_t p1) { mpfr_t x, y, z; mpfr_rnd_t rnd; int dif; mpfr_init (x); mpfr_init (y); mpfr_init2 (z, p1 + 10); mpfr_const_log2 (z, MPFR_RNDN); mpfr_clear_cache (__gmpfr_cache_const_log2); for (; p0<=p1; p0++) { mpfr_set_prec (x, p0); mpfr_set_prec (y, p0); { rnd = RND_RAND (); mpfr_const_log2 (x, rnd); mpfr_set (y, z, rnd); if ((dif = mpfr_cmp (x, y)) && mpfr_can_round (z, mpfr_get_prec(z), MPFR_RNDN, rnd, p0)) { printf ("mpfr_const_log2 fails for prec=%u, rnd=%s Diff=%d\n", (unsigned int) p0, mpfr_print_rnd_mode (rnd), dif); printf ("expected "), mpfr_dump (y); printf ("got "), mpfr_dump (x); exit (1); } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
static int phase1() { int i, j; mpfr_t u; mpfr_zinit(u); jmax = n3; for (i = 0; i <= m; i++) { if (col[i] > n2) mpfr_set_d(q[0][i], -1, GMP_RNDN); } minimize(); tableau(u, 0, 0); if (mpfr_cmp(u, minuseps) < 0) { mpfr_clear(u); return 0; } for (i = 1; i <= m; i++) { if (col[i] > n2) { col[i] = -1; } } mpfr_set_d(q[0][0], 1, GMP_RNDN); for (j = 1; j <= m; j++) mpfr_set_d(q[0][j], 0, GMP_RNDN); for (i = 1; i <= m; i++) { if ((j = col[i]) > 0 && j <= n && mpfr_cmp_d(c[j], 0) != 0) { mpfr_set(u, c[j], GMP_RNDN); for (j = 1; j <= m; j++) { mpfr_fms(q[0][j], q[i][j], u, q[0][j], GMP_RNDN); mpfr_neg(q[0][j], q[0][j], GMP_RNDN); } } } mpfr_clear(u); return 1; }
/* expx is the value of exp(X) rounded towards -infinity */ static void check_worst_case (const char *Xs, const char *expxs) { mpfr_t x, y; mpfr_inits2(53, x, y, NULL); mpfr_set_str1(x, Xs); mpfr_exp(y, x, GMP_RNDD); if (mpfr_cmp_str1 (y, expxs)) { printf ("exp(x) rounded towards -infinity is wrong\n"); exit(1); } mpfr_set_str1(x, Xs); mpfr_exp(x, x, GMP_RNDU); mpfr_add_one_ulp(y, GMP_RNDN); if (mpfr_cmp(x,y)) { printf ("exp(x) rounded towards +infinity is wrong\n"); exit(1); } mpfr_clears(x,y,NULL); }
/* expx is the value of exp(X) rounded toward -infinity */ static void check_worst_case (const char *Xs, const char *expxs) { mpfr_t x, y; mpfr_inits2 (53, x, y, (mpfr_ptr) 0); mpfr_set_str1(x, Xs); test_exp(y, x, MPFR_RNDD); if (mpfr_cmp_str1 (y, expxs)) { printf ("exp(x) rounded toward -infinity is wrong\n"); exit(1); } mpfr_set_str1(x, Xs); test_exp(x, x, MPFR_RNDU); mpfr_nexttoinf (y); if (mpfr_cmp(x,y)) { printf ("exp(x) rounded toward +infinity is wrong\n"); exit(1); } mpfr_clears (x, y, (mpfr_ptr) 0); }
/* check sqrt(x^2) = x */ static void test_property2 (mpfr_prec_t p, mpfr_rnd_t r) { mpfr_t x, y; mpfr_init2 (x, p); mpfr_init2 (y, p); mpfr_urandomb (x, RANDS); mpfr_mul (y, x, x, r); mpfr_sqrt (y, y, r); if (mpfr_cmp (y, x)) { printf ("Error, sqrt(x^2) = x does not hold for r=%s\n", mpfr_print_rnd_mode (r)); printf ("x="); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } mpfr_clear (x); mpfr_clear (y); }
/* check that 1.23e is correctly parsed, cf http://gmplib.org/list-archives/gmp-bugs/2010-March/001898.html */ static void test20100310 (void) { mpfr_t x, y; char str[] = "1.23e", *endptr; mpfr_init2 (x, 53); mpfr_init2 (y, 53); mpfr_strtofr (x, str, &endptr, 10, MPFR_RNDN); mpfr_strtofr (y, "1.23", NULL, 10, MPFR_RNDN); if (mpfr_cmp (x, y) != 0) { printf ("x <> y in test20100310\n"); exit (1); } if (endptr != str + 4) /* strtofr should take into account '1.23', not '1.23e' */ { printf ("endptr <> str + 4 in test20100310\n"); exit (1); } mpfr_clear (x); mpfr_clear (y); }
int mpfr_mul (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode) { mpfr_t ta, tb, tc; int inexact1, inexact2; mpfr_init2 (ta, MPFR_PREC (a)); mpfr_init2 (tb, MPFR_PREC (b)); mpfr_init2 (tc, MPFR_PREC (c)); MPFR_ASSERTN (mpfr_set (tb, b, MPFR_RNDN) == 0); MPFR_ASSERTN (mpfr_set (tc, c, MPFR_RNDN) == 0); inexact2 = mpfr_mul3 (ta, tb, tc, rnd_mode); inexact1 = mpfr_mul2 (a, b, c, rnd_mode); if (mpfr_cmp (ta, a) || inexact1*inexact2 < 0 || (inexact1*inexact2 == 0 && (inexact1|inexact2) != 0)) { fprintf (stderr, "mpfr_mul return different values for %s\n" "Prec_a = %lu, Prec_b = %lu, Prec_c = %lu\nB = ", mpfr_print_rnd_mode (rnd_mode), MPFR_PREC (a), MPFR_PREC (b), MPFR_PREC (c)); mpfr_out_str (stderr, 16, 0, tb, MPFR_RNDN); fprintf (stderr, "\nC = "); mpfr_out_str (stderr, 16, 0, tc, MPFR_RNDN); fprintf (stderr, "\nOldMul: "); mpfr_out_str (stderr, 16, 0, ta, MPFR_RNDN); fprintf (stderr, "\nNewMul: "); mpfr_out_str (stderr, 16, 0, a, MPFR_RNDN); fprintf (stderr, "\nNewInexact = %d | OldInexact = %d\n", inexact1, inexact2); MPFR_ASSERTN(0); } mpfr_clears (ta, tb, tc, (mpfr_ptr) 0); return inexact1; }
int mpfi_interv_fr (mpfi_ptr a, mpfr_srcptr b, mpfr_srcptr c) { int inexact_left, inexact_right, inexact=0; if ( mpfr_nan_p (b) || mpfr_nan_p (c) ) { mpfr_set_nan (&(a->left)); mpfr_set_nan (&(a->right)); MPFR_RET_NAN; } if ( mpfr_cmp (b, c) <= 0 ) { inexact_left = mpfr_set (&(a->left), b, MPFI_RNDD); inexact_right = mpfr_set (&(a->right), c, MPFI_RNDU); } else { inexact_left = mpfr_set (&(a->left), c, MPFI_RNDD); inexact_right = mpfr_set (&(a->right), b, MPFI_RNDU); } /* a cannot be a NaN, it has been tested before */ if (inexact_left) inexact += 1; if (inexact_right) inexact += 2; /* do not allow -0 as lower bound */ if (mpfr_zero_p (&(a->left)) && mpfr_signbit (&(a->left))) { mpfr_neg (&(a->left), &(a->left), MPFI_RNDU); } /* do not allow +0 as upper bound */ if (mpfr_zero_p (&(a->right)) && !mpfr_signbit (&(a->right))) { mpfr_neg (&(a->right), &(a->right), MPFI_RNDD); } return inexact; }
static void check_ternary_value (void) { int p, q, rnd; int inexact, cmp; mpfr_t x, y; mpfr_init (x); mpfr_init (y); for (p=2; p<500; p++) { mpfr_set_prec (x, p); mpfr_urandomb (x, RANDS); if (randlimb () % 2) mpfr_neg (x, x, MPFR_RNDN); for (q=2; q<2*p; q++) { mpfr_set_prec (y, q); for (rnd = 0; rnd < MPFR_RND_MAX; rnd++) { inexact = mpfr_set (y, x, (mpfr_rnd_t) rnd); cmp = mpfr_cmp (y, x); if (((inexact == 0) && (cmp != 0)) || ((inexact > 0) && (cmp <= 0)) || ((inexact < 0) && (cmp >= 0))) { printf ("Wrong ternary value in mpfr_set: expected %d," " got %d\n", cmp, inexact); exit (1); } } } } mpfr_clear (x); mpfr_clear (y); }