1 /* Test file for mpfr_set_f.
2
3 Copyright 1999, 2001-2023 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 https://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 "mpfr-test.h"
24
25 #ifndef MPFR_USE_MINI_GMP
26
27 int
main(void)28 main (void)
29 {
30 mpfr_t x, u;
31 mpf_t y, z;
32 mpfr_exp_t emax;
33 unsigned long k, pr;
34 int r, inexact;
35
36 tests_start_mpfr ();
37
38 mpf_init (y);
39 mpf_init (z);
40
41 mpf_set_d (y, 0.0);
42
43 /* check prototype of mpfr_init_set_f */
44 mpfr_init_set_f (x, y, MPFR_RNDN);
45 mpfr_set_prec (x, 100);
46 mpfr_set_f (x, y, MPFR_RNDN);
47
48 mpf_urandomb (y, RANDS, 10 * GMP_NUMB_BITS);
49 mpfr_set_f (x, y, RND_RAND ());
50
51 /* bug found by Jean-Pierre Merlet */
52 mpfr_set_prec (x, 256);
53 mpf_set_prec (y, 256);
54 mpfr_init2 (u, 256);
55 mpfr_set_str (u,
56 "7.f10872b020c49ba5e353f7ced916872b020c49ba5e353f7ced916872b020c498@2",
57 16, MPFR_RNDN);
58 mpf_set_str (y, "2033033E-3", 10); /* avoid 2033.033 which is
59 locale-sensitive */
60 mpfr_set_f (x, y, MPFR_RNDN);
61 if (mpfr_cmp (x, u))
62 {
63 printf ("mpfr_set_f failed for y=2033033E-3\n");
64 exit (1);
65 }
66 mpf_set_str (y, "-2033033E-3", 10); /* avoid -2033.033 which is
67 locale-sensitive */
68 mpfr_set_f (x, y, MPFR_RNDN);
69 mpfr_neg (u, u, MPFR_RNDN);
70 if (mpfr_cmp (x, u))
71 {
72 printf ("mpfr_set_f failed for y=-2033033E-3\n");
73 exit (1);
74 }
75
76 mpf_set_prec (y, 300);
77 mpf_set_str (y, "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", -2);
78 mpf_mul_2exp (y, y, 600);
79 mpfr_set_prec (x, 300);
80 mpfr_set_f (x, y, MPFR_RNDN);
81 if (mpfr_check (x) == 0)
82 {
83 printf ("Error in mpfr_set_f: corrupted result\n");
84 mpfr_dump (x);
85 exit (1);
86 }
87 MPFR_ASSERTN(mpfr_cmp_ui_2exp (x, 1, 901) == 0);
88
89 /* random values */
90 for (k = 1; k <= 1000; k++)
91 {
92 pr = 2 + (randlimb () & 255);
93 mpf_set_prec (z, pr);
94 mpf_urandomb (z, RANDS, z->_mp_prec);
95 mpfr_set_prec (u, ((pr / GMP_NUMB_BITS + 1) * GMP_NUMB_BITS));
96 mpfr_set_f (u, z, MPFR_RNDN);
97 if (mpfr_cmp_f (u, z) != 0)
98 {
99 printf ("Error in mpfr_set_f:\n");
100 printf ("mpf (precision=%lu)=", pr);
101 mpf_out_str (stdout, 16, 0, z);
102 printf ("\nmpfr(precision=%lu)=",
103 ((pr / GMP_NUMB_BITS + 1) * GMP_NUMB_BITS));
104 mpfr_out_str (stdout, 16, 0, u, MPFR_RNDN);
105 putchar ('\n');
106 exit (1);
107 }
108 mpfr_set_prec (x, pr);
109 mpfr_set_f (x, z, MPFR_RNDN);
110 mpfr_sub (u, u, x, MPFR_RNDN);
111 mpfr_abs (u, u, MPFR_RNDN);
112 if (mpfr_cmp_ui_2exp (u, 1, -pr - 1) > 0)
113 {
114 printf ("Error in mpfr_set_f: precision=%lu\n", pr);
115 printf ("mpf =");
116 mpf_out_str (stdout, 16, 0, z);
117 printf ("\nmpfr=");
118 mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN);
119 putchar ('\n');
120 exit (1);
121 }
122 }
123
124 /* Check for +0 */
125 mpfr_set_prec (x, 53);
126 mpf_set_prec (y, 53);
127 mpf_set_ui (y, 0);
128 RND_LOOP (r)
129 {
130 int i;
131 for (i = -1; i <= 1; i++)
132 {
133 if (i)
134 mpfr_set_si (x, i, MPFR_RNDN);
135 inexact = mpfr_set_f (x, y, (mpfr_rnd_t) r);
136 if (!MPFR_IS_ZERO(x) || !MPFR_IS_POS(x) || inexact)
137 {
138 printf ("mpfr_set_f(x,0) failed for %s, i = %d\n",
139 mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
140 exit (1);
141 }
142 }
143 }
144
145 /* coverage test */
146 mpf_set_prec (y, 2);
147 mpfr_set_prec (x, 3 * mp_bits_per_limb);
148 mpf_set_ui (y, 1);
149 for (r = 0; r < mp_bits_per_limb; r++)
150 {
151 mpfr_urandomb (x, RANDS); /* to fill low limbs with random data */
152 inexact = mpfr_set_f (x, y, MPFR_RNDN);
153 MPFR_ASSERTN(inexact == 0 && mpfr_cmp_ui_2exp (x, 1, r) == 0);
154 mpf_mul_2exp (y, y, 1);
155 }
156
157 mpf_set_ui (y, 1);
158 mpf_mul_2exp (y, y, ULONG_MAX);
159 mpfr_set_f (x, y, MPFR_RNDN);
160 mpfr_set_ui (u, 1, MPFR_RNDN);
161 mpfr_mul_2ui (u, u, ULONG_MAX, MPFR_RNDN);
162 if (!mpfr_equal_p (x, u))
163 {
164 printf ("Error: mpfr_set_f (x, y, MPFR_RNDN) for y = 2^ULONG_MAX\n");
165 exit (1);
166 }
167
168 emax = mpfr_get_emax ();
169
170 /* For mpf_mul_2exp, emax must fit in an unsigned long! */
171 if (emax >= 0 && emax <= ULONG_MAX)
172 {
173 mpf_set_ui (y, 1);
174 mpf_mul_2exp (y, y, emax);
175 mpfr_set_f (x, y, MPFR_RNDN);
176 mpfr_set_ui_2exp (u, 1, emax, MPFR_RNDN);
177 if (!mpfr_equal_p (x, u))
178 {
179 printf ("Error: mpfr_set_f (x, y, MPFR_RNDN) for y = 2^emax\n");
180 exit (1);
181 }
182 }
183
184 /* For mpf_mul_2exp, emax - 1 must fit in an unsigned long! */
185 if (emax >= 1 && emax - 1 <= ULONG_MAX)
186 {
187 mpf_set_ui (y, 1);
188 mpf_mul_2exp (y, y, emax - 1);
189 mpfr_set_f (x, y, MPFR_RNDN);
190 mpfr_set_ui_2exp (u, 1, emax - 1, MPFR_RNDN);
191 if (!mpfr_equal_p (x, u))
192 {
193 printf ("Error: mpfr_set_f (x, y, MPFR_RNDN) for y = 2^(emax-1)\n");
194 exit (1);
195 }
196 }
197
198 mpfr_clear (x);
199 mpfr_clear (u);
200 mpf_clear (y);
201 mpf_clear (z);
202
203 tests_end_mpfr ();
204 return 0;
205 }
206
207 #else
208
209 int
main(void)210 main (void)
211 {
212 return 77;
213 }
214
215 #endif
216