Tizen 2.1 base
[external/gmp.git] / tests / mpf / t-sqrt_ui.c
1 /* Test mpf_sqrt_ui.
2
3 Copyright 2004 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
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.
11
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.
16
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/.  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "gmp.h"
24 #include "gmp-impl.h"
25 #include "tests.h"
26
27
28 void
29 check_rand (void)
30 {
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;
35   mpf_t              r, s;
36   int                i;
37
38   mpf_init (r);
39   mpf_init (s);
40   refmpf_set_prec_limbs (s, 2*max_prec+10);
41
42   for (i = 0; i < 50; i++)
43     {
44       /* input, a random non-zero ulong, exponentially distributed */
45       do {
46         x = gmp_urandomb_ui (rands,
47                              gmp_urandomm_ui (rands, BITS_PER_ULONG) + 1);
48       } while (x == 0);
49
50       /* result precision */
51       prec = gmp_urandomm_ui (rands, max_prec-min_prec) + min_prec;
52       refmpf_set_prec_limbs (r, prec);
53
54       mpf_sqrt_ui (r, x);
55       MPF_CHECK_FORMAT (r);
56
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
60          be exactly prec.  */
61       if (SIZ(r) != prec)
62         {
63           printf ("mpf_sqrt_ui result not enough result limbs\n");
64           printf    ("  x=%lu\n", x);
65           printf    ("  want prec=%lu\n", prec);
66           mpf_trace ("  r", r);
67           printf    ("  r size %ld\n", (long) SIZ(r));
68           printf    ("  r prec %ld\n", (long) PREC(r));
69           abort ();
70         }
71
72       /* Must have r^2 <= x, since r has been truncated. */
73       mpf_mul (s, r, r);
74       if (! (mpf_cmp_ui (s, x) <= 0))
75         {
76           printf    ("mpf_sqrt_ui result too big\n");
77           printf    ("  x=%lu\n", x);
78           printf    ("  want prec=%lu\n", prec);
79           mpf_trace ("  r", r);
80           mpf_trace ("  s", s);
81           abort ();
82         }
83
84       /* Must have (r+ulp)^2 > x.
85          No overflow from refmpf_add_ulp since r is only prec limbs. */
86       refmpf_add_ulp (r);
87       mpf_mul (s, r, r);
88       if (! (mpf_cmp_ui (s, x) > 0))
89         {
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);
94           mpf_trace ("  s", s);
95           abort ();
96         }
97     }
98
99   mpf_clear (r);
100   mpf_clear (s);
101 }
102
103 int
104 main (int argc, char **argv)
105 {
106   tests_start ();
107   mp_trace_base = -16;
108
109   check_rand ();
110
111   tests_end ();
112   exit (0);
113 }