1 /* Test mpz_cmp_d and mpz_cmpabs_d.
3 Copyright 2001-2003, 2005, 2013 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/. */
22 #include "testutils.h"
24 /* FIXME: Not sure if the tests here are exhaustive. Ought to try to get
25 each possible exit from mpz_cmp_d (and mpz_cmpabs_d) exercised. */
28 #define SGN(n) ((n) > 0 ? 1 : (n) < 0 ? -1 : 0)
32 check_one (const char *name, mpz_srcptr x, double y, int cmp, int cmpabs)
36 got = mpz_cmp_d (x, y);
40 printf ("mpz_cmp_d wrong (from %s)\n", name);
41 printf (" got %d\n", got);
42 printf (" want %d\n", cmp);
45 mpz_out_str (stdout, 10, x);
46 printf ("\n y %g\n", y);
48 mpz_out_str (stdout, -16, x);
49 printf ("\n y %g\n", y);
51 for (i = 0; i < sizeof(y); i++)
52 printf (" %02X", (unsigned) ((unsigned char *) &y)[i]);
57 got = mpz_cmpabs_d (x, y);
58 if (SGN(got) != cmpabs)
60 printf ("mpz_cmpabs_d wrong\n");
61 printf (" got %d\n", got);
62 printf (" want %d\n", cmpabs);
83 { "-1", -0.5, -1, 1 },
88 { "0x1000000000000000000000000000000000000000000000000", 1.0, 1, 1 },
89 { "-0x1000000000000000000000000000000000000000000000000", 1.0, -1, 1 },
91 { "0", 1e100, -1, -1 },
92 { "0", -1e100, 1, -1 },
97 { "-2", -1.5, -1, 1 },
105 for (i = 0; i < numberof (data); i++)
107 mpz_set_str_or_abort (x, data[i].x, 0);
108 check_one ("check_data", x, data[i].y, data[i].cmp, data[i].cmpabs);
115 /* Equality of integers with up to 53 bits */
123 mpz_init_set_ui (x, 0L);
126 for (i = 0; i < 512; i++)
128 mpz_mul_2exp (x, x, 1);
129 mpz_add_ui (x, x, 1L);
134 /* stop if any truncation is occurring */
135 if (mpz_cmp (x, x2) != 0)
138 check_one ("check_onebits", x, y, 0, 0);
139 check_one ("check_onebits", x, -y, 1, 0);
141 check_one ("check_onebits", x, y, -1, 0);
142 check_one ("check_onebits", x, -y, 0, 0);
151 /* With the mpz differing by 1, in a limb position possibly below the double */
153 check_low_z_one (void)
161 /* FIXME: It'd be better to base this on the float format. */
162 #if defined (__vax) || defined (__vax__)
163 #define LIM 127 /* vax fp numbers have limited range */
168 for (i = 1; i < LIM; i++)
171 mpz_mul_2exp (x, x, i);
174 check_one ("check_low_z_one", x, y, 0, 0);
175 check_one ("check_low_z_one", x, -y, 1, 0);
177 check_one ("check_low_z_one", x, y, -1, 0);
178 check_one ("check_low_z_one", x, -y, 0, 0);
181 mpz_sub_ui (x, x, 1);
183 check_one ("check_low_z_one", x, y, -1, -1);
184 check_one ("check_low_z_one", x, -y, 1, -1);
186 check_one ("check_low_z_one", x, y, -1, -1);
187 check_one ("check_low_z_one", x, -y, 1, -1);
190 mpz_add_ui (x, x, 2);
192 check_one ("check_low_z_one", x, y, 1, 1);
193 check_one ("check_low_z_one", x, -y, 1, 1);
195 check_one ("check_low_z_one", x, y, -1, 1);
196 check_one ("check_low_z_one", x, -y, -1, 1);
203 /* Comparing 1 and 1+2^-n. "y" is volatile to make gcc store and fetch it,
204 which forces it to a 64-bit double, whereas on x86 it would otherwise
205 remain on the float stack as an 80-bit long double. */
207 check_one_2exp (void)
217 for (i = 0; i < 128; i++)
225 check_one ("check_one_2exp", x, y, -1, -1);
226 check_one ("check_one_2exp", x, -y, 1, -1);
229 check_one ("check_one_2exp", x, y, -1, -1);
230 check_one ("check_one_2exp", x, -y, 1, -1);
237 check_infinity (void)
248 check_one ("check_infinity", x, y, -1, -1);
249 check_one ("check_infinity", x, -y, 1, -1);
252 mpz_set_ui (x, 123L);
253 check_one ("check_infinity", x, y, -1, -1);
254 check_one ("check_infinity", x, -y, 1, -1);
257 mpz_set_si (x, -123L);
258 check_one ("check_infinity", x, y, -1, -1);
259 check_one ("check_infinity", x, -y, 1, -1);
263 mpz_mul_2exp (x, x, 5000L);
264 check_one ("check_infinity", x, y, -1, -1);
265 check_one ("check_infinity", x, -y, 1, -1);
267 /* -2^5000 cmp inf */
269 check_one ("check_infinity", x, y, -1, -1);
270 check_one ("check_infinity", x, -y, 1, -1);
276 testmain (int argc, char *argv[])