1 /* Test file for mpfr_const_pi.
3 Copyright 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
26 #include "mpfr-test.h"
28 /* tconst_pi [prec] [rnd] [0 = no print] */
35 mpfr_init2 (x, 20000);
36 mpfr_init2 (y, 21000);
37 mpfr_init2 (z, 11791);
39 /* The algo failed to round for p=11791. */
40 (mpfr_const_pi) (z, MPFR_RNDU);
41 mpfr_const_pi (x, MPFR_RNDN); /* First one ! */
42 mpfr_const_pi (y, MPFR_RNDN); /* Then the other - cache - */
43 mpfr_prec_round (y, 20000, MPFR_RNDN);
44 if (mpfr_cmp (x, y)) {
45 printf ("const_pi: error for large prec (%d)\n", 1);
48 mpfr_prec_round (y, 11791, MPFR_RNDU);
49 if (mpfr_cmp (z, y)) {
50 printf ("const_pi: error for large prec (%d)\n", 2);
54 /* a worst-case to exercise recomputation */
55 if (MPFR_PREC_MAX > 33440) {
56 mpfr_set_prec (x, 33440);
57 mpfr_const_pi (x, MPFR_RNDZ);
60 mpfr_clears (x, y, z, (mpfr_ptr) 0);
63 /* Wrapper for tgeneric */
65 my_const_pi (mpfr_ptr x, mpfr_srcptr y, mpfr_rnd_t r)
67 return mpfr_const_pi (x, r);
70 #define RAND_FUNCTION(x) mpfr_set_ui ((x), 0, MPFR_RNDN)
71 #define TEST_FUNCTION my_const_pi
83 mpfr_init2 (x, MPFR_PREC_MIN);
84 for (p = MPFR_PREC_MIN; p <= 100; p++)
87 inex = mpfr_const_pi (x, MPFR_RNDU);
90 printf ("Error, inex < 0 for RNDU (prec=%lu)\n",
94 inex = mpfr_const_pi (x, MPFR_RNDD);
97 printf ("Error, inex > 0 for RNDD (prec=%lu)\n",
103 mpfr_init2 (x_ref, MPFR_PREC_MIN);
104 for (p = MPFR_PREC_MIN; p <= 100; p++)
106 mpfr_set_prec (x, p + 10);
107 mpfr_const_pi (x, MPFR_RNDN);
108 mpfr_set_prec (x, p);
109 mpfr_set_prec (x_ref, p);
110 for (r = 0; r < MPFR_RND_MAX; r++)
112 inex = mpfr_const_pi (x, (mpfr_rnd_t) r);
113 inex_ref = mpfr_const_pi_internal (x_ref, (mpfr_rnd_t) r);
114 if (inex != inex_ref || mpfr_cmp (x, x_ref) != 0)
116 printf ("mpfr_const_pi and mpfr_const_pi_internal disagree\n");
117 printf ("mpfr_const_pi gives ");
119 printf ("mpfr_const_pi_internal gives ");
121 printf ("inex=%d inex_ref=%d\n", inex, inex_ref);
131 main (int argc, char *argv[])
142 long a = atol (argv[1]);
143 if (a >= MPFR_PREC_MIN && a <= MPFR_PREC_MAX)
147 rnd = (argc > 2) ? (mpfr_rnd_t) atoi(argv[2]) : MPFR_RNDZ;
150 mpfr_const_pi (x, rnd);
153 if (argc < 4 || atoi (argv[3]) != 0)
156 mpfr_out_str (stdout, 10, 0, x, rnd);
160 else if (mpfr_cmp_str1 (x, "3.141592653589793116") )
162 printf ("mpfr_const_pi failed for prec=53\n");
163 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar('\n');
167 mpfr_set_prec (x, 32);
168 mpfr_const_pi (x, MPFR_RNDN);
169 if (mpfr_cmp_str1 (x, "3.141592653468251") )
171 printf ("mpfr_const_pi failed for prec=32\n");
181 test_generic (2, 200, 1);