1 /* Test file for mpfr_const_pi. 2 3 Copyright 1999, 2001-2016 Free Software Foundation, Inc. 4 Contributed by the AriC and Caramba projects, INRIA. 5 6 This file is part of the GNU MPFR Library. 7 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. 12 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. 17 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. */ 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 30 static void 31 check_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 */ 64 static int 65 my_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 74 static void 75 bug20091030 (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", 91 (unsigned long) p); 92 exit (1); 93 } 94 inex = mpfr_const_pi (x, MPFR_RNDD); 95 if (inex > 0) 96 { 97 printf ("Error, inex > 0 for RNDD (prec=%lu)\n", 98 (unsigned long) p); 99 exit (1); 100 } 101 } 102 mpfr_free_cache (); 103 mpfr_init2 (x_ref, MPFR_PREC_MIN); 104 for (p = MPFR_PREC_MIN; p <= 100; p++) 105 { 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++) 111 { 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) 115 { 116 printf ("mpfr_const_pi and mpfr_const_pi_internal disagree\n"); 117 printf ("mpfr_const_pi gives "); 118 mpfr_dump (x); 119 printf ("mpfr_const_pi_internal gives "); 120 mpfr_dump (x_ref); 121 printf ("inex=%d inex_ref=%d\n", inex, inex_ref); 122 exit (1); 123 } 124 } 125 } 126 mpfr_clear (x); 127 mpfr_clear (x_ref); 128 } 129 130 int 131 main (int argc, char *argv[]) 132 { 133 mpfr_t x; 134 mpfr_prec_t p; 135 mpfr_rnd_t rnd; 136 137 tests_start_mpfr (); 138 139 p = 53; 140 if (argc > 1) 141 { 142 long a = atol (argv[1]); 143 if (a >= MPFR_PREC_MIN && a <= MPFR_PREC_MAX) 144 p = a; 145 } 146 147 rnd = (argc > 2) ? (mpfr_rnd_t) atoi(argv[2]) : MPFR_RNDZ; 148 149 mpfr_init2 (x, p); 150 mpfr_const_pi (x, rnd); 151 if (argc >= 2) 152 { 153 if (argc < 4 || atoi (argv[3]) != 0) 154 { 155 printf ("Pi="); 156 mpfr_out_str (stdout, 10, 0, x, rnd); 157 puts (""); 158 } 159 } 160 else if (mpfr_cmp_str1 (x, "3.141592653589793116") ) 161 { 162 printf ("mpfr_const_pi failed for prec=53\n"); 163 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar('\n'); 164 exit (1); 165 } 166 167 mpfr_set_prec (x, 32); 168 mpfr_const_pi (x, MPFR_RNDN); 169 if (mpfr_cmp_str1 (x, "3.141592653468251") ) 170 { 171 printf ("mpfr_const_pi failed for prec=32\n"); 172 exit (1); 173 } 174 175 mpfr_clear (x); 176 177 bug20091030 (); 178 179 check_large (); 180 181 test_generic (2, 200, 1); 182 183 tests_end_mpfr (); 184 185 return 0; 186 } 187