1 /* Test file for mpfr_abs.
3 Copyright 2000, 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. */
27 #include "mpfr-test.h"
44 mpfr_set_prec (absx, p);
45 mpfr_urandomb (x, RANDS);
48 mpfr_set (absx, x, MPFR_RNDN);
49 mpfr_neg (x, x, MPFR_RNDN);
52 mpfr_set (absx, x, MPFR_RNDN);
58 inexact = mpfr_abs (y, x, (mpfr_rnd_t) rnd);
59 cmp = mpfr_cmp (y, absx);
60 if (((inexact == 0) && (cmp != 0)) ||
61 ((inexact > 0) && (cmp <= 0)) ||
62 ((inexact < 0) && (cmp >= 0)))
64 printf ("Wrong inexact flag: expected %d, got %d\n",
66 printf ("x="); mpfr_print_binary (x); puts ("");
67 printf ("absx="); mpfr_print_binary (absx); puts ("");
68 printf ("y="); mpfr_print_binary (y); puts ("");
81 check_cmp (int argc, char *argv[])
86 mpfr_inits2 (53, x, y, (mpfr_ptr) 0);
88 mpfr_set_ui(x, 1, MPFR_RNDN);
89 (mpfr_abs) (x, x, MPFR_RNDN);
90 if (mpfr_cmp_ui (x, 1))
92 printf ("Error in mpfr_abs(1.0)\n");
96 mpfr_set_si(x, -1, MPFR_RNDN);
97 mpfr_abs(x, x, MPFR_RNDN);
98 if (mpfr_cmp_ui (x, 1))
100 printf ("Error in mpfr_abs(1.0)\n");
104 mpfr_set_si(x, -1, MPFR_RNDN);
105 mpfr_abs(x, x, MPFR_RNDN);
106 if (mpfr_cmp_ui (x, 1))
108 printf ("Error in mpfr_abs(-1.0)\n");
113 mpfr_abs (x, x, MPFR_RNDN);
114 if (!mpfr_inf_p(x) || (mpfr_sgn(x) <= 0))
116 printf ("Error in mpfr_abs(Inf).\n");
119 mpfr_set_inf (x, -1);
120 mpfr_abs (x, x, MPFR_RNDN);
121 if (!mpfr_inf_p(x) || (mpfr_sgn(x) <= 0))
123 printf ("Error in mpfr_abs(-Inf).\n");
128 mpfr_abs (x, x, MPFR_RNDN);
131 printf ("Error in mpfr_abs(NAN).\n");
135 n = (argc==1) ? 25000 : atoi(argv[1]);
136 for (k = 1; k <= n; k++)
139 int sign = SIGN_RAND ();
141 mpfr_urandomb (x, RANDS);
142 MPFR_SET_SIGN (x, sign);
144 mpfr_abs (y, x, rnd);
148 printf ("Mismatch for sign=%d and x=", sign);
149 mpfr_print_binary (x);
150 printf ("\nResults=");
151 mpfr_print_binary (y);
157 mpfr_clears (x, y, (mpfr_ptr) 0);
160 #define TEST_FUNCTION mpfr_abs
161 #include "tgeneric.c"
164 main (int argc, char *argv[])
170 check_cmp (argc, argv);
172 test_generic (2, 1000, 10);