1 /* Test mpz_cmp_d and mpz_cmpabs_d.
3 Copyright 2001-2003, 2005 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library test suite.
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 Public License for more details.
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite. If not, see https://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);
88 { "-1", -0.5, -1, 1 },
93 { "0x1000000000000000000000000000000000000000000000000", 1.0, 1, 1 },
94 { "-0x1000000000000000000000000000000000000000000000000", 1.0, -1, 1 },
96 { "0", 1e100, -1, -1 },
97 { "0", -1e100, 1, -1 },
101 { "-2", 1.5, -1, 1 },
102 { "-2", -1.5, -1, 1 },
110 for (i = 0; i < numberof (data); i++)
112 mpz_set_str_or_abort (x, data[i].x, 0);
113 check_one ("check_data", x, data[i].y, data[i].cmp, data[i].cmpabs);
120 /* Equality of integers with up to 53 bits */
128 mpz_init_set_ui (x, 0L);
131 for (i = 0; i < 512; i++)
133 mpz_mul_2exp (x, x, 1);
134 mpz_add_ui (x, x, 1L);
139 /* stop if any truncation is occurring */
140 if (mpz_cmp (x, x2) != 0)
143 check_one ("check_onebits", x, y, 0, 0);
144 check_one ("check_onebits", x, -y, 1, 0);
146 check_one ("check_onebits", x, y, -1, 0);
147 check_one ("check_onebits", x, -y, 0, 0);
156 /* With the mpz differing by 1, in a limb position possibly below the double */
158 check_low_z_one (void)
166 /* FIXME: It'd be better to base this on the float format. */
167 #if defined (__vax) || defined (__vax__)
168 #define LIM 127 /* vax fp numbers have limited range */
173 for (i = 1; i < LIM; i++)
176 mpz_mul_2exp (x, x, i);
179 check_one ("check_low_z_one", x, y, 0, 0);
180 check_one ("check_low_z_one", x, -y, 1, 0);
182 check_one ("check_low_z_one", x, y, -1, 0);
183 check_one ("check_low_z_one", x, -y, 0, 0);
186 mpz_sub_ui (x, x, 1);
188 check_one ("check_low_z_one", x, y, -1, -1);
189 check_one ("check_low_z_one", x, -y, 1, -1);
191 check_one ("check_low_z_one", x, y, -1, -1);
192 check_one ("check_low_z_one", x, -y, 1, -1);
195 mpz_add_ui (x, x, 2);
197 check_one ("check_low_z_one", x, y, 1, 1);
198 check_one ("check_low_z_one", x, -y, 1, 1);
200 check_one ("check_low_z_one", x, y, -1, 1);
201 check_one ("check_low_z_one", x, -y, -1, 1);
208 /* Comparing 1 and 1+2^-n. "y" is volatile to make gcc store and fetch it,
209 which forces it to a 64-bit double, whereas on x86 it would otherwise
210 remain on the float stack as an 80-bit long double. */
212 check_one_2exp (void)
222 for (i = 0; i < 128; i++)
230 check_one ("check_one_2exp", x, y, -1, -1);
231 check_one ("check_one_2exp", x, -y, 1, -1);
234 check_one ("check_one_2exp", x, y, -1, -1);
235 check_one ("check_one_2exp", x, -y, 1, -1);
242 check_infinity (void)
245 double y = tests_infinity_d ();
253 check_one ("check_infinity", x, y, -1, -1);
254 check_one ("check_infinity", x, -y, 1, -1);
257 mpz_set_ui (x, 123L);
258 check_one ("check_infinity", x, y, -1, -1);
259 check_one ("check_infinity", x, -y, 1, -1);
262 mpz_set_si (x, -123L);
263 check_one ("check_infinity", x, y, -1, -1);
264 check_one ("check_infinity", x, -y, 1, -1);
268 mpz_mul_2exp (x, x, 5000L);
269 check_one ("check_infinity", x, y, -1, -1);
270 check_one ("check_infinity", x, -y, 1, -1);
272 /* -2^5000 cmp inf */
274 check_one ("check_infinity", x, y, -1, -1);
275 check_one ("check_infinity", x, -y, 1, -1);
281 main (int argc, char *argv[])