3 Copyright 2004 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
31 unsigned long max_prec = 15;
32 unsigned long min_prec = __GMPF_BITS_TO_PREC (1);
33 gmp_randstate_ptr rands = RANDS;
34 unsigned long x, prec;
40 refmpf_set_prec_limbs (s, 2*max_prec+10);
42 for (i = 0; i < 50; i++)
44 /* input, a random non-zero ulong, exponentially distributed */
46 x = gmp_urandomb_ui (rands,
47 gmp_urandomm_ui (rands, BITS_PER_ULONG) + 1);
50 /* result precision */
51 prec = gmp_urandomm_ui (rands, max_prec-min_prec) + min_prec;
52 refmpf_set_prec_limbs (r, prec);
57 /* Expect to prec limbs of result.
58 In the current implementation there's no stripping of low zero
59 limbs in mpf_sqrt_ui, not even on perfect squares, so size should
63 printf ("mpf_sqrt_ui result not enough result limbs\n");
64 printf (" x=%lu\n", x);
65 printf (" want prec=%lu\n", prec);
67 printf (" r size %ld\n", (long) SIZ(r));
68 printf (" r prec %ld\n", (long) PREC(r));
72 /* Must have r^2 <= x, since r has been truncated. */
74 if (! (mpf_cmp_ui (s, x) <= 0))
76 printf ("mpf_sqrt_ui result too big\n");
77 printf (" x=%lu\n", x);
78 printf (" want prec=%lu\n", prec);
84 /* Must have (r+ulp)^2 > x.
85 No overflow from refmpf_add_ulp since r is only prec limbs. */
88 if (! (mpf_cmp_ui (s, x) > 0))
90 printf ("mpf_sqrt_ui result too small\n");
91 printf (" x=%lu\n", x);
92 printf (" want prec=%lu\n", prec);
93 mpf_trace (" r+ulp", r);
104 main (int argc, char **argv)