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