1 /* Test mpz_cmp_d and mpz_cmpabs_d.
3 Copyright 2001, 2002, 2003, 2005 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/. */
29 /* FIXME: Not sure if the tests here are exhaustive. Ought to try to get
30 each possible exit from mpz_cmp_d (and mpz_cmpabs_d) exercised. */
33 #define SGN(n) ((n) > 0 ? 1 : (n) < 0 ? -1 : 0)
37 check_one (const char *name, mpz_srcptr x, double y, int cmp, int cmpabs)
41 got = mpz_cmp_d (x, y);
45 printf ("mpz_cmp_d wrong (from %s)\n", name);
46 printf (" got %d\n", got);
47 printf (" want %d\n", cmp);
50 printf (" y %g\n", y);
53 printf (" y %g\n", y);
55 for (i = 0; i < sizeof(y); i++)
56 printf (" %02X", (unsigned) ((unsigned char *) &y)[i]);
61 got = mpz_cmpabs_d (x, y);
62 if (SGN(got) != cmpabs)
64 printf ("mpz_cmpabs_d wrong\n");
65 printf (" got %d\n", got);
66 printf (" want %d\n", cmpabs);
90 { "0x1000000000000000000000000000000000000000000000000", 0.0, 1, 1 },
91 { "-0x1000000000000000000000000000000000000000000000000", 0.0, -1, 1 },
93 { "0", 1e100, -1, -1 },
94 { "0", -1e100, 1, -1 },
99 { "-2", -1.5, -1, 1 },
107 for (i = 0; i < numberof (data); i++)
109 mpz_set_str_or_abort (x, data[i].x, 0);
110 check_one ("check_data", x, data[i].y, data[i].cmp, data[i].cmpabs);
117 /* Equality of integers with up to 53 bits */
125 mpz_init_set_ui (x, 0L);
128 for (i = 0; i < 512; i++)
130 mpz_mul_2exp (x, x, 1);
131 mpz_add_ui (x, x, 1L);
136 /* stop if any truncation is occurring */
137 if (mpz_cmp (x, x2) != 0)
140 check_one ("check_onebits", x, y, 0, 0);
141 check_one ("check_onebits", x, -y, 1, 0);
143 check_one ("check_onebits", x, y, -1, 0);
144 check_one ("check_onebits", x, -y, 0, 0);
153 /* With the mpz differing by 1, in a limb position possibly below the double */
155 check_low_z_one (void)
163 /* FIXME: It'd be better to base this on the float format. */
165 #define LIM 127 /* vax fp numbers have limited range */
170 for (i = 1; i < LIM; i++)
173 mpz_mul_2exp (x, x, i);
176 check_one ("check_low_z_one", x, y, 0, 0);
177 check_one ("check_low_z_one", x, -y, 1, 0);
179 check_one ("check_low_z_one", x, y, -1, 0);
180 check_one ("check_low_z_one", x, -y, 0, 0);
183 mpz_sub_ui (x, x, 1);
185 check_one ("check_low_z_one", x, y, -1, -1);
186 check_one ("check_low_z_one", x, -y, 1, -1);
188 check_one ("check_low_z_one", x, y, -1, -1);
189 check_one ("check_low_z_one", x, -y, 1, -1);
192 mpz_add_ui (x, x, 2);
194 check_one ("check_low_z_one", x, y, 1, 1);
195 check_one ("check_low_z_one", x, -y, 1, 1);
197 check_one ("check_low_z_one", x, y, -1, 1);
198 check_one ("check_low_z_one", x, -y, -1, 1);
205 /* Comparing 1 and 1+2^-n. "y" is volatile to make gcc store and fetch it,
206 which forces it to a 64-bit double, whereas on x86 it would otherwise
207 remain on the float stack as an 80-bit long double. */
209 check_one_2exp (void)
219 for (i = 0; i < 128; i++)
227 check_one ("check_one_2exp", x, y, -1, -1);
228 check_one ("check_one_2exp", x, -y, 1, -1);
231 check_one ("check_one_2exp", x, y, -1, -1);
232 check_one ("check_one_2exp", x, -y, 1, -1);
239 check_infinity (void)
242 double y = tests_infinity_d ();
250 check_one ("check_infinity", x, y, -1, -1);
251 check_one ("check_infinity", x, -y, 1, -1);
254 mpz_set_ui (x, 123L);
255 check_one ("check_infinity", x, y, -1, -1);
256 check_one ("check_infinity", x, -y, 1, -1);
259 mpz_set_si (x, -123L);
260 check_one ("check_infinity", x, y, -1, -1);
261 check_one ("check_infinity", x, -y, 1, -1);
265 mpz_mul_2exp (x, x, 5000L);
266 check_one ("check_infinity", x, y, -1, -1);
267 check_one ("check_infinity", x, -y, 1, -1);
269 /* -2^5000 cmp inf */
271 check_one ("check_infinity", x, y, -1, -1);
272 check_one ("check_infinity", x, -y, 1, -1);
278 main (int argc, char *argv[])