xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tgrandom.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /* Test file for mpfr_grandom
2 
3 Copyright 2011-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 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 *) tests_allocate (nbtests * sizeof (mpfr_t));
63 
64   for (i = 0; i < nbtests; ++i)
65     mpfr_init2 (t[i], prec);
66 
67   for (i = 0; i < nbtests; i += 2)
68     {
69       inexact = mpfr_grandom (t[i], t[i + 1], RANDS, MPFR_RNDN);
70       if ((inexact & 3) == 0 || (inexact & (3 << 2)) == 0)
71         {
72           /* one call in the loop pretended to return an exact number! */
73           printf ("Error: mpfr_grandom() returns a zero ternary value.\n");
74           exit (1);
75         }
76     }
77 
78 #ifdef HAVE_STDARG
79   if (verbose)
80     {
81       mpfr_init2 (av, prec);
82       mpfr_init2 (va, prec);
83       mpfr_init2 (tmp, prec);
84 
85       mpfr_set_ui (av, 0, MPFR_RNDN);
86       mpfr_set_ui (va, 0, MPFR_RNDN);
87       for (i = 0; i < nbtests; ++i)
88         {
89           mpfr_add (av, av, t[i], MPFR_RNDN);
90           mpfr_sqr (tmp, t[i], MPFR_RNDN);
91           mpfr_add (va, va, tmp, MPFR_RNDN);
92         }
93       mpfr_div_ui (av, av, nbtests, MPFR_RNDN);
94       mpfr_div_ui (va, va, nbtests, MPFR_RNDN);
95       mpfr_sqr (tmp, av, MPFR_RNDN);
96       mpfr_sub (va, va, av, MPFR_RNDN);
97 
98       mpfr_printf ("Average = %.5Rf\nVariance = %.5Rf\n", av, va);
99       mpfr_clear (av);
100       mpfr_clear (va);
101       mpfr_clear (tmp);
102     }
103 #endif /* HAVE_STDARG */
104 
105   for (i = 0; i < nbtests; ++i)
106     mpfr_clear (t[i]);
107   tests_free (t, nbtests * sizeof (mpfr_t));
108   return;
109 }
110 
111 
112 int
113 main (int argc, char *argv[])
114 {
115   long nbtests;
116   int verbose;
117   tests_start_mpfr ();
118 
119   verbose = 0;
120   nbtests = 10;
121   if (argc > 1)
122     {
123       long a = atol (argv[1]);
124       verbose = 1;
125       if (a != 0)
126         nbtests = a;
127     }
128 
129   test_grandom (nbtests, 420, MPFR_RNDN, verbose);
130   test_special (2);
131   test_special (42000);
132 
133   tests_end_mpfr ();
134   return 0;
135 }
136