tset_si.c revision 1.1.1.5 1 /* Test file for mpfr_set_si, mpfr_set_ui, mpfr_get_si and mpfr_get_ui.
2
3 Copyright 1999, 2001-2020 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 #define PRINT_ERROR(str) \
26 do { printf ("Error for %s\n", str); exit (1); } while (0)
27
28 static void
29 test_2exp (void)
30 {
31 mpfr_t x;
32 int res;
33
34 mpfr_init2 (x, 32);
35
36 mpfr_set_ui_2exp (x, 1, 0, MPFR_RNDN);
37 if (mpfr_cmp_ui (x, 1) != 0)
38 PRINT_ERROR ("(1U,0)");
39
40 mpfr_set_ui_2exp (x, 1024, -10, MPFR_RNDN);
41 if (mpfr_cmp_ui(x, 1) != 0)
42 PRINT_ERROR ("(1024U,-10)");
43
44 mpfr_set_ui_2exp (x, 1024, 10, MPFR_RNDN);
45 if (mpfr_cmp_ui (x, 1024 * 1024) != 0)
46 PRINT_ERROR ("(1024U,+10)");
47
48 mpfr_set_si_2exp (x, -1024L * 1024L, -10, MPFR_RNDN);
49 if (mpfr_cmp_si (x, -1024) != 0)
50 PRINT_ERROR ("(1M,-10)");
51
52 mpfr_set_ui_2exp (x, 0x92345678, 16, MPFR_RNDN);
53 if (mpfr_cmp_str (x, "92345678@4", 16, MPFR_RNDN) != 0)
54 PRINT_ERROR ("(x92345678U,+16)");
55
56 mpfr_set_si_2exp (x, -0x1ABCDEF0, -256, MPFR_RNDN);
57 if (mpfr_cmp_str (x, "-1ABCDEF0@-64", 16, MPFR_RNDN) != 0)
58 PRINT_ERROR ("(-x1ABCDEF0,-256)");
59
60 mpfr_set_prec (x, 2);
61 res = mpfr_set_si_2exp (x, 7, 10, MPFR_RNDU);
62 if (mpfr_cmp_ui (x, 1<<13) != 0 || res <= 0)
63 PRINT_ERROR ("Prec 2 + si_2exp");
64
65 res = mpfr_set_ui_2exp (x, 7, 10, MPFR_RNDU);
66 if (mpfr_cmp_ui (x, 1<<13) != 0 || res <= 0)
67 PRINT_ERROR ("Prec 2 + ui_2exp");
68
69 mpfr_clear_flags ();
70 mpfr_set_ui_2exp (x, 17, MPFR_EMAX_MAX, MPFR_RNDN);
71 if (!mpfr_inf_p (x) || MPFR_IS_NEG (x))
72 PRINT_ERROR ("mpfr_set_ui_2exp and overflow (bad result)");
73 if (!mpfr_overflow_p ())
74 PRINT_ERROR ("mpfr_set_ui_2exp and overflow (overflow flag not set)");
75
76 mpfr_clear_flags ();
77 mpfr_set_si_2exp (x, 17, MPFR_EMAX_MAX, MPFR_RNDN);
78 if (!mpfr_inf_p (x) || MPFR_IS_NEG (x))
79 PRINT_ERROR ("mpfr_set_si_2exp (pos) and overflow (bad result)");
80 if (!mpfr_overflow_p ())
81 PRINT_ERROR ("mpfr_set_si_2exp (pos) and overflow (overflow flag not set)");
82
83 mpfr_clear_flags ();
84 mpfr_set_si_2exp (x, -17, MPFR_EMAX_MAX, MPFR_RNDN);
85 if (!mpfr_inf_p (x) || MPFR_IS_POS (x))
86 PRINT_ERROR ("mpfr_set_si_2exp (neg) and overflow (bad result)");
87 if (!mpfr_overflow_p ())
88 PRINT_ERROR ("mpfr_set_si_2exp (neg) and overflow (overflow flag not set)");
89
90 mpfr_clear (x);
91 }
92
93 #define REXP 1024
94
95 static void
96 test_2exp_extreme_aux (void)
97 {
98 mpfr_t x1, x2, y;
99 mpfr_exp_t e, ep[1 + 8 * 5], eb[] =
100 { MPFR_EMIN_MIN, -REXP, REXP, MPFR_EMAX_MAX, MPFR_EXP_MAX };
101 mpfr_flags_t flags1, flags2;
102 int i, j, rnd, inex1, inex2;
103 char s;
104
105 ep[0] = MPFR_EXP_MIN;
106 for (i = 0; i < numberof(eb); i++)
107 for (j = 0; j < 8; j++)
108 ep[1 + 8 * i + j] = eb[i] - j;
109
110 mpfr_inits2 (3, x1, x2, (mpfr_ptr) 0);
111 mpfr_init2 (y, 32);
112
113 for (i = 0; i < numberof(ep); i++)
114 for (j = -31; j <= 31; j++)
115 RND_LOOP_NO_RNDF (rnd)
116 {
117 int sign = j < 0 ? -1 : 1;
118
119 /* Compute the expected value, inex and flags */
120 inex1 = mpfr_set_si (y, j, MPFR_RNDN);
121 MPFR_ASSERTN (inex1 == 0);
122 inex1 = mpfr_set (x1, y, (mpfr_rnd_t) rnd);
123 /* x1 is the rounded value and inex1 the ternary value,
124 assuming that the exponent argument is 0 (this is the
125 rounded significand of the final result, assuming an
126 unbounded exponent range). The multiplication by a
127 power of 2 is exact, unless underflow/overflow occurs.
128 The tests on the exponent below avoid integer overflows
129 (ep[i] may take extreme values). */
130 e = mpfr_get_exp (x1);
131 mpfr_clear_flags ();
132 if (j != 0 && ep[i] < __gmpfr_emin - e) /* underflow */
133 {
134 mpfr_rnd_t r =
135 (rnd == MPFR_RNDN &&
136 (ep[i] < __gmpfr_emin - mpfr_get_exp (y) - 1 ||
137 IS_POW2 (sign * j))) ?
138 MPFR_RNDZ : (mpfr_rnd_t) rnd;
139 inex1 = mpfr_underflow (x1, r, sign);
140 flags1 = __gmpfr_flags;
141 }
142 else if (j != 0 && ep[i] > __gmpfr_emax - e) /* overflow */
143 {
144 inex1 = mpfr_overflow (x1, (mpfr_rnd_t) rnd, sign);
145 flags1 = __gmpfr_flags;
146 }
147 else
148 {
149 if (j != 0)
150 mpfr_set_exp (x1, ep[i] + e);
151 flags1 = inex1 != 0 ? MPFR_FLAGS_INEXACT : 0;
152 }
153
154 /* Test mpfr_set_si_2exp */
155 mpfr_clear_flags ();
156 inex2 = mpfr_set_si_2exp (x2, j, ep[i], (mpfr_rnd_t) rnd);
157 flags2 = __gmpfr_flags;
158
159 if (! (flags1 == flags2 && SAME_SIGN (inex1, inex2) &&
160 mpfr_equal_p (x1, x2)))
161 {
162 s = 's';
163 goto err_extreme;
164 }
165
166 if (j < 0)
167 continue;
168
169 /* Test mpfr_set_ui_2exp */
170 mpfr_clear_flags ();
171 inex2 = mpfr_set_ui_2exp (x2, j, ep[i], (mpfr_rnd_t) rnd);
172 flags2 = __gmpfr_flags;
173
174 if (! (flags1 == flags2 && SAME_SIGN (inex1, inex2) &&
175 mpfr_equal_p (x1, x2)))
176 {
177 s = 'u';
178 err_extreme:
179 printf ("Error in extreme mpfr_set_%ci_2exp for i=%d j=%d %s\n",
180 s, i, j, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
181 printf ("emin=%" MPFR_EXP_FSPEC "d "
182 "emax=%" MPFR_EXP_FSPEC "d\n",
183 (mpfr_eexp_t) __gmpfr_emin,
184 (mpfr_eexp_t) __gmpfr_emax);
185 printf ("ep[%d] = %" MPFR_EXP_FSPEC "d\n",
186 i, (mpfr_eexp_t) ep[i]);
187 printf ("Expected ");
188 mpfr_dump (x1);
189 printf ("with inex = %d and flags =", inex1);
190 flags_out (flags1);
191 printf ("Got ");
192 mpfr_dump (x2);
193 printf ("with inex = %d and flags =", inex2);
194 flags_out (flags2);
195 exit (1);
196 }
197 }
198
199 mpfr_clears (x1, x2, y, (mpfr_ptr) 0);
200 }
201
202 static void
203 test_2exp_extreme (void)
204 {
205 mpfr_exp_t emin, emax;
206
207 emin = mpfr_get_emin ();
208 emax = mpfr_get_emax ();
209
210 set_emin (MPFR_EMIN_MIN);
211 set_emax (MPFR_EMAX_MAX);
212 test_2exp_extreme_aux ();
213
214 set_emin (-REXP);
215 set_emax (REXP);
216 test_2exp_extreme_aux ();
217
218 set_emin (emin);
219 set_emax (emax);
220 }
221
222 static void
223 test_macros (void)
224 {
225 mpfr_t x[3];
226 mpfr_ptr p;
227 int r;
228
229 /* Note: the ++'s below allow one to check that the corresponding
230 arguments are evaluated only once by the macros. */
231
232 mpfr_inits (x[0], x[1], x[2], (mpfr_ptr) 0);
233 p = x[0];
234 r = 0;
235 mpfr_set_ui (p++, 0, (mpfr_rnd_t) r++);
236 if (p != x[1] || r != 1)
237 {
238 printf ("Error in mpfr_set_ui macro: p - x[0] = %d (expecting 1), "
239 "r = %d (expecting 1)\n", (int) (p - x[0]), r);
240 exit (1);
241 }
242 p = x[0];
243 r = 0;
244 mpfr_set_si (p++, 0, (mpfr_rnd_t) r++);
245 if (p != x[1] || r != 1)
246 {
247 printf ("Error in mpfr_set_si macro: p - x[0] = %d (expecting 1), "
248 "r = %d (expecting 1)\n", (int) (p - x[0]), r);
249 exit (1);
250 }
251 mpfr_clears (x[0], x[1], x[2], (mpfr_ptr) 0);
252 }
253
254 static void
255 test_macros_keyword (void)
256 {
257 mpfr_t x;
258 unsigned long i;
259
260 mpfr_init2 (x, 64);
261 #define MKN 0x1000000
262 #define long short
263 mpfr_set_ui (x, MKN, MPFR_RNDN);
264 #undef long
265 i = mpfr_get_ui (x, MPFR_RNDN);
266 if (i != MKN)
267 {
268 printf ("Error in test_macros_keyword: expected 0x%lx, got 0x%lx.\n",
269 (unsigned long) MKN, i);
270 exit (1);
271 }
272 mpfr_clear (x);
273 }
274
275 static void
276 test_get_ui_smallneg (void)
277 {
278 mpfr_t x;
279 int i;
280
281 mpfr_init2 (x, 64);
282
283 for (i = 1; i <= 4; i++)
284 {
285 int r;
286
287 mpfr_set_si_2exp (x, -i, -2, MPFR_RNDN);
288 RND_LOOP (r)
289 {
290 long s;
291 unsigned long u;
292
293 mpfr_clear_erangeflag ();
294 s = mpfr_get_si (x, r != MPFR_RNDF ? (mpfr_rnd_t) r : MPFR_RNDA);
295 if (mpfr_erangeflag_p ())
296 {
297 printf ("ERROR for get_si + ERANGE + small negative op"
298 " for rnd = %s and x = -%d/4\n",
299 mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
300 exit (1);
301 }
302 u = mpfr_get_ui (x, (mpfr_rnd_t) r);
303 if (u != 0)
304 {
305 printf ("ERROR for get_ui + ERANGE + small negative op"
306 " for rnd = %s and x = -%d/4\n",
307 mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
308 printf ("Expected 0, got %lu\n", u);
309 exit (1);
310 }
311 if ((s == 0) ^ !mpfr_erangeflag_p ())
312 {
313 const char *Not = s == 0 ? "" : " not";
314
315 printf ("ERROR for get_ui + ERANGE + small negative op"
316 " for rnd = %s and x = -%d/4\n",
317 mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
318 printf ("The rounding integer (%ld) is%s representable in "
319 "unsigned long,\nbut the erange flag is%s set.\n",
320 s, Not, Not);
321 exit (1);
322 }
323 }
324 }
325
326 mpfr_clear (x);
327 }
328
329 /* Test mpfr_get_si and mpfr_get_ui, on values around some particular
330 * integers (see ts[] and tu[]): x = t?[i] + j/4, where '?' is 's' or
331 * 'u', and j is an integer from -8 to 8.
332 */
333 static void get_tests (void)
334 {
335 mpfr_exp_t emin, emax;
336 mpfr_t x, z;
337 long ts[5] = { LONG_MIN, LONG_MAX, -17, 0, 17 };
338 unsigned long tu[3] = { 0, ULONG_MAX, 17 };
339 int s, i, j, odd, ctr = 0;
340 int inex;
341 int r;
342
343 emin = mpfr_get_emin ();
344 emax = mpfr_get_emax ();
345
346 /* We need the bitsize of an unsigned long + 3 bits (1 additional bit for
347 * the cases >= ULONG_MAX + 1; 2 additional bits for the fractional part).
348 */
349 mpfr_init2 (x, sizeof (unsigned long) * CHAR_BIT + 3);
350
351 mpfr_init2 (z, MPFR_PREC_MIN);
352 mpfr_set_ui_2exp (z, 1, -2, MPFR_RNDN); /* z = 1/4 */
353
354 for (s = 1; s >= 0; s--)
355 for (i = 0; i < (s ? 5 : 3); i++)
356 {
357 odd = (s ? (unsigned long) ts[i] : tu[i]) & 1;
358 inex = s ?
359 mpfr_set_si (x, ts[i], MPFR_RNDN) :
360 mpfr_set_ui (x, tu[i], MPFR_RNDN);
361 MPFR_ASSERTN (inex == 0);
362 inex = mpfr_sub_ui (x, x, 2, MPFR_RNDN);
363 MPFR_ASSERTN (inex == 0);
364 for (j = -8; j <= 8; j++)
365 {
366 /* Test x = t?[i] + j/4 in each non-RNDF rounding mode... */
367 RND_LOOP_NO_RNDF (r)
368 {
369 mpfr_flags_t ex_flags, flags;
370 int e, k, overflow;
371
372 ctr++; /* for the check below */
373
374 /* Let's determine k such that the rounded integer should
375 be t?[i] + k, assuming an unbounded exponent range. */
376 k = (j + 8 +
377 (MPFR_IS_LIKE_RNDD (r, MPFR_SIGN (x)) ? 0 :
378 MPFR_IS_LIKE_RNDU (r, MPFR_SIGN (x)) ? 3 :
379 2)) / 4 - 2;
380 if (r == MPFR_RNDN && ((unsigned int) j & 3) == 2 &&
381 ((odd + k) & 1))
382 k--; /* even rounding */
383
384 /* Overflow cases. Note that with the above choices:
385 _ t?[0] == minval(type)
386 _ t?[1] == maxval(type)
387 */
388 overflow = (i == 0 && k < 0) || (i == 1 && k > 0);
389
390 /* Expected flags. Note that in case of overflow, only the
391 erange flag is set. Otherwise, the result is inexact iff
392 j mod 1 != 0, i.e. the last two bits are not 00. */
393 ex_flags = overflow ? MPFR_FLAGS_ERANGE
394 : ((unsigned int) j & 3) != 0 ? MPFR_FLAGS_INEXACT : 0;
395
396 mpfr_clear_flags ();
397
398 #define GET_TESTS_TEST(TYPE,TZ,F,C,FMT) \
399 do { \
400 TYPE a, d; \
401 \
402 a = TZ[i] + (overflow ? 0 : k); \
403 if (e) \
404 { \
405 mpfr_exp_t ex; \
406 ex = MPFR_GET_EXP (x); \
407 set_emin (ex); \
408 set_emax (ex); \
409 } \
410 d = F (x, (mpfr_rnd_t) r); \
411 flags = __gmpfr_flags; \
412 set_emin (emin); \
413 set_emax (emax); \
414 if (flags != ex_flags || a != d) \
415 { \
416 printf ("Error in get_tests for " #F " on %s%s\n", \
417 mpfr_print_rnd_mode ((mpfr_rnd_t) r), \
418 e ? ", reduced exponent range" : ""); \
419 printf ("x = t" C "[%d] + (%d/4) = ", i, j); \
420 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); \
421 printf ("\n--> k = %d\n", k); \
422 printf ("Expected %l" FMT "\n", a); \
423 printf ("Got %l" FMT "\n", d); \
424 printf ("Expected flags:"); \
425 flags_out (ex_flags); \
426 printf ("Got flags: "); \
427 flags_out (flags); \
428 exit (1); \
429 } \
430 } while (0)
431
432 for (e = 0; e < 2; e++)
433 {
434 if (e && MPFR_IS_ZERO (x))
435 break;
436 if (s)
437 GET_TESTS_TEST (long,
438 ts, mpfr_get_si, "s", "d");
439 else
440 GET_TESTS_TEST (unsigned long,
441 tu, mpfr_get_ui, "u", "u");
442 }
443 }
444 inex = mpfr_add (x, x, z, MPFR_RNDN);
445 MPFR_ASSERTN (inex == 0);
446 }
447 }
448
449 /* Check that we have tested everything: 8 = 5 + 3 integers t?[i]
450 * with 17 = 8 - (-8) + 1 additional terms (j/4) for each integer,
451 * and each non-RNDF rounding mode.
452 */
453 MPFR_ASSERTN (ctr == 8 * 17 * ((int) MPFR_RND_MAX - 1));
454
455 mpfr_clear (x);
456 mpfr_clear (z);
457 }
458
459 /* FIXME: Comparing against mpfr_get_si/ui is not ideal, it'd be better to
460 have all tests examine the bits in mpfr_t for what should come out. */
461
462 int
463 main (int argc, char *argv[])
464 {
465 mpfr_t x;
466 long k, z, d, N;
467 unsigned long zl, dl;
468 int inex;
469 int r;
470 mpfr_exp_t emin, emax;
471 int flag;
472
473 tests_start_mpfr ();
474
475 get_tests ();
476
477 mpfr_init2 (x, 100);
478
479 N = (argc == 1) ? 100000 : atol (argv[1]);
480
481 for (k = 1; k <= N; k++)
482 {
483 z = (long) (randlimb () & LONG_MAX) + LONG_MIN / 2;
484 inex = mpfr_set_si (x, z, MPFR_RNDZ);
485 d = mpfr_get_si (x, MPFR_RNDZ);
486 if (d != z)
487 {
488 printf ("Error in mpfr_set_si: expected %ld got %ld\n", z, d);
489 exit (1);
490 }
491 if (inex)
492 {
493 printf ("Error in mpfr_set_si: inex value incorrect for %ld: %d\n",
494 z, inex);
495 exit (1);
496 }
497 }
498
499 for (k = 1; k <= N; k++)
500 {
501 zl = randlimb ();
502 inex = mpfr_set_ui (x, zl, MPFR_RNDZ);
503 dl = mpfr_get_ui (x, MPFR_RNDZ);
504 if (dl != zl)
505 {
506 printf ("Error in mpfr_set_ui: expected %lu got %lu\n", zl, dl);
507 exit (1);
508 }
509 if (inex)
510 {
511 printf ("Error in mpfr_set_ui: inex value incorrect for %lu: %d\n",
512 zl, inex);
513 exit (1);
514 }
515 }
516
517 mpfr_set_prec (x, 2);
518 if (mpfr_set_si (x, 5, MPFR_RNDZ) >= 0)
519 {
520 printf ("Wrong inexact flag for x=5, rnd=MPFR_RNDZ\n");
521 exit (1);
522 }
523
524 mpfr_set_prec (x, 2);
525 if (mpfr_set_si (x, -5, MPFR_RNDZ) <= 0)
526 {
527 printf ("Wrong inexact flag for x=-5, rnd=MPFR_RNDZ\n");
528 exit (1);
529 }
530
531 mpfr_set_prec (x, 3);
532 inex = mpfr_set_si (x, 77617, MPFR_RNDD); /* should be 65536 */
533 if (MPFR_MANT(x)[0] != MPFR_LIMB_HIGHBIT || inex >= 0)
534 {
535 printf ("Error in mpfr_set_si(x:3, 77617, MPFR_RNDD)\n");
536 mpfr_dump (x);
537 exit (1);
538 }
539 inex = mpfr_set_ui (x, 77617, MPFR_RNDD); /* should be 65536 */
540 if (MPFR_MANT(x)[0] != MPFR_LIMB_HIGHBIT || inex >= 0)
541 {
542 printf ("Error in mpfr_set_ui(x:3, 77617, MPFR_RNDD)\n");
543 mpfr_dump (x);
544 exit (1);
545 }
546
547 mpfr_set_prec (x, 2);
548 inex = mpfr_set_si (x, 33096, MPFR_RNDU);
549 if (mpfr_get_si (x, MPFR_RNDZ) != 49152 || inex <= 0)
550 {
551 printf ("Error in mpfr_set_si, exp. 49152, got %ld, inex %d\n",
552 mpfr_get_si (x, MPFR_RNDZ), inex);
553 exit (1);
554 }
555 inex = mpfr_set_ui (x, 33096, MPFR_RNDU);
556 if (mpfr_get_si (x, MPFR_RNDZ) != 49152)
557 {
558 printf ("Error in mpfr_set_ui, exp. 49152, got %ld, inex %d\n",
559 mpfr_get_si (x, MPFR_RNDZ), inex);
560 exit (1);
561 }
562 /* Also test the mpfr_set_ui function (instead of macro). */
563 inex = (mpfr_set_ui) (x, 33096, MPFR_RNDU);
564 if (mpfr_get_si (x, MPFR_RNDZ) != 49152)
565 {
566 printf ("Error in mpfr_set_ui function, exp. 49152, got %ld, inex %d\n",
567 mpfr_get_si (x, MPFR_RNDZ), inex);
568 exit (1);
569 }
570
571 for (r = 0 ; r < MPFR_RND_MAX ; r++)
572 {
573 mpfr_set_si (x, -1, (mpfr_rnd_t) r);
574 mpfr_set_ui (x, 0, (mpfr_rnd_t) r);
575 if (MPFR_IS_NEG (x) || mpfr_get_ui (x, (mpfr_rnd_t) r) != 0)
576 {
577 printf ("mpfr_set_ui (x, 0) gives -0 for %s\n",
578 mpfr_print_rnd_mode ((mpfr_rnd_t) r));
579 exit (1);
580 }
581
582 mpfr_set_si (x, -1, (mpfr_rnd_t) r);
583 mpfr_set_si (x, 0, (mpfr_rnd_t) r);
584 if (MPFR_IS_NEG (x) || mpfr_get_si (x, (mpfr_rnd_t) r) != 0)
585 {
586 printf ("mpfr_set_si (x, 0) gives -0 for %s\n",
587 mpfr_print_rnd_mode ((mpfr_rnd_t) r));
588 exit (1);
589 }
590 }
591
592 /* check potential bug in case mp_limb_t is unsigned */
593 emax = mpfr_get_emax ();
594 set_emax (0);
595 mpfr_set_si (x, -1, MPFR_RNDN);
596 if (mpfr_sgn (x) >= 0)
597 {
598 printf ("mpfr_set_si (x, -1) fails\n");
599 exit (1);
600 }
601 set_emax (emax);
602
603 emax = mpfr_get_emax ();
604 set_emax (5);
605 mpfr_set_prec (x, 2);
606 mpfr_set_si (x, -31, MPFR_RNDN);
607 if (mpfr_sgn (x) >= 0)
608 {
609 printf ("mpfr_set_si (x, -31) fails\n");
610 exit (1);
611 }
612 set_emax (emax);
613
614 /* test for get_ui */
615 mpfr_set_ui (x, 0, MPFR_RNDN);
616 MPFR_ASSERTN(mpfr_get_ui (x, MPFR_RNDN) == 0);
617 mpfr_set_ui (x, ULONG_MAX, MPFR_RNDU);
618 mpfr_nextabove (x);
619 mpfr_get_ui (x, MPFR_RNDU);
620
621 /* another test for get_ui */
622 mpfr_set_prec (x, 10);
623 mpfr_set_str_binary (x, "10.101");
624 dl = mpfr_get_ui (x, MPFR_RNDN);
625 MPFR_ASSERTN (dl == 3);
626
627 mpfr_set_str_binary (x, "-1.0");
628 mpfr_get_ui (x, MPFR_RNDN);
629
630 mpfr_set_str_binary (x, "0.1");
631 dl = mpfr_get_ui (x, MPFR_RNDN);
632 MPFR_ASSERTN (dl == 0);
633 dl = mpfr_get_ui (x, MPFR_RNDZ);
634 MPFR_ASSERTN (dl == 0);
635 dl = mpfr_get_ui (x, MPFR_RNDD);
636 MPFR_ASSERTN (dl == 0);
637 dl = mpfr_get_ui (x, MPFR_RNDU);
638 MPFR_ASSERTN (dl == 1);
639
640 /* coverage tests */
641 mpfr_set_prec (x, 2);
642 mpfr_set_si (x, -7, MPFR_RNDD);
643 MPFR_ASSERTN(mpfr_cmp_si (x, -8) == 0);
644 mpfr_set_prec (x, 2);
645 mpfr_set_ui (x, 7, MPFR_RNDU);
646 MPFR_ASSERTN(mpfr_cmp_ui (x, 8) == 0);
647 emax = mpfr_get_emax ();
648 set_emax (3);
649 mpfr_set_ui (x, 7, MPFR_RNDU);
650 MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
651 set_emax (1);
652 MPFR_ASSERTN( mpfr_set_ui (x, 7, MPFR_RNDU) );
653 MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
654 set_emax (emax);
655 mpfr_set_ui_2exp (x, 17, -50, MPFR_RNDN);
656 MPFR_ASSERTN (mpfr_get_ui (x, MPFR_RNDD) == 0);
657 MPFR_ASSERTN (mpfr_get_si (x, MPFR_RNDD) == 0);
658
659 /* Test for ERANGE flag + correct behavior if overflow */
660 mpfr_set_prec (x, 256);
661 mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN);
662 mpfr_clear_erangeflag ();
663 dl = mpfr_get_ui (x, MPFR_RNDN);
664 if (dl != ULONG_MAX || mpfr_erangeflag_p ())
665 {
666 printf ("ERROR for get_ui + ERANGE + ULONG_MAX (1)\n");
667 exit (1);
668 }
669 mpfr_add_ui (x, x, 1, MPFR_RNDN);
670 dl = mpfr_get_ui (x, MPFR_RNDN);
671 if (dl != ULONG_MAX || !mpfr_erangeflag_p ())
672 {
673 printf ("ERROR for get_ui + ERANGE + ULONG_MAX (2)\n");
674 exit (1);
675 }
676 mpfr_set_si (x, -1, MPFR_RNDN);
677 mpfr_clear_erangeflag ();
678 dl = mpfr_get_ui (x, MPFR_RNDN);
679 if (dl != 0 || !mpfr_erangeflag_p ())
680 {
681 printf ("ERROR for get_ui + ERANGE + -1 \n");
682 exit (1);
683 }
684 mpfr_set_si (x, LONG_MAX, MPFR_RNDN);
685 mpfr_clear_erangeflag ();
686 d = mpfr_get_si (x, MPFR_RNDN);
687 if (d != LONG_MAX || mpfr_erangeflag_p ())
688 {
689 printf ("ERROR for get_si + ERANGE + LONG_MAX (1): %ld\n", d);
690 exit (1);
691 }
692 mpfr_add_ui (x, x, 1, MPFR_RNDN);
693 d = mpfr_get_si (x, MPFR_RNDN);
694 if (d != LONG_MAX || !mpfr_erangeflag_p ())
695 {
696 printf ("ERROR for get_si + ERANGE + LONG_MAX (2)\n");
697 exit (1);
698 }
699 mpfr_set_si (x, LONG_MIN, MPFR_RNDN);
700 mpfr_clear_erangeflag ();
701 d = mpfr_get_si (x, MPFR_RNDN);
702 if (d != LONG_MIN || mpfr_erangeflag_p ())
703 {
704 printf ("ERROR for get_si + ERANGE + LONG_MIN (1)\n");
705 exit (1);
706 }
707 mpfr_sub_ui (x, x, 1, MPFR_RNDN);
708 d = mpfr_get_si (x, MPFR_RNDN);
709 if (d != LONG_MIN || !mpfr_erangeflag_p ())
710 {
711 printf ("ERROR for get_si + ERANGE + LONG_MIN (2)\n");
712 exit (1);
713 }
714
715 mpfr_set_nan (x);
716 mpfr_clear_flags ();
717 d = mpfr_get_ui (x, MPFR_RNDN);
718 if (d != 0 || __gmpfr_flags != MPFR_FLAGS_ERANGE)
719 {
720 printf ("ERROR for get_ui + NaN\n");
721 exit (1);
722 }
723 mpfr_clear_erangeflag ();
724 d = mpfr_get_si (x, MPFR_RNDN);
725 if (d != 0 || __gmpfr_flags != MPFR_FLAGS_ERANGE)
726 {
727 printf ("ERROR for get_si + NaN\n");
728 exit (1);
729 }
730
731 emin = mpfr_get_emin ();
732 mpfr_set_prec (x, 2);
733
734 mpfr_set_emin (4);
735 mpfr_clear_flags ();
736 mpfr_set_ui (x, 7, MPFR_RNDU);
737 flag = mpfr_underflow_p ();
738 mpfr_set_emin (emin);
739 if (mpfr_cmp_ui (x, 8) != 0)
740 {
741 printf ("Error for mpfr_set_ui (x, 7, MPFR_RNDU), prec = 2, emin = 4\n");
742 exit (1);
743 }
744 if (flag)
745 {
746 printf ("mpfr_set_ui (x, 7, MPFR_RNDU) should not underflow "
747 "with prec = 2, emin = 4\n");
748 exit (1);
749 }
750
751 mpfr_set_emin (4);
752 mpfr_clear_flags ();
753 mpfr_set_si (x, -7, MPFR_RNDD);
754 flag = mpfr_underflow_p ();
755 mpfr_set_emin (emin);
756 if (mpfr_cmp_si (x, -8) != 0)
757 {
758 printf ("Error for mpfr_set_si (x, -7, MPFR_RNDD), prec = 2, emin = 4\n");
759 exit (1);
760 }
761 if (flag)
762 {
763 printf ("mpfr_set_si (x, -7, MPFR_RNDD) should not underflow "
764 "with prec = 2, emin = 4\n");
765 exit (1);
766 }
767
768 mpfr_clear (x);
769
770 test_2exp ();
771 test_2exp_extreme ();
772 test_macros ();
773 test_macros_keyword ();
774 test_get_ui_smallneg ();
775 tests_end_mpfr ();
776 return 0;
777 }
778