tset.c revision 1.1.1.5 1 1.1 mrg /* Test file for mpfr_set.
2 1.1 mrg
3 1.1.1.5 mrg Copyright 2001-2020 Free Software Foundation, Inc.
4 1.1.1.3 mrg Contributed by the AriC and Caramba projects, INRIA.
5 1.1 mrg
6 1.1 mrg This file is part of the GNU MPFR Library.
7 1.1 mrg
8 1.1 mrg The GNU MPFR Library is free software; you can redistribute it and/or modify
9 1.1 mrg it under the terms of the GNU Lesser General Public License as published by
10 1.1 mrg the Free Software Foundation; either version 3 of the License, or (at your
11 1.1 mrg option) any later version.
12 1.1 mrg
13 1.1 mrg The GNU MPFR Library is distributed in the hope that it will be useful, but
14 1.1 mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 1.1 mrg or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 1.1 mrg License for more details.
17 1.1 mrg
18 1.1 mrg You should have received a copy of the GNU Lesser General Public License
19 1.1 mrg along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 1.1.1.5 mrg https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 1.1 mrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 1.1 mrg
23 1.1 mrg #include "mpfr-test.h"
24 1.1 mrg
25 1.1.1.4 mrg static int error;
26 1.1 mrg
27 1.1 mrg #define PRINT_ERROR_IF(condition, text) \
28 1.1 mrg do { \
29 1.1 mrg if (condition) \
30 1.1 mrg { \
31 1.1 mrg printf ("%s", text); \
32 1.1 mrg error = 1; \
33 1.1 mrg } \
34 1.1 mrg } while (0)
35 1.1 mrg
36 1.1 mrg
37 1.1.1.4 mrg /* Maybe better create its own test file? */
38 1.1 mrg static void
39 1.1 mrg check_neg_special (void)
40 1.1 mrg {
41 1.1.1.4 mrg mpfr_t x, y;
42 1.1.1.4 mrg int inexact;
43 1.1.1.4 mrg int s1, s2, s3;
44 1.1.1.4 mrg
45 1.1.1.4 mrg mpfr_inits2 (53, x, y, (mpfr_ptr) 0);
46 1.1.1.4 mrg
47 1.1 mrg MPFR_SET_NAN (x);
48 1.1.1.4 mrg s1 = mpfr_signbit (x) != 0;
49 1.1.1.4 mrg
50 1.1 mrg mpfr_clear_nanflag ();
51 1.1.1.4 mrg inexact = mpfr_neg (y, x, MPFR_RNDN);
52 1.1.1.4 mrg s2 = mpfr_signbit (y) != 0;
53 1.1 mrg PRINT_ERROR_IF (!mpfr_nanflag_p (),
54 1.1.1.4 mrg "ERROR: neg (NaN) doesn't set Nan flag (1).\n");
55 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nan_p (y) || inexact != 0,
56 1.1.1.4 mrg "ERROR: neg (NaN) failed to set variable to NaN (1).\n");
57 1.1.1.4 mrg PRINT_ERROR_IF (s1 == s2,
58 1.1.1.4 mrg "ERROR: neg (NaN) doesn't correctly flip sign bit (1).\n");
59 1.1.1.4 mrg
60 1.1.1.4 mrg mpfr_clear_nanflag ();
61 1.1.1.4 mrg inexact = mpfr_neg (x, x, MPFR_RNDN);
62 1.1.1.4 mrg s2 = mpfr_signbit (x) != 0;
63 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nanflag_p (),
64 1.1.1.4 mrg "ERROR: neg (NaN) doesn't set Nan flag (2).\n");
65 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nan_p (x) || inexact != 0,
66 1.1.1.4 mrg "ERROR: neg (NaN) failed to set variable to NaN (2).\n");
67 1.1.1.4 mrg /* check following "bug" is fixed:
68 1.1.1.4 mrg https://sympa.inria.fr/sympa/arc/mpfr/2017-11/msg00003.html */
69 1.1.1.4 mrg PRINT_ERROR_IF (s1 == s2,
70 1.1.1.4 mrg "ERROR: neg (NaN) doesn't correctly flip sign bit (2).\n");
71 1.1.1.4 mrg
72 1.1.1.4 mrg mpfr_clear_nanflag ();
73 1.1.1.4 mrg inexact = mpfr_neg (y, x, MPFR_RNDN);
74 1.1.1.4 mrg s3 = mpfr_signbit (y) != 0;
75 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nanflag_p (),
76 1.1.1.4 mrg "ERROR: neg (NaN) doesn't set Nan flag (3).\n");
77 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nan_p (y) || inexact != 0,
78 1.1.1.4 mrg "ERROR: neg (NaN) failed to set variable to NaN (3).\n");
79 1.1.1.4 mrg PRINT_ERROR_IF (s2 == s3,
80 1.1.1.4 mrg "ERROR: neg (NaN) doesn't correctly flip sign bit (3).\n");
81 1.1.1.4 mrg
82 1.1.1.4 mrg mpfr_clear_nanflag ();
83 1.1.1.4 mrg inexact = mpfr_neg (x, x, MPFR_RNDN);
84 1.1.1.4 mrg s3 = mpfr_signbit (x) != 0;
85 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nanflag_p (),
86 1.1.1.4 mrg "ERROR: neg (NaN) doesn't set Nan flag (4).\n");
87 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nan_p (x) || inexact != 0,
88 1.1.1.4 mrg "ERROR: neg (NaN) failed to set variable to NaN (4).\n");
89 1.1.1.4 mrg PRINT_ERROR_IF (s2 == s3,
90 1.1.1.4 mrg "ERROR: neg (NaN) doesn't correctly flip sign bit (4).\n");
91 1.1.1.4 mrg
92 1.1.1.4 mrg mpfr_clears (x, y, (mpfr_ptr) 0);
93 1.1 mrg }
94 1.1 mrg
95 1.1 mrg static void
96 1.1 mrg check_special (void)
97 1.1 mrg {
98 1.1 mrg mpfr_t x, y;
99 1.1 mrg int inexact;
100 1.1.1.4 mrg int s1, s2;
101 1.1 mrg
102 1.1.1.4 mrg mpfr_inits2 (53, x, y, (mpfr_ptr) 0);
103 1.1 mrg
104 1.1 mrg mpfr_set_inf (x, 1);
105 1.1 mrg PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) < 0,
106 1.1.1.4 mrg "ERROR: mpfr_set_inf failed to set variable to +inf [1].\n");
107 1.1.1.4 mrg mpfr_set_inf (x, INT_MAX);
108 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) < 0,
109 1.1.1.4 mrg "ERROR: mpfr_set_inf failed to set variable to +inf [2].\n");
110 1.1.1.4 mrg mpfr_set_inf (x, 0);
111 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) < 0,
112 1.1.1.4 mrg "ERROR: mpfr_set_inf failed to set variable to +inf [3].\n");
113 1.1 mrg inexact = mpfr_set (y, x, MPFR_RNDN);
114 1.1 mrg PRINT_ERROR_IF (!mpfr_inf_p (y) || mpfr_sgn (y) < 0 || inexact != 0,
115 1.1 mrg "ERROR: mpfr_set failed to set variable to +infinity.\n");
116 1.1 mrg
117 1.1 mrg inexact = mpfr_set_ui (y, 0, MPFR_RNDN);
118 1.1.1.4 mrg PRINT_ERROR_IF (MPFR_NOTZERO (y) || MPFR_IS_NEG (y) || inexact != 0,
119 1.1 mrg "ERROR: mpfr_set_ui failed to set variable to +0.\n");
120 1.1 mrg
121 1.1 mrg mpfr_set_inf (x, -1);
122 1.1 mrg PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) > 0,
123 1.1.1.4 mrg "ERROR: mpfr_set_inf failed to set variable to -inf [1].\n");
124 1.1.1.4 mrg mpfr_set_inf (x, INT_MIN);
125 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) > 0,
126 1.1.1.4 mrg "ERROR: mpfr_set_inf failed to set variable to -inf [2].\n");
127 1.1 mrg inexact = mpfr_set (y, x, MPFR_RNDN);
128 1.1 mrg PRINT_ERROR_IF (!mpfr_inf_p (y) || mpfr_sgn (y) > 0 || inexact != 0,
129 1.1 mrg "ERROR: mpfr_set failed to set variable to -infinity.\n");
130 1.1 mrg
131 1.1 mrg mpfr_set_zero (x, 1);
132 1.1.1.4 mrg PRINT_ERROR_IF (MPFR_NOTZERO (x) || MPFR_IS_NEG (x),
133 1.1.1.4 mrg "ERROR: mpfr_set_zero failed to set variable to +0 [1].\n");
134 1.1.1.4 mrg mpfr_set_zero (x, INT_MAX);
135 1.1.1.4 mrg PRINT_ERROR_IF (MPFR_NOTZERO (x) || MPFR_IS_NEG (x),
136 1.1.1.4 mrg "ERROR: mpfr_set_zero failed to set variable to +0 [2].\n");
137 1.1.1.4 mrg mpfr_set_zero (x, 0);
138 1.1.1.4 mrg PRINT_ERROR_IF (MPFR_NOTZERO (x) || MPFR_IS_NEG (x),
139 1.1.1.4 mrg "ERROR: mpfr_set_zero failed to set variable to +0 [3].\n");
140 1.1 mrg inexact = mpfr_set (y, x, MPFR_RNDN);
141 1.1.1.4 mrg PRINT_ERROR_IF (MPFR_NOTZERO (y) || MPFR_IS_NEG (y) || inexact != 0,
142 1.1 mrg "ERROR: mpfr_set failed to set variable to +0.\n");
143 1.1 mrg
144 1.1 mrg mpfr_set_zero (x, -1);
145 1.1.1.4 mrg PRINT_ERROR_IF (MPFR_NOTZERO (x) || MPFR_IS_POS (x),
146 1.1.1.4 mrg "ERROR: mpfr_set_zero failed to set variable to -0 [1].\n");
147 1.1.1.4 mrg mpfr_set_zero (x, INT_MIN);
148 1.1.1.4 mrg PRINT_ERROR_IF (MPFR_NOTZERO (x) || MPFR_IS_POS (x),
149 1.1.1.4 mrg "ERROR: mpfr_set_zero failed to set variable to -0 [2].\n");
150 1.1 mrg inexact = mpfr_set (y, x, MPFR_RNDN);
151 1.1.1.4 mrg PRINT_ERROR_IF (MPFR_NOTZERO (y) || MPFR_IS_POS (y) || inexact != 0,
152 1.1 mrg "ERROR: mpfr_set failed to set variable to -0.\n");
153 1.1 mrg
154 1.1.1.4 mrg /* NaN tests */
155 1.1.1.4 mrg
156 1.1 mrg mpfr_set_nan (x);
157 1.1 mrg PRINT_ERROR_IF (!mpfr_nan_p (x),
158 1.1 mrg "ERROR: mpfr_set_nan failed to set variable to NaN.\n");
159 1.1.1.4 mrg s1 = mpfr_signbit (x) != 0;
160 1.1.1.4 mrg
161 1.1.1.4 mrg mpfr_clear_nanflag ();
162 1.1 mrg inexact = mpfr_set (y, x, MPFR_RNDN);
163 1.1.1.4 mrg s2 = mpfr_signbit (y) != 0;
164 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nanflag_p (),
165 1.1.1.4 mrg "ERROR: mpfr_set doesn't set Nan flag (1).\n");
166 1.1 mrg PRINT_ERROR_IF (!mpfr_nan_p (y) || inexact != 0,
167 1.1.1.4 mrg "ERROR: mpfr_set failed to set variable to NaN (1).\n");
168 1.1.1.4 mrg PRINT_ERROR_IF (s1 != s2,
169 1.1.1.4 mrg "ERROR: mpfr_set doesn't preserve the sign bit (1).\n");
170 1.1 mrg
171 1.1.1.4 mrg mpfr_clear_nanflag ();
172 1.1.1.4 mrg inexact = mpfr_set (x, x, MPFR_RNDN);
173 1.1.1.4 mrg s2 = mpfr_signbit (x) != 0;
174 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nanflag_p (),
175 1.1.1.4 mrg "ERROR: mpfr_set doesn't set Nan flag (2).\n");
176 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nan_p (x) || inexact != 0,
177 1.1.1.4 mrg "ERROR: mpfr_set failed to set variable to NaN (2).\n");
178 1.1.1.4 mrg PRINT_ERROR_IF (s1 != s2,
179 1.1.1.4 mrg "ERROR: mpfr_set doesn't preserve the sign bit (2).\n");
180 1.1.1.4 mrg
181 1.1.1.4 mrg MPFR_CHANGE_SIGN (x);
182 1.1.1.4 mrg s1 = !s1;
183 1.1.1.4 mrg
184 1.1.1.4 mrg mpfr_clear_nanflag ();
185 1.1.1.4 mrg inexact = mpfr_set (y, x, MPFR_RNDN);
186 1.1.1.4 mrg s2 = mpfr_signbit (y) != 0;
187 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nanflag_p (),
188 1.1.1.4 mrg "ERROR: mpfr_set doesn't set Nan flag (3).\n");
189 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nan_p (y) || inexact != 0,
190 1.1.1.4 mrg "ERROR: mpfr_set failed to set variable to NaN (3).\n");
191 1.1.1.4 mrg PRINT_ERROR_IF (s1 != s2,
192 1.1.1.4 mrg "ERROR: mpfr_set doesn't preserve the sign bit (3).\n");
193 1.1.1.4 mrg
194 1.1.1.4 mrg mpfr_clear_nanflag ();
195 1.1.1.4 mrg inexact = mpfr_set (x, x, MPFR_RNDN);
196 1.1.1.4 mrg s2 = mpfr_signbit (x) != 0;
197 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nanflag_p (),
198 1.1.1.4 mrg "ERROR: mpfr_set doesn't set Nan flag (4).\n");
199 1.1.1.4 mrg PRINT_ERROR_IF (!mpfr_nan_p (x) || inexact != 0,
200 1.1.1.4 mrg "ERROR: mpfr_set failed to set variable to NaN (4).\n");
201 1.1.1.4 mrg PRINT_ERROR_IF (s1 != s2,
202 1.1.1.4 mrg "ERROR: mpfr_set doesn't preserve the sign bit (4).\n");
203 1.1.1.4 mrg
204 1.1.1.4 mrg mpfr_clears (x, y, (mpfr_ptr) 0);
205 1.1 mrg }
206 1.1 mrg
207 1.1 mrg static void
208 1.1 mrg check_ternary_value (void)
209 1.1 mrg {
210 1.1 mrg int p, q, rnd;
211 1.1 mrg int inexact, cmp;
212 1.1 mrg mpfr_t x, y;
213 1.1 mrg
214 1.1 mrg mpfr_init (x);
215 1.1 mrg mpfr_init (y);
216 1.1 mrg for (p=2; p<500; p++)
217 1.1 mrg {
218 1.1 mrg mpfr_set_prec (x, p);
219 1.1 mrg mpfr_urandomb (x, RANDS);
220 1.1 mrg if (randlimb () % 2)
221 1.1 mrg mpfr_neg (x, x, MPFR_RNDN);
222 1.1 mrg for (q=2; q<2*p; q++)
223 1.1 mrg {
224 1.1 mrg mpfr_set_prec (y, q);
225 1.1 mrg for (rnd = 0; rnd < MPFR_RND_MAX; rnd++)
226 1.1 mrg {
227 1.1.1.4 mrg if (rnd == MPFR_RNDF) /* the test below makes no sense */
228 1.1.1.4 mrg continue;
229 1.1 mrg inexact = mpfr_set (y, x, (mpfr_rnd_t) rnd);
230 1.1 mrg cmp = mpfr_cmp (y, x);
231 1.1 mrg if (((inexact == 0) && (cmp != 0)) ||
232 1.1 mrg ((inexact > 0) && (cmp <= 0)) ||
233 1.1 mrg ((inexact < 0) && (cmp >= 0)))
234 1.1 mrg {
235 1.1.1.4 mrg printf ("Wrong ternary value in mpfr_set for %s: expected"
236 1.1.1.4 mrg " %d, got %d\n",
237 1.1.1.4 mrg mpfr_print_rnd_mode ((mpfr_rnd_t) rnd), cmp,
238 1.1.1.4 mrg inexact);
239 1.1.1.4 mrg exit (1);
240 1.1.1.4 mrg }
241 1.1.1.4 mrg /* Test mpfr_set function too */
242 1.1.1.4 mrg inexact = (mpfr_set) (y, x, (mpfr_rnd_t) rnd);
243 1.1.1.4 mrg cmp = mpfr_cmp (y, x);
244 1.1.1.4 mrg if (((inexact == 0) && (cmp != 0)) ||
245 1.1.1.4 mrg ((inexact > 0) && (cmp <= 0)) ||
246 1.1.1.4 mrg ((inexact < 0) && (cmp >= 0)))
247 1.1.1.4 mrg {
248 1.1.1.4 mrg printf ("Wrong ternary value in mpfr_set(2): expected %d,"
249 1.1 mrg " got %d\n", cmp, inexact);
250 1.1 mrg exit (1);
251 1.1 mrg }
252 1.1 mrg }
253 1.1 mrg }
254 1.1 mrg }
255 1.1 mrg mpfr_clear (x);
256 1.1 mrg mpfr_clear (y);
257 1.1 mrg }
258 1.1 mrg
259 1.1.1.5 mrg static void
260 1.1.1.5 mrg test_set_1_2 (void)
261 1.1.1.5 mrg {
262 1.1.1.5 mrg mpfr_t u, v, zz, z;
263 1.1.1.5 mrg int inex;
264 1.1.1.5 mrg
265 1.1.1.5 mrg /* (8,16)-bit test */
266 1.1.1.5 mrg mpfr_inits2 (16, u, v, zz, (mpfr_ptr) 0);
267 1.1.1.5 mrg mpfr_init2 (z, 8);
268 1.1.1.5 mrg mpfr_set_str_binary (u, "0.1100001100011010E-1");
269 1.1.1.5 mrg mpfr_set_str_binary (v, "0.1100010101110010E0");
270 1.1.1.5 mrg /* u + v = 1.0010011011111111 */
271 1.1.1.5 mrg inex = mpfr_add (zz, u, v, MPFR_RNDN);
272 1.1.1.5 mrg MPFR_ASSERTN(inex > 0);
273 1.1.1.5 mrg mpfr_set_str_binary (u, "1.001001110000000");
274 1.1.1.5 mrg MPFR_ASSERTN(mpfr_equal_p (zz, u));
275 1.1.1.5 mrg inex = mpfr_set_1_2 (z, zz, MPFR_RNDN, inex);
276 1.1.1.5 mrg /* we should have z = 1.0010011 and inex < 0 */
277 1.1.1.5 mrg MPFR_ASSERTN(inex < 0);
278 1.1.1.5 mrg mpfr_set_str_binary (u, "1.0010011");
279 1.1.1.5 mrg MPFR_ASSERTN(mpfr_equal_p (z, u));
280 1.1.1.5 mrg mpfr_clears (u, v, zz, z, (mpfr_ptr) 0);
281 1.1.1.5 mrg
282 1.1.1.5 mrg /* (16,32)-bit test:
283 1.1.1.5 mrg * take for v a random 32-bit number in [1/2,1), here 2859611790/2^32
284 1.1.1.5 mrg * take for z a random 16-bit number in [1,2), less than 2*v,
285 1.1.1.5 mrg with last bit 0, here we take z = 40900/2^15
286 1.1.1.5 mrg * take u = z-v-1/2^16-1/2^32 */
287 1.1.1.5 mrg mpfr_inits2 (32, u, v, zz, (mpfr_ptr) 0);
288 1.1.1.5 mrg mpfr_init2 (z, 16);
289 1.1.1.5 mrg mpfr_set_str_binary (u, "0.10010101000101001100100101110001");
290 1.1.1.5 mrg mpfr_set_str_binary (v, "0.10101010011100100011011010001110");
291 1.1.1.5 mrg /* u + v = 1.00111111100001101111111111111111 */
292 1.1.1.5 mrg inex = mpfr_add (zz, u, v, MPFR_RNDN);
293 1.1.1.5 mrg MPFR_ASSERTN(inex > 0);
294 1.1.1.5 mrg mpfr_set_str_binary (u, "1.0011111110000111");
295 1.1.1.5 mrg MPFR_ASSERTN(mpfr_equal_p (zz, u));
296 1.1.1.5 mrg inex = mpfr_set_1_2 (z, zz, MPFR_RNDN, inex);
297 1.1.1.5 mrg /* we should have z = 1.001111111000011 and inex < 0 */
298 1.1.1.5 mrg MPFR_ASSERTN(inex < 0);
299 1.1.1.5 mrg mpfr_set_str_binary (u, "1.001111111000011");
300 1.1.1.5 mrg MPFR_ASSERTN(mpfr_equal_p (z, u));
301 1.1.1.5 mrg mpfr_clears (u, v, zz, z, (mpfr_ptr) 0);
302 1.1.1.5 mrg
303 1.1.1.5 mrg /* (32,64)-bit test:
304 1.1.1.5 mrg * take for v a random 64-bit number in [1/2,1),
305 1.1.1.5 mrg here v = 13687985014345662879/2^64
306 1.1.1.5 mrg * take for z a random 32-bit number in [1,2), less than 2*v,
307 1.1.1.5 mrg with last bit 0, here we take z = 2871078774/2^31
308 1.1.1.5 mrg * take u = z-v-1/2^32-1/2^64 */
309 1.1.1.5 mrg mpfr_inits2 (64, u, v, zz, (mpfr_ptr) 0);
310 1.1.1.5 mrg mpfr_init2 (z, 32);
311 1.1.1.5 mrg mpfr_set_str_binary (u, "0.10011000010011001110000100010001110010010000111001111110011");
312 1.1.1.5 mrg mpfr_set_str_binary (v, "0.1011110111110101011111011101100100110110111100011000000110011111");
313 1.1.1.5 mrg /* u + v = 1.0101011001000010010111101110101011111111111111111111111111111111 */
314 1.1.1.5 mrg inex = mpfr_add (zz, u, v, MPFR_RNDN);
315 1.1.1.5 mrg MPFR_ASSERTN(inex > 0);
316 1.1.1.5 mrg mpfr_set_str_binary (u, "1.01010110010000100101111011101011");
317 1.1.1.5 mrg MPFR_ASSERTN(mpfr_equal_p (zz, u));
318 1.1.1.5 mrg inex = mpfr_set_1_2 (z, zz, MPFR_RNDN, inex);
319 1.1.1.5 mrg /* we should have z = 1.0101011001000010010111101110101 and inex < 0 */
320 1.1.1.5 mrg MPFR_ASSERTN(inex < 0);
321 1.1.1.5 mrg mpfr_set_str_binary (u, "1.0101011001000010010111101110101");
322 1.1.1.5 mrg MPFR_ASSERTN(mpfr_equal_p (z, u));
323 1.1.1.5 mrg mpfr_clears (u, v, zz, z, (mpfr_ptr) 0);
324 1.1.1.5 mrg
325 1.1.1.5 mrg /* (64,128)-bit test:
326 1.1.1.5 mrg * take for v a random 128-bit number in [1/2,1),
327 1.1.1.5 mrg here v = 322263811942091240216761391118876232409/2^128
328 1.1.1.5 mrg * take for z a random 64-bit number in [1,2), less than 2*v,
329 1.1.1.5 mrg with last bit 0, here we take z = 16440347967874738276/2^63
330 1.1.1.5 mrg * take u = z-v-1/2^64-1/2^128 */
331 1.1.1.5 mrg mpfr_inits2 (128, u, v, zz, (mpfr_ptr) 0);
332 1.1.1.5 mrg mpfr_init2 (z, 64);
333 1.1.1.5 mrg mpfr_set_str_binary (u, "0.1101010111011101111100100001011111111000010011011001000101111010110101101101011011100110101001010001101011011110101101010010011");
334 1.1.1.5 mrg mpfr_set_str_binary (v, "0.11110010011100011100000010100110100010011010110010111111010011000010100100101001000110010101101011100101001000010100101011011001");
335 1.1.1.5 mrg inex = mpfr_add (zz, u, v, MPFR_RNDN);
336 1.1.1.5 mrg MPFR_ASSERTN(inex > 0);
337 1.1.1.5 mrg mpfr_set_str_binary (u, "1.1100100001001111101100101011111010000001111110100101000011000111");
338 1.1.1.5 mrg MPFR_ASSERTN(mpfr_equal_p (zz, u));
339 1.1.1.5 mrg inex = mpfr_set_1_2 (z, zz, MPFR_RNDN, inex);
340 1.1.1.5 mrg MPFR_ASSERTN(inex < 0);
341 1.1.1.5 mrg mpfr_set_str_binary (u, "1.1100100001001111101100101011111010000001111110100101000011000110");
342 1.1.1.5 mrg MPFR_ASSERTN(mpfr_equal_p (z, u));
343 1.1.1.5 mrg mpfr_clears (u, v, zz, z, (mpfr_ptr) 0);
344 1.1.1.5 mrg }
345 1.1.1.5 mrg
346 1.1 mrg #define TEST_FUNCTION mpfr_set
347 1.1 mrg #include "tgeneric.c"
348 1.1 mrg
349 1.1 mrg int
350 1.1 mrg main (void)
351 1.1 mrg {
352 1.1 mrg mpfr_t x, y, z, u;
353 1.1 mrg int inexact;
354 1.1 mrg mpfr_exp_t emax;
355 1.1 mrg
356 1.1 mrg tests_start_mpfr ();
357 1.1 mrg
358 1.1.1.5 mrg test_set_1_2 ();
359 1.1.1.5 mrg
360 1.1 mrg /* Default : no error */
361 1.1 mrg error = 0;
362 1.1 mrg
363 1.1 mrg /* check prototypes of mpfr_init_set_* */
364 1.1 mrg inexact = mpfr_init_set_si (x, -1, MPFR_RNDN);
365 1.1.1.2 mrg MPFR_ASSERTN (inexact == 0);
366 1.1 mrg inexact = mpfr_init_set (y, x, MPFR_RNDN);
367 1.1.1.2 mrg MPFR_ASSERTN (inexact == 0);
368 1.1 mrg inexact = mpfr_init_set_ui (z, 1, MPFR_RNDN);
369 1.1.1.2 mrg MPFR_ASSERTN (inexact == 0);
370 1.1 mrg inexact = mpfr_init_set_d (u, 1.0, MPFR_RNDN);
371 1.1.1.2 mrg MPFR_ASSERTN (inexact == 0);
372 1.1 mrg
373 1.1 mrg emax = mpfr_get_emax ();
374 1.1 mrg set_emax (0);
375 1.1 mrg mpfr_set_prec (x, 3);
376 1.1 mrg mpfr_set_str_binary (x, "0.111");
377 1.1 mrg mpfr_set_prec (y, 2);
378 1.1 mrg mpfr_set (y, x, MPFR_RNDU);
379 1.1.1.4 mrg if (!(MPFR_IS_INF (y) && MPFR_IS_POS (y)))
380 1.1 mrg {
381 1.1 mrg printf ("Error for y=x=0.111 with px=3, py=2 and emax=0\nx=");
382 1.1 mrg mpfr_dump (x);
383 1.1 mrg printf ("y=");
384 1.1 mrg mpfr_dump (y);
385 1.1 mrg exit (1);
386 1.1 mrg }
387 1.1 mrg
388 1.1 mrg set_emax (emax);
389 1.1 mrg
390 1.1 mrg mpfr_set_prec (y, 11);
391 1.1 mrg mpfr_set_str_binary (y, "0.11111111100E-8");
392 1.1 mrg mpfr_set_prec (x, 2);
393 1.1 mrg mpfr_set (x, y, MPFR_RNDN);
394 1.1 mrg mpfr_set_str_binary (y, "1.0E-8");
395 1.1 mrg if (mpfr_cmp (x, y))
396 1.1 mrg {
397 1.1 mrg printf ("Error for y=0.11111111100E-8, prec=2, rnd=MPFR_RNDN\n");
398 1.1 mrg exit (1);
399 1.1 mrg }
400 1.1 mrg
401 1.1 mrg mpfr_clear (x);
402 1.1 mrg mpfr_clear (y);
403 1.1 mrg mpfr_clear (z);
404 1.1 mrg mpfr_clear (u);
405 1.1 mrg
406 1.1 mrg check_ternary_value ();
407 1.1 mrg check_special ();
408 1.1 mrg check_neg_special ();
409 1.1 mrg
410 1.1.1.4 mrg test_generic (MPFR_PREC_MIN, 1000, 10);
411 1.1 mrg
412 1.1 mrg tests_end_mpfr ();
413 1.1 mrg return error;
414 1.1 mrg }
415