1 /* Test file for mpfr_urandom
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 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_urandom (long nbtests, mpfr_prec_t prec, mpfr_rnd_t rnd, long bit_index,
33 int *tab, size_tab, k, sh, xn;
34 double d, av = 0, var = 0, chi2 = 0, th;
36 mp_size_t limb_index = 0;
37 mp_limb_t limb_mask = 0;
42 size_tab = (nbtests >= 1000 ? nbtests / 50 : 20);
43 tab = (int *) calloc (size_tab, sizeof(int));
46 fprintf (stderr, "trandom: can't allocate memory in test_urandom\n");
51 xn = 1 + (prec - 1) / mp_bits_per_limb;
52 sh = xn * mp_bits_per_limb - prec;
53 if (bit_index >= 0 && bit_index < prec)
55 /* compute the limb index and limb mask to fetch the bit #bit_index */
56 limb_index = (prec - bit_index) / mp_bits_per_limb;
57 i = 1 + bit_index - (bit_index / mp_bits_per_limb) * mp_bits_per_limb;
58 limb_mask = MPFR_LIMB_ONE << (mp_bits_per_limb - i);
61 for (k = 0; k < nbtests; k++)
63 i = mpfr_urandom (x, RANDS, rnd);
64 inex = (i != 0) && inex;
65 /* check that lower bits are zero */
66 if (MPFR_MANT(x)[0] & MPFR_LIMB_MASK(sh) && !MPFR_IS_ZERO (x))
68 printf ("Error: mpfr_urandom() returns invalid numbers:\n");
69 mpfr_print_binary (x); puts ("");
72 /* check that the value is in [0,1] */
73 if (mpfr_cmp_ui (x, 0) < 0 || mpfr_cmp_ui (x, 1) > 0)
75 printf ("Error: mpfr_urandom() returns number outside [0, 1]:\n");
76 mpfr_print_binary (x); puts ("");
80 d = mpfr_get_d1 (x); av += d; var += d*d;
81 i = (int)(size_tab * d);
85 if (limb_mask && (MPFR_MANT (x)[limb_index] & limb_mask))
91 /* one call in the loop pretended to return an exact number! */
92 printf ("Error: mpfr_urandom() returns a zero ternary value.\n");
97 emin = mpfr_get_emin ();
98 for (k = 0; k < 5; k++)
101 inex = mpfr_urandom (x, RANDS, rnd);
102 if (( (rnd == MPFR_RNDZ || rnd == MPFR_RNDD)
103 && (!MPFR_IS_ZERO (x) || inex != -1))
104 || ((rnd == MPFR_RNDU || rnd == MPFR_RNDA)
105 && (mpfr_cmp_ui (x, 1 << k) != 0 || inex != +1))
107 && (k > 0 || mpfr_cmp_ui (x, 1 << k) != 0 || inex != +1)
108 && (!MPFR_IS_ZERO (x) || inex != -1)))
110 printf ("Error: mpfr_urandom() do not handle correctly a restricted"
111 " exponent range.\nrounding mode: %s\nternary value: %d\n"
112 "random value: ", mpfr_print_rnd_mode (rnd), inex);
113 mpfr_print_binary (x); puts ("");
127 var = (var / nbtests) - av * av;
129 th = (double)nbtests / size_tab;
130 printf ("Average = %.5f\nVariance = %.5f\n", av, var);
131 printf ("Repartition for urandom with rounding mode %s. "
132 "Each integer should be close to %d.\n",
133 mpfr_print_rnd_mode (rnd), (int)th);
135 for (k = 0; k < size_tab; k++)
137 chi2 += (tab[k] - th) * (tab[k] - th) / th;
138 printf("%d ", tab[k]);
139 if (((k+1) & 7) == 0)
143 printf("\nChi2 statistics value (with %d degrees of freedom) : %.5f\n",
147 printf ("Bit #%ld is set %ld/%ld = %.1f %% of time\n",
148 bit_index, count, nbtests, count * 100.0 / nbtests);
156 /* problem reported by Carl Witty */
163 #if __MPFR_GMP(4,2,0)
164 # define C1 "0.8488312"
165 # define C2 "0.8156509"
167 # define C1 "0.6485367"
168 # define C2 "0.9362717"
171 gmp_randinit_default (s);
172 gmp_randseed_ui (s, 42);
174 mpfr_urandom (x, s, MPFR_RNDN);
175 if (mpfr_cmp_str1 (x, C1) != 0)
177 printf ("Error in bug20100914, expected " C1 ", got ");
178 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
182 mpfr_urandom (x, s, MPFR_RNDN);
183 if (mpfr_cmp_str1 (x, C2) != 0)
185 printf ("Error in bug20100914, expected " C2 ", got ");
186 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
195 main (int argc, char *argv[])
211 long a = atol(argv[1]);
219 prec = atol(argv[2]);
225 bit_index = atol(argv[3]);
226 if (bit_index >= prec)
228 printf ("Warning. Cannot compute the bit frequency: the given bit "
229 "index (= %ld) is not less than the precision (= %ld).\n",
230 bit_index, (long) prec);
237 test_urandom (nbtests, prec, (mpfr_rnd_t) rnd, bit_index, verbose);
239 if (argc == 1) /* check also small precision */
241 test_urandom (nbtests, 2, (mpfr_rnd_t) rnd, -1, 0);