1 /* Test file for mpfr_grandom
3 Copyright 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"
29 test_special (mpfr_prec_t p)
36 inexact = mpfr_grandom (x, NULL, RANDS, MPFR_RNDN);
37 if ((inexact & 3) == 0)
39 printf ("Error: mpfr_grandom() returns a zero ternary value.\n");
42 if ((inexact & (3 << 2)) != 0)
44 printf ("Error: the second ternary value of mpfr_grandom(x, NULL, ...)"
54 test_grandom (long nbtests, mpfr_prec_t prec, mpfr_rnd_t rnd,
61 nbtests = (nbtests & 1) ? (nbtests + 1) : nbtests;
62 t = (mpfr_t *) malloc (nbtests * sizeof (mpfr_t));
65 fprintf (stderr, "tgrandom: can't allocate memory in test_grandom\n");
69 for (i = 0; i < nbtests; ++i)
70 mpfr_init2 (t[i], prec);
72 for (i = 0; i < nbtests; i += 2)
74 inexact = mpfr_grandom (t[i], t[i + 1], RANDS, MPFR_RNDN);
75 if ((inexact & 3) == 0 || (inexact & (3 << 2)) == 0)
77 /* one call in the loop pretended to return an exact number! */
78 printf ("Error: mpfr_grandom() returns a zero ternary value.\n");
86 mpfr_init2 (av, prec);
87 mpfr_init2 (va, prec);
88 mpfr_init2 (tmp, prec);
90 mpfr_set_ui (av, 0, MPFR_RNDN);
91 mpfr_set_ui (va, 0, MPFR_RNDN);
92 for (i = 0; i < nbtests; ++i)
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);
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);
103 mpfr_printf ("Average = %.5Rf\nVariance = %.5Rf\n", av, va);
108 #endif /* HAVE_STDARG */
110 for (i = 0; i < nbtests; ++i)
118 main (int argc, char *argv[])
128 long a = atol (argv[1]);
134 test_grandom (nbtests, 420, MPFR_RNDN, verbose);
136 test_special (42000);