1 /* Test file for mpfr_grandom 2 3 Copyright 2011, 2012, 2013 Free Software Foundation, Inc. 4 Contributed by the AriC and Caramel 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 static void 29 test_special (mpfr_prec_t p) 30 { 31 mpfr_t x; 32 int inexact; 33 34 mpfr_init2 (x, p); 35 36 inexact = mpfr_grandom (x, NULL, RANDS, MPFR_RNDN); 37 if ((inexact & 3) == 0) 38 { 39 printf ("Error: mpfr_grandom() returns a zero ternary value.\n"); 40 exit (1); 41 } 42 if ((inexact & (3 << 2)) != 0) 43 { 44 printf ("Error: the second ternary value of mpfr_grandom(x, NULL, ...)" 45 " must be 0.\n"); 46 exit (1); 47 } 48 49 mpfr_clear(x); 50 } 51 52 53 static void 54 test_grandom (long nbtests, mpfr_prec_t prec, mpfr_rnd_t rnd, 55 int verbose) 56 { 57 mpfr_t *t; 58 mpfr_t av, va, tmp; 59 int i, inexact; 60 61 nbtests = (nbtests & 1) ? (nbtests + 1) : nbtests; 62 t = (mpfr_t *) malloc (nbtests * sizeof (mpfr_t)); 63 if (t == NULL) 64 { 65 fprintf (stderr, "tgrandom: can't allocate memory in test_grandom\n"); 66 exit (1); 67 } 68 69 for (i = 0; i < nbtests; ++i) 70 mpfr_init2 (t[i], prec); 71 72 for (i = 0; i < nbtests; i += 2) 73 { 74 inexact = mpfr_grandom (t[i], t[i + 1], RANDS, MPFR_RNDN); 75 if ((inexact & 3) == 0 || (inexact & (3 << 2)) == 0) 76 { 77 /* one call in the loop pretended to return an exact number! */ 78 printf ("Error: mpfr_grandom() returns a zero ternary value.\n"); 79 exit (1); 80 } 81 } 82 83 #ifdef HAVE_STDARG 84 if (verbose) 85 { 86 mpfr_init2 (av, prec); 87 mpfr_init2 (va, prec); 88 mpfr_init2 (tmp, prec); 89 90 mpfr_set_ui (av, 0, MPFR_RNDN); 91 mpfr_set_ui (va, 0, MPFR_RNDN); 92 for (i = 0; i < nbtests; ++i) 93 { 94 mpfr_add (av, av, t[i], MPFR_RNDN); 95 mpfr_sqr (tmp, t[i], MPFR_RNDN); 96 mpfr_add (va, va, tmp, MPFR_RNDN); 97 } 98 mpfr_div_ui (av, av, nbtests, MPFR_RNDN); 99 mpfr_div_ui (va, va, nbtests, MPFR_RNDN); 100 mpfr_sqr (tmp, av, MPFR_RNDN); 101 mpfr_sub (va, va, av, MPFR_RNDN); 102 103 mpfr_printf ("Average = %.5Rf\nVariance = %.5Rf\n", av, va); 104 mpfr_clear (av); 105 mpfr_clear (va); 106 mpfr_clear (tmp); 107 } 108 #endif /* HAVE_STDARG */ 109 110 for (i = 0; i < nbtests; ++i) 111 mpfr_clear (t[i]); 112 free (t); 113 return; 114 } 115 116 117 int 118 main (int argc, char *argv[]) 119 { 120 long nbtests; 121 int verbose; 122 tests_start_mpfr (); 123 124 verbose = 0; 125 nbtests = 10; 126 if (argc > 1) 127 { 128 long a = atol (argv[1]); 129 verbose = 1; 130 if (a != 0) 131 nbtests = a; 132 } 133 134 test_grandom (nbtests, 420, MPFR_RNDN, verbose); 135 test_special (2); 136 test_special (42000); 137 138 tests_end_mpfr (); 139 return 0; 140 } 141