1
/* Test file for mpfr_set_si and mpfr_set_ui.
3
Copyright 1999, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5
This file is part of the MPFR Library.
7
The MPFR 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 2.1 of the License, or (at your
10
option) any later version.
12
The MPFR 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 MPFR Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
20
MA 02110-1301, USA. */
27
#include "mpfr-test.h"
29
#define ERROR(str) {printf("Error for "str"\n"); exit(1);}
39
mpfr_set_ui_2exp (x, 1, 0, GMP_RNDN);
40
if (mpfr_cmp_ui(x, 1))
43
mpfr_set_ui_2exp (x, 1024, -10, GMP_RNDN);
44
if (mpfr_cmp_ui(x, 1))
47
mpfr_set_ui_2exp (x, 1024, 10, GMP_RNDN);
48
if (mpfr_cmp_ui(x, 1024*1024))
51
mpfr_set_si_2exp (x, -1024*1024, -10, GMP_RNDN);
52
if (mpfr_cmp_si(x, -1024))
55
mpfr_set_ui_2exp (x, 0x92345678, 16, GMP_RNDN);
56
if (mpfr_cmp_str (x, "92345678@4", 16, GMP_RNDN))
57
ERROR("(x92345678U,+16)");
59
mpfr_set_si_2exp (x, -0x1ABCDEF0, -256, GMP_RNDN);
60
if (mpfr_cmp_str (x, "-1ABCDEF0@-64", 16, GMP_RNDN))
61
ERROR("(-x1ABCDEF0,-256)");
64
res = mpfr_set_si_2exp (x, 7, 10, GMP_RNDU);
65
if (mpfr_cmp_ui (x, 1<<13) || res <= 0)
66
ERROR ("Prec 2 + si_2exp");
68
res = mpfr_set_ui_2exp (x, 7, 10, GMP_RNDU);
69
if (mpfr_cmp_ui (x, 1<<13) || res <= 0)
70
ERROR ("Prec 2 + ui_2exp");
82
mpfr_inits (x[0], x[1], x[2], NULL);
85
mpfr_set_ui (p++, 0, r++);
86
if (p != x[1] || r != 1)
88
printf ("Error in mpfr_set_ui macro: p - x[0] = %d (expecting 1), "
89
"r = %d (expecting 1)\n", (int) (p - x[0]), r);
94
mpfr_set_si (p++, 0, r++);
95
if (p != x[1] || r != 1)
97
printf ("Error in mpfr_set_si macro: p - x[0] = %d (expecting 1), "
98
"r = %d (expecting 1)\n", (int) (p - x[0]), r);
101
mpfr_clears (x[0], x[1], x[2], NULL);
104
/* FIXME: Comparing against mpfr_get_si/ui is not ideal, it'd be better to
105
have all tests examine the bits in mpfr_t for what should come out. */
108
main (int argc, char *argv[])
112
unsigned long zl, dl;
121
N = (argc==1) ? 100000 : atol (argv[1]);
123
for (k = 1; k <= N; k++)
125
z = (long) (randlimb () & LONG_MAX) + LONG_MIN / 2;
126
inex = mpfr_set_si (x, z, GMP_RNDZ);
127
d = mpfr_get_si (x, GMP_RNDZ);
130
printf ("Error in mpfr_set_si: expected %ld got %ld\n", z, d);
135
printf ("Error in mpfr_set_si: inex value incorrect for %ld: %d\n",
141
for (k = 1; k <= N; k++)
144
inex = mpfr_set_ui (x, zl, GMP_RNDZ);
145
dl = mpfr_get_ui (x, GMP_RNDZ);
148
printf ("Error in mpfr_set_ui: expected %lu got %lu\n", zl, dl);
153
printf ("Error in mpfr_set_ui: inex value incorrect for %lu: %d\n",
159
mpfr_set_prec (x, 2);
160
if (mpfr_set_si (x, 5, GMP_RNDZ) >= 0)
162
printf ("Wrong inexact flag for x=5, rnd=GMP_RNDZ\n");
166
mpfr_set_prec (x, 2);
167
if (mpfr_set_si (x, -5, GMP_RNDZ) <= 0)
169
printf ("Wrong inexact flag for x=-5, rnd=GMP_RNDZ\n");
173
mpfr_set_prec (x, 3);
174
inex = mpfr_set_si (x, 77617, GMP_RNDD); /* should be 65536 */
175
if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1))
178
printf ("Error in mpfr_set_si(x:3, 77617, GMP_RNDD)\n");
179
mpfr_print_binary (x);
183
inex = mpfr_set_ui (x, 77617, GMP_RNDD); /* should be 65536 */
184
if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1))
187
printf ("Error in mpfr_set_ui(x:3, 77617, GMP_RNDD)\n");
188
mpfr_print_binary (x);
193
mpfr_set_prec (x, 2);
194
inex = mpfr_set_si (x, 33096, GMP_RNDU);
195
if (mpfr_get_si (x, GMP_RNDZ) != 49152 || inex <= 0)
197
printf ("Error in mpfr_set_si, exp. 49152, got %ld, inex %d\n",
198
mpfr_get_si (x, GMP_RNDZ), inex);
201
inex = mpfr_set_ui (x, 33096, GMP_RNDU);
202
if (mpfr_get_si (x, GMP_RNDZ) != 49152)
204
printf ("Error in mpfr_set_ui, exp. 49152, got %ld, inex %d\n",
205
mpfr_get_si (x, GMP_RNDZ), inex);
209
for (r = 0 ; r < GMP_RND_MAX ; r++)
211
mpfr_set_si (x, -1, (mp_rnd_t) r);
212
mpfr_set_ui (x, 0, (mp_rnd_t) r);
213
if (MPFR_IS_NEG (x) || mpfr_get_ui (x, (mp_rnd_t) r) != 0)
215
printf ("mpfr_set_ui (x, 0) gives -0 for %s\n",
216
mpfr_print_rnd_mode ((mp_rnd_t) r));
220
mpfr_set_si (x, -1, (mp_rnd_t) r);
221
mpfr_set_si (x, 0, (mp_rnd_t) r);
222
if (MPFR_IS_NEG (x) || mpfr_get_si (x, (mp_rnd_t) r) != 0)
224
printf ("mpfr_set_si (x, 0) gives -0 for %s\n",
225
mpfr_print_rnd_mode ((mp_rnd_t) r));
230
/* check potential bug in case mp_limb_t is unsigned */
231
emax = mpfr_get_emax ();
233
mpfr_set_si (x, -1, GMP_RNDN);
234
if (mpfr_sgn (x) >= 0)
236
printf ("mpfr_set_si (x, -1) fails\n");
241
emax = mpfr_get_emax ();
243
mpfr_set_prec (x, 2);
244
mpfr_set_si (x, -31, GMP_RNDN);
245
if (mpfr_sgn (x) >= 0)
247
printf ("mpfr_set_si (x, -31) fails\n");
252
/* test for get_ui */
253
mpfr_set_ui (x, 0, GMP_RNDN);
254
MPFR_ASSERTN(mpfr_get_ui (x, GMP_RNDN) == 0);
255
mpfr_set_ui (x, ULONG_MAX, GMP_RNDU);
257
mpfr_get_ui (x, GMP_RNDU);
259
/* another test for get_ui */
260
mpfr_set_prec (x, 10);
261
mpfr_set_str_binary (x, "10.101");
262
dl = mpfr_get_ui (x, GMP_RNDN);
263
MPFR_ASSERTN (dl == 3);
265
mpfr_set_str_binary (x, "-1.0");
266
mpfr_get_ui (x, GMP_RNDN);
268
mpfr_set_str_binary (x, "0.1");
269
dl = mpfr_get_ui (x, GMP_RNDN);
270
MPFR_ASSERTN (dl == 0);
271
dl = mpfr_get_ui (x, GMP_RNDZ);
272
MPFR_ASSERTN (dl == 0);
273
dl = mpfr_get_ui (x, GMP_RNDD);
274
MPFR_ASSERTN (dl == 0);
275
dl = mpfr_get_ui (x, GMP_RNDU);
276
MPFR_ASSERTN (dl == 1);
279
mpfr_set_prec (x, 2);
280
mpfr_set_si (x, -7, GMP_RNDD);
281
MPFR_ASSERTN(mpfr_cmp_si (x, -8) == 0);
282
mpfr_set_prec (x, 2);
283
mpfr_set_ui (x, 7, GMP_RNDU);
284
MPFR_ASSERTN(mpfr_cmp_ui (x, 8) == 0);
285
emax = mpfr_get_emax ();
287
mpfr_set_ui (x, 7, GMP_RNDU);
288
MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
290
MPFR_ASSERTN( mpfr_set_ui (x, 7, GMP_RNDU) );
291
MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
293
mpfr_set_ui_2exp (x, 17, -50, GMP_RNDN);
294
MPFR_ASSERTN (mpfr_get_ui (x, GMP_RNDD) == 0);
295
MPFR_ASSERTN (mpfr_get_si (x, GMP_RNDD) == 0);
297
/* Test for ERANGE flag + correct behaviour if overflow */
298
mpfr_set_prec (x, 256);
299
mpfr_set_ui (x, ULONG_MAX, GMP_RNDN);
300
mpfr_clear_erangeflag ();
301
dl = mpfr_get_ui (x, GMP_RNDN);
302
if (dl != ULONG_MAX || mpfr_erangeflag_p ())
304
printf ("ERROR for get_ui + ERANGE + ULONG_MAX (1)\n");
307
mpfr_add_ui (x, x, 1, GMP_RNDN);
308
dl = mpfr_get_ui (x, GMP_RNDN);
309
if (dl != ULONG_MAX || !mpfr_erangeflag_p ())
311
printf ("ERROR for get_ui + ERANGE + ULONG_MAX (2)\n");
314
mpfr_set_si (x, -1, GMP_RNDN);
315
mpfr_clear_erangeflag ();
316
dl = mpfr_get_ui (x, GMP_RNDN);
317
if (dl != 0 || !mpfr_erangeflag_p ())
319
printf ("ERROR for get_ui + ERANGE + -1 \n");
322
mpfr_set_si (x, LONG_MAX, GMP_RNDN);
323
mpfr_clear_erangeflag ();
324
d = mpfr_get_si (x, GMP_RNDN);
325
if (d != LONG_MAX || mpfr_erangeflag_p ())
327
printf ("ERROR for get_si + ERANGE + LONG_MAX (1): %ld\n", d);
330
mpfr_add_ui (x, x, 1, GMP_RNDN);
331
d = mpfr_get_si (x, GMP_RNDN);
332
if (d != LONG_MAX || !mpfr_erangeflag_p ())
334
printf ("ERROR for get_si + ERANGE + LONG_MAX (2)\n");
337
mpfr_set_si (x, LONG_MIN, GMP_RNDN);
338
mpfr_clear_erangeflag ();
339
d = mpfr_get_si (x, GMP_RNDN);
340
if (d != LONG_MIN || mpfr_erangeflag_p ())
342
printf ("ERROR for get_si + ERANGE + LONG_MIN (1)\n");
345
mpfr_sub_ui (x, x, 1, GMP_RNDN);
346
d = mpfr_get_si (x, GMP_RNDN);
347
if (d != LONG_MIN || !mpfr_erangeflag_p ())
349
printf ("ERROR for get_si + ERANGE + LONG_MIN (2)\n");