1/* Test file for mpfr_const_pi. 2 3Copyright 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4Contributed by the Arenaire and Cacao projects, INRIA. 5 6This file is part of the GNU MPFR Library. 7 8The GNU MPFR Library is free software; you can redistribute it and/or modify 9it under the terms of the GNU Lesser General Public License as published by 10the Free Software Foundation; either version 3 of the License, or (at your 11option) any later version. 12 13The GNU MPFR Library is distributed in the hope that it will be useful, but 14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16License for more details. 17 18You should have received a copy of the GNU Lesser General Public License 19along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 20http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23#include <stdio.h> 24#include <stdlib.h> 25 26#include "mpfr-test.h" 27 28/* tconst_pi [prec] [rnd] [0 = no print] */ 29 30static void 31check_large (void) 32{ 33 mpfr_t x, y, z; 34 35 mpfr_init2 (x, 20000); 36 mpfr_init2 (y, 21000); 37 mpfr_init2 (z, 11791); 38 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); 46 exit (1); 47 } 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); 51 exit (1); 52 } 53 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); 58 } 59 60 mpfr_clears (x, y, z, (mpfr_ptr) 0); 61} 62 63/* Wrapper for tgeneric */ 64static int 65my_const_pi (mpfr_ptr x, mpfr_srcptr y, mpfr_rnd_t r) 66{ 67 return mpfr_const_pi (x, r); 68} 69 70#define RAND_FUNCTION(x) mpfr_set_ui ((x), 0, MPFR_RNDN) 71#define TEST_FUNCTION my_const_pi 72#include "tgeneric.c" 73 74static void 75bug20091030 (void) 76{ 77 mpfr_t x, x_ref; 78 int inex, inex_ref; 79 mpfr_prec_t p; 80 int r; 81 82 mpfr_free_cache (); 83 mpfr_init2 (x, MPFR_PREC_MIN); 84 for (p = MPFR_PREC_MIN; p <= 100; p++) 85 { 86 mpfr_set_prec (x, p); 87 inex = mpfr_const_pi (x, MPFR_RNDU); 88 if (inex < 0) 89 { 90 printf ("Error, inex < 0 for RNDU (prec=%lu)\n", p); 91 exit (1); 92 } 93 inex = mpfr_const_pi (x, MPFR_RNDD); 94 if (inex > 0) 95 { 96 printf ("Error, inex > 0 for RNDD (prec=%lu)\n", p); 97 exit (1); 98 } 99 } 100 mpfr_free_cache (); 101 mpfr_init2 (x_ref, MPFR_PREC_MIN); 102 for (p = MPFR_PREC_MIN; p <= 100; p++) 103 { 104 mpfr_set_prec (x, p + 10); 105 mpfr_const_pi (x, MPFR_RNDN); 106 mpfr_set_prec (x, p); 107 mpfr_set_prec (x_ref, p); 108 for (r = 0; r < MPFR_RND_MAX; r++) 109 { 110 inex = mpfr_const_pi (x, (mpfr_rnd_t) r); 111 inex_ref = mpfr_const_pi_internal (x_ref, (mpfr_rnd_t) r); 112 if (inex != inex_ref || mpfr_cmp (x, x_ref) != 0) 113 { 114 printf ("mpfr_const_pi and mpfr_const_pi_internal disagree\n"); 115 printf ("mpfr_const_pi gives "); 116 mpfr_dump (x); 117 printf ("mpfr_const_pi_internal gives "); 118 mpfr_dump (x_ref); 119 printf ("inex=%d inex_ref=%d\n", inex, inex_ref); 120 exit (1); 121 } 122 } 123 } 124 mpfr_clear (x); 125 mpfr_clear (x_ref); 126} 127 128int 129main (int argc, char *argv[]) 130{ 131 mpfr_t x; 132 mpfr_prec_t p; 133 mpfr_rnd_t rnd; 134 135 tests_start_mpfr (); 136 137 p = 53; 138 if (argc > 1) 139 { 140 long a = atol (argv[1]); 141 if (a >= MPFR_PREC_MIN && a <= MPFR_PREC_MAX) 142 p = a; 143 } 144 145 rnd = (argc > 2) ? (mpfr_rnd_t) atoi(argv[2]) : MPFR_RNDZ; 146 147 mpfr_init2 (x, p); 148 mpfr_const_pi (x, rnd); 149 if (argc >= 2) 150 { 151 if (argc < 4 || atoi (argv[3]) != 0) 152 { 153 printf ("Pi="); 154 mpfr_out_str (stdout, 10, 0, x, rnd); 155 puts (""); 156 } 157 } 158 else if (mpfr_cmp_str1 (x, "3.141592653589793116") ) 159 { 160 printf ("mpfr_const_pi failed for prec=53\n"); 161 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar('\n'); 162 exit (1); 163 } 164 165 mpfr_set_prec (x, 32); 166 mpfr_const_pi (x, MPFR_RNDN); 167 if (mpfr_cmp_str1 (x, "3.141592653468251") ) 168 { 169 printf ("mpfr_const_pi failed for prec=32\n"); 170 exit (1); 171 } 172 173 mpfr_clear (x); 174 175 bug20091030 (); 176 177 check_large (); 178 179 test_generic (2, 200, 1); 180 181 tests_end_mpfr (); 182 183 return 0; 184} 185