int main (int argc, char *argv[]) { mpfr_t x; mpfr_prec_t p; mpfr_rnd_t rnd; tests_start_mpfr (); p = 53; if (argc > 1) { long a = atol (argv[1]); if (MPFR_PREC_COND (a)) p = a; } rnd = (argc > 2) ? (mpfr_rnd_t) atoi(argv[2]) : MPFR_RNDZ; mpfr_init2 (x, p); mpfr_const_pi (x, rnd); if (argc >= 2) { if (argc < 4 || atoi (argv[3]) != 0) { printf ("Pi="); mpfr_out_str (stdout, 10, 0, x, rnd); puts (""); } } else if (mpfr_cmp_str1 (x, "3.141592653589793116") ) { printf ("mpfr_const_pi failed for prec=53\n"); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar('\n'); exit (1); } mpfr_set_prec (x, 32); mpfr_const_pi (x, MPFR_RNDN); if (mpfr_cmp_str1 (x, "3.141592653468251") ) { printf ("mpfr_const_pi failed for prec=32\n"); exit (1); } mpfr_clear (x); bug20091030 (); check_large (); test_generic (MPFR_PREC_MIN, 200, 1); RUN_PTHREAD_TEST(); tests_end_mpfr (); return 0; }
MPFR_HOT_FUNCTION_ATTR void mpfr_set_prec (mpfr_ptr x, mpfr_prec_t p) { mp_size_t xsize, xoldsize; mpfr_limb_ptr tmp; /* first, check if p is correct */ MPFR_ASSERTN (MPFR_PREC_COND (p)); /* Calculate the new number of limbs */ xsize = MPFR_PREC2LIMBS (p); /* Realloc only if the new size is greater than the old */ xoldsize = MPFR_GET_ALLOC_SIZE (x); if (MPFR_UNLIKELY (xsize > xoldsize)) { tmp = (mpfr_limb_ptr) (*__gmp_reallocate_func) (MPFR_GET_REAL_PTR(x), MPFR_MALLOC_SIZE(xoldsize), MPFR_MALLOC_SIZE(xsize)); MPFR_SET_MANT_PTR(x, tmp); MPFR_SET_ALLOC_SIZE(x, xsize); } MPFR_PREC (x) = p; MPFR_SET_NAN (x); /* initializes to NaN */ }
void mpfr_set_default_prec (mpfr_prec_t prec) { MPFR_ASSERTN (MPFR_PREC_COND (prec)); __gmpfr_default_fp_bit_precision = prec; }
/* Check data in file f for function foo, with name 'name'. Each line consists of the file f one: xprec yprec rnd x y where: xprec is the input precision yprec is the output precision rnd is the rounding mode (n, z, u, d, a, Z, *) x is the input (hexadecimal format) y is the expected output (hexadecimal format) for foo(x) with rounding rnd If rnd is Z, y is the expected output in round-toward-zero, and the four directed rounding modes are tested, then the round-to-nearest mode is tested in precision yprec-1. This is useful for worst cases, where yprec is the minimum value such that one has a worst case in a directed rounding mode. If rnd is *, y must be an exact case. All the rounding modes are tested and the ternary value is checked (it must be 0). */ void data_check (const char *f, int (*foo) (FLIST), const char *name) { FILE *fp; long int xprec, yprec; /* not mpfr_prec_t because of the fscanf */ mpfr_t x, y, z; mpfr_rnd_t rnd; char r; int c; fp = fopen (f, "r"); if (fp == NULL) fp = src_fopen (f, "r"); if (fp == NULL) { char *v = (char *) MPFR_VERSION_STRING; /* In the '-dev' versions, assume that the data file exists and return an error if the file cannot be opened to make sure that such failures are detected. */ while (*v != '\0') v++; if (v[-4] == '-' && v[-3] == 'd' && v[-2] == 'e' && v[-1] == 'v') { printf ("Error: unable to open file '%s'\n", f); exit (1); } else return; } mpfr_init (x); mpfr_init (y); mpfr_init (z); while (!feof (fp)) { /* skip whitespace, for consistency */ if (fscanf (fp, " ") == EOF) { if (ferror (fp)) { perror ("data_check"); exit (1); } else break; /* end of file */ } if ((c = getc (fp)) == EOF) { if (ferror (fp)) { perror ("data_check"); exit (1); } else break; /* end of file */ } if (c == '#') /* comment: read entire line */ { do { c = getc (fp); } while (!feof (fp) && c != '\n'); } else { ungetc (c, fp); c = fscanf (fp, "%ld %ld %c", &xprec, &yprec, &r); MPFR_ASSERTN (MPFR_PREC_COND (xprec)); MPFR_ASSERTN (MPFR_PREC_COND (yprec)); if (c == EOF) { perror ("data_check"); exit (1); } else if (c != 3) { printf ("Error: corrupted line in file '%s'\n", f); exit (1); } switch (r) { case 'n': rnd = MPFR_RNDN; break; case 'z': case 'Z': rnd = MPFR_RNDZ; break; case 'u': rnd = MPFR_RNDU; break; case 'd': rnd = MPFR_RNDD; break; case '*': rnd = MPFR_RND_MAX; /* non-existing rounding mode */ break; default: printf ("Error: unexpected rounding mode" " in file '%s': %c\n", f, (int) r); exit (1); } mpfr_set_prec (x, xprec); mpfr_set_prec (y, yprec); if (mpfr_inp_str (x, fp, 0, MPFR_RNDN) == 0) { printf ("Error: corrupted argument in file '%s'\n", f); exit (1); } if (mpfr_inp_str (y, fp, 0, MPFR_RNDN) == 0) { printf ("Error: corrupted result in file '%s'\n", f); exit (1); } if (getc (fp) != '\n') { printf ("Error: result not followed by \\n in file '%s'\n", f); exit (1); } /* Skip whitespace, in particular at the end of the file. */ if (fscanf (fp, " ") == EOF && ferror (fp)) { perror ("data_check"); exit (1); } if (r == '*') { int rndint; RND_LOOP (rndint) test5rm (foo, x, y, z, (mpfr_rnd_t) rndint, 2, name); } else test5rm (foo, x, y, z, rnd, r != 'Z', name); } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); fclose (fp); }