/* Test file for in-place operations. Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc. Contributed by the AriC and Caramel projects, INRIA. This file is part of the GNU MPFR Library. The GNU MPFR Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. The GNU MPFR Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include "mpfr-test.h" #define DISP(s, t) {printf(s); mpfr_out_str(stdout, 2, 0, t, MPFR_RNDN); } #define DISP2(s,t) {DISP(s,t); putchar('\n');} #define SPECIAL_MAX 12 static void set_special (mpfr_ptr x, unsigned int select) { MPFR_ASSERTN (select < SPECIAL_MAX); switch (select) { case 0: MPFR_SET_NAN (x); break; case 1: MPFR_SET_INF (x); MPFR_SET_POS (x); break; case 2: MPFR_SET_INF (x); MPFR_SET_NEG (x); break; case 3: MPFR_SET_ZERO (x); MPFR_SET_POS (x); break; case 4: MPFR_SET_ZERO (x); MPFR_SET_NEG (x); break; case 5: mpfr_set_str_binary (x, "1"); break; case 6: mpfr_set_str_binary (x, "-1"); break; case 7: mpfr_set_str_binary (x, "1e-1"); break; case 8: mpfr_set_str_binary (x, "1e+1"); break; case 9: mpfr_const_pi (x, MPFR_RNDN); break; case 10: mpfr_const_pi (x, MPFR_RNDN); MPFR_SET_EXP (x, MPFR_GET_EXP (x)-1); break; default: mpfr_urandomb (x, RANDS); if (randlimb () & 1) mpfr_neg (x, x, MPFR_RNDN); break; } } /* same than mpfr_cmp, but returns 0 for both NaN's */ static int mpfr_compare (mpfr_srcptr a, mpfr_srcptr b) { return (MPFR_IS_NAN(a)) ? !MPFR_IS_NAN(b) : (MPFR_IS_NAN(b) || mpfr_cmp(a, b)); } static void test3 (int (*testfunc)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_rnd_t), const char *foo, mpfr_prec_t prec, mpfr_rnd_t rnd) { mpfr_t ref1, ref2, ref3; mpfr_t res1; int i; #ifdef DEBUG printf("checking %s\n", foo); #endif mpfr_init2 (ref1, prec); mpfr_init2 (ref2, prec); mpfr_init2 (ref3, prec); mpfr_init2 (res1, prec); /* for each variable, consider each of the following 6 possibilities: NaN, +Infinity, -Infinity, +0, -0 or a random number */ for (i=0; i < SPECIAL_MAX*SPECIAL_MAX ; i++) { set_special (ref2, i%SPECIAL_MAX); set_special (ref3, i/SPECIAL_MAX); /* reference call: foo(a, b, c) */ testfunc (ref1, ref2, ref3, rnd); /* foo(a, a, c) */ mpfr_set (res1, ref2, rnd); /* exact operation */ testfunc (res1, res1, ref3, rnd); if (mpfr_compare (res1, ref1)) { printf ("Error for %s(a, a, c) for ", foo); DISP("a=",ref2); DISP2(", c=",ref3); printf ("expected "); mpfr_print_binary (ref1); puts (""); printf ("got "); mpfr_print_binary (res1); puts (""); exit (1); } /* foo(a, b, a) */ mpfr_set (res1, ref3, rnd); testfunc (res1, ref2, res1, rnd); if (mpfr_compare (res1, ref1)) { printf ("Error for %s(a, b, a) for ", foo); DISP("b=",ref2); DISP2(", a=", ref3); DISP("expected ", ref1); DISP2(", got ",res1); exit (1); } /* foo(a, a, a) */ mpfr_set (ref3, ref2, rnd); testfunc (ref1, ref2, ref3, rnd); mpfr_set (res1, ref2, rnd); testfunc (res1, res1, res1, rnd); if (mpfr_compare (res1, ref1)) { printf ("Error for %s(a, a, a) for ", foo); DISP2("a=",ref2); DISP("expected ", ref1); DISP2(", got", res1); exit (1); } } mpfr_clear (ref1); mpfr_clear (ref2); mpfr_clear (ref3); mpfr_clear (res1); } static void test4 (int (*testfunc)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mpfr_rnd_t), const char *foo, mpfr_prec_t prec, mpfr_rnd_t rnd) { mpfr_t ref, op1, op2, op3; mpfr_t res; int i, j, k; #ifdef DEBUG printf("checking %s\n", foo); #endif mpfr_init2 (ref, prec); mpfr_init2 (op1, prec); mpfr_init2 (op2, prec); mpfr_init2 (op3, prec); mpfr_init2 (res, prec); /* for each variable, consider each of the following 6 possibilities: NaN, +Infinity, -Infinity, +0, -0 or a random number */ for (i=0; i= MPFR_VERSION_NUM(2,4,0) test2 (mpfr_li2, "mpfr_li2", p, (mpfr_rnd_t) rnd); test2 (mpfr_rec_sqrt, "mpfr_rec_sqrt", p, (mpfr_rnd_t) rnd); test3 (mpfr_fmod, "mpfr_fmod", p, (mpfr_rnd_t) rnd); test3a (mpfr_modf, "mpfr_modf", p, (mpfr_rnd_t) rnd); test3a (mpfr_sinh_cosh, "mpfr_sinh_cosh", p, (mpfr_rnd_t) rnd); #endif } tests_end_mpfr (); return 0; }