1 /* Test mpz_set_d and mpz_init_set_d.
3 Copyright 2000, 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
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.
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.
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/. */
33 mp_limb_t want_data[2];
40 { 123.0, 1, { 123 } },
41 { -123.0, -1, { 123 } },
45 { 2.328306436538696e-10, 0, { 0 } },
46 { -2.328306436538696e-10, 0, { 0 } },
47 { 5.421010862427522e-20, 0, { 0 } },
48 { -5.421010862427522e-20, 0, { 0 } },
49 { 2.938735877055719e-39, 0, { 0 } },
50 { -2.938735877055719e-39, 0, { 0 } },
56 for (i = 0; i < numberof (data); i++)
59 mpz_set_d (z, data[i].d);
61 if (z->_mp_size != data[i].want_size
62 || refmpn_cmp_allowzero (z->_mp_d, data[i].want_data,
63 ABS (data[i].want_size)) != 0)
65 printf ("mpz_set_d wrong on data[%d]\n", i);
67 d_trace (" d ", data[i].d);
68 printf (" got size %ld\n", (long) z->_mp_size);
69 printf (" want size %ld\n", (long) data[i].want_size);
70 mpn_trace (" got z", z->_mp_d, z->_mp_size);
71 mpn_trace (" want z", data[i].want_data, data[i].want_size);
76 mpz_init_set_d (z, data[i].d);
78 if (z->_mp_size != data[i].want_size
79 || refmpn_cmp_allowzero (z->_mp_d, data[i].want_data,
80 ABS (data[i].want_size)) != 0)
82 printf ("mpz_init_set_d wrong on data[%d]\n", i);
89 /* Try mpz_set_d on values 2^i+1, while such a value fits a double. */
91 check_2n_plus_1 (void)
93 volatile double p, d, diff;
101 mpz_set_ui (want, 2L); /* gives 3 on first step */
103 for (i = 1; i < 500; i++)
105 mpz_mul_2exp (want, want, 1L);
106 mpz_sub_ui (want, want, 1L); /* want = 2^i+1 */
108 p *= 2.0; /* p = 2^i */
112 break; /* rounding occurred, stop now */
115 MPZ_CHECK_FORMAT (got);
116 if (mpz_cmp (got, want) != 0)
118 printf ("mpz_set_d wrong on 2^%d+1\n", i);
120 mpz_trace (" got ", got);
121 mpz_trace (" want ", want);