1 /* Test file for mpfr_cmp_ui and mpfr_cmp_si.
3 Copyright 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #ifdef TCMP_UI_CHECK_NAN
25 mpfr_clear_erangeflag ();
26 c = mpfr_cmp_si (x, TCMP_UI_CHECK_NAN);
27 if (c != 0 || !mpfr_erangeflag_p ())
29 printf ("NaN error on %d (1)\n", TCMP_UI_CHECK_NAN);
32 mpfr_clear_erangeflag ();
33 c = (mpfr_cmp_si) (x, TCMP_UI_CHECK_NAN);
34 if (c != 0 || !mpfr_erangeflag_p ())
36 printf ("NaN error on %d (2)\n", TCMP_UI_CHECK_NAN);
39 if (TCMP_UI_CHECK_NAN >= 0)
41 mpfr_clear_erangeflag ();
42 c = mpfr_cmp_ui (x, TCMP_UI_CHECK_NAN);
43 if (c != 0 || !mpfr_erangeflag_p ())
45 printf ("NaN error on %d (3)\n", TCMP_UI_CHECK_NAN);
48 mpfr_clear_erangeflag ();
49 c = (mpfr_cmp_ui) (x, TCMP_UI_CHECK_NAN);
50 if (c != 0 || !mpfr_erangeflag_p ())
52 printf ("NaN error on %d (4)\n", TCMP_UI_CHECK_NAN);
62 #include "mpfr-test.h"
72 /* We need constants to completely test the macros. */
73 #undef TCMP_UI_CHECK_NAN
74 #define TCMP_UI_CHECK_NAN -17
76 #undef TCMP_UI_CHECK_NAN
77 #define TCMP_UI_CHECK_NAN 0
79 #undef TCMP_UI_CHECK_NAN
80 #define TCMP_UI_CHECK_NAN 17
82 for (i = -17; i <= 17; i += 17)
84 #undef TCMP_UI_CHECK_NAN
85 #define TCMP_UI_CHECK_NAN i
91 /* Since mpfr_cmp_ui and mpfr_cmp_si are also implemented by a macro
92 with __builtin_constant_p for GCC, check that side effects are
103 mpfr_set_ui (x, 17, MPFR_RNDN);
104 if (mpfr_cmp_ui (x, 17) != 0)
106 printf ("Error 1 on mpfr_cmp_ui(x,17) in check_macros\n");
109 if (mpfr_cmp_ui (x, (c++, 17)) != 0)
111 printf ("Error 2 on mpfr_cmp_ui(x,17) in check_macros\n");
116 printf ("Error 3 on mpfr_cmp_ui(x,17) in check_macros\n"
117 "(c = %d instead of 1)\n", c);
120 if (mpfr_cmp_si (x, 17) != 0)
122 printf ("Error 1 on mpfr_cmp_si(x,17) in check_macros\n");
125 if (mpfr_cmp_si (x, (c++, 17)) != 0)
127 printf ("Error 2 on mpfr_cmp_si(x,17) in check_macros\n");
132 printf ("Error 3 on mpfr_cmp_si(x,17) in check_macros\n"
133 "(c = %d instead of 2)\n", c);
138 mpfr_set_ui (x, 0, MPFR_RNDN);
139 if (mpfr_cmp_ui (x, 0) != 0)
141 printf ("Error 1 on mpfr_cmp_ui(x,0) in check_macros\n");
144 if (mpfr_cmp_ui (x, (c++, 0)) != 0)
146 printf ("Error 2 on mpfr_cmp_ui(x,0) in check_macros\n");
151 printf ("Error 3 on mpfr_cmp_ui(x,0) in check_macros\n"
152 "(c = %d instead of 1)\n", c);
155 if (mpfr_cmp_si (x, 0) != 0)
157 printf ("Error 1 on mpfr_cmp_si(x,0) in check_macros\n");
160 if (mpfr_cmp_si (x, (c++, 0)) != 0)
162 printf ("Error 2 on mpfr_cmp_si(x,0) in check_macros\n");
167 printf ("Error 3 on mpfr_cmp_si(x,0) in check_macros\n"
168 "(c = %d instead of 2)\n", c);
182 mpfr_inits (x[0], x[1], x[2], (mpfr_ptr) 0);
183 mpfr_set_ui (x[0], 0, MPFR_RNDN);
185 if (mpfr_cmp_ui (p++, 0) != 0)
187 printf ("Error in mpfr_cmp_ui macro: result should be 0.\n");
192 printf ("Error in mpfr_cmp_ui macro: p - x[0] = %d (expecting 1)\n",
197 if (mpfr_cmp_si (p++, 0) != 0)
199 printf ("Error in mpfr_cmp_si macro: result should be 0.\n");
204 printf ("Error in mpfr_cmp_si macro: p - x[0] = %d (expecting 1)\n",
208 mpfr_clears (x[0], x[1], x[2], (mpfr_ptr) 0);
222 /* tests for cmp_ui */
223 mpfr_set_ui (x, 3, MPFR_RNDZ);
224 if ((mpfr_cmp_ui) (x, i = 3) != 0)
226 printf ("Error in mpfr_cmp_ui(3.0, 3)\n");
229 if (mpfr_cmp_ui (x, i = 2) <= 0)
231 printf ("Error in mpfr_cmp_ui(3.0,2)\n");
234 if (mpfr_cmp_ui (x, i = 4) >= 0)
236 printf ("Error in mpfr_cmp_ui(3.0,4)\n");
239 mpfr_set_ui (x, 0, MPFR_RNDZ);
240 mpfr_neg (x, x, MPFR_RNDZ);
241 if (mpfr_cmp_ui (x, i = 0))
243 printf ("Error in mpfr_cmp_ui(0.0,0)\n");
246 mpfr_set_ui (x, 1, MPFR_RNDZ);
247 if (mpfr_cmp_ui (x, i = 0) == 0)
249 printf ("Error in mpfr_cmp_ui(1.0,0)\n");
254 if (mpfr_cmp_ui (x, 1) <= 0)
256 printf ("Error in mpfr_cmp_ui (Inf, 0)\n");
259 mpfr_set_inf (x, -1);
260 if (mpfr_cmp_ui (x, 1) >= 0)
262 printf ("Error in mpfr_cmp_ui (-Inf, 0)\n");
266 mpfr_set_si (x, -1, MPFR_RNDN);
267 MPFR_ASSERTN(mpfr_cmp_ui (x, 1) < 0);
268 MPFR_ASSERTN(mpfr_cmp_ui (x, 0) < 0);
270 mpfr_set_ui (x, 1, MPFR_RNDN);
271 MPFR_ASSERTN(mpfr_cmp_ui (x, 0) > 0);
273 /* tests for cmp_si */
274 (mpfr_set_si) (x, -3, MPFR_RNDZ);
275 if ((mpfr_cmp_si) (x, s = -3) != 0)
277 printf ("Error in mpfr_cmp_si(-3.0,-3)\n");
280 if (mpfr_cmp_si (x, s = -4) <= 0)
282 printf ("Error in mpfr_cmp_si(-3.0,-4)\n");
285 if (mpfr_cmp_si (x, s = 1) >= 0)
287 printf ("Error in mpfr_cmp_si(-3.0,1)\n");
292 if (mpfr_cmp_si (x, -1) <= 0)
294 printf ("Error in mpfr_cmp_si (Inf, 0)\n");
297 mpfr_set_inf (x, -1);
298 if (mpfr_cmp_si (x, -1) >= 0)
300 printf ("Error in mpfr_cmp_si (-Inf, 0)\n");
305 mpfr_set_ui (x, 0, MPFR_RNDZ);
306 MPFR_ASSERTN(mpfr_cmp_si (x, 0) == 0);
307 MPFR_ASSERTN(mpfr_cmp_si (x, 1) < 0);
308 MPFR_ASSERTN(mpfr_cmp_si (x, -1) > 0);
311 mpfr_set_ui (x, 1, MPFR_RNDZ);
312 MPFR_ASSERTN(mpfr_cmp_si (x, 0) > 0);
313 mpfr_set_ui (x, 0, MPFR_RNDZ);
314 MPFR_ASSERTN(mpfr_cmp_si (x, 0) == 0);
315 mpfr_neg (x, x, MPFR_RNDZ);
316 MPFR_ASSERTN(mpfr_cmp_si (x, 0) == 0);
317 mpfr_set_si (x, -1, MPFR_RNDZ);
318 MPFR_ASSERTN(mpfr_cmp_si (x, 0) < 0);
321 mpfr_set_str_binary (x, "1E100");
322 MPFR_ASSERTN(mpfr_cmp_si (x, 0) > 0);
323 MPFR_ASSERTN(mpfr_cmp_si (x, 1) > 0);
324 MPFR_ASSERTN(mpfr_cmp_si (x, -1) > 0);
325 mpfr_set_str_binary (x, "-1E100");
326 MPFR_ASSERTN(mpfr_cmp_si (x, 0) < 0);
327 MPFR_ASSERTN(mpfr_cmp_si (x, 1) < 0);
328 MPFR_ASSERTN(mpfr_cmp_si (x, -1) < 0);
331 mpfr_set_ui (x, 1, MPFR_RNDZ);
332 mpfr_mul_2exp (x, x, GMP_NUMB_BITS - 1, MPFR_RNDZ);
333 /* now EXP(x)=GMP_NUMB_BITS */
334 MPFR_ASSERTN(mpfr_cmp_si (x, 1) > 0);