Annotation of OpenXM_contrib/gmp/mpfr/tests/tui_pow.c, Revision 1.1
1.1 ! ohara 1: /* Test file for mpfr_ui_pow.
! 2:
! 3: Copyright 2001 Free Software Foundation.
! 4: Adapted from tarctan.c.
! 5:
! 6: This file is part of the MPFR Library.
! 7:
! 8: The 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 2.1 of the License, or (at your
! 11: option) any later version.
! 12:
! 13: The 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.
! 17:
! 18: You should have received a copy of the GNU Lesser General Public License
! 19: along with the MPFR Library; see the file COPYING.LIB. If not, write to
! 20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
! 21: MA 02111-1307, USA. */
! 22:
! 23: #include <stdio.h>
! 24: #include <stdlib.h>
! 25: #include "gmp.h"
! 26: #include "gmp-impl.h"
! 27: #include "mpfr.h"
! 28: #include "mpfr-impl.h"
! 29: #include "mpfr-test.h"
! 30:
! 31:
! 32: int
! 33: main (int argc, char *argv[])
! 34: {
! 35: mpfr_t x, y;
! 36: unsigned long int n;
! 37:
! 38: mpfr_init (x);
! 39: mpfr_init (y);
! 40:
! 41: n = abs(random());
! 42:
! 43: MPFR_CLEAR_NAN(x);
! 44: MPFR_SET_INF(x);
! 45: mpfr_ui_pow (y, n, x, GMP_RNDN);
! 46: if(!MPFR_IS_INF(y))
! 47: {
! 48: printf ("evaluation of function in INF does not return INF\n");
! 49: exit (1);
! 50: }
! 51:
! 52: MPFR_CHANGE_SIGN(x);
! 53: mpfr_ui_pow (y, n,x, GMP_RNDN);
! 54: if(!MPFR_IS_ZERO(y))
! 55: {
! 56: printf ("evaluation of function in -INF does not return 0");
! 57: exit (1);
! 58: }
! 59:
! 60: MPFR_SET_NAN(x);
! 61: mpfr_ui_pow (y, n,x, GMP_RNDN);
! 62: if(!MPFR_IS_NAN(y))
! 63: {
! 64: printf ("evaluation of function in NAN does not return NAN");
! 65: exit (1);
! 66: }
! 67:
! 68: {
! 69: mp_prec_t prec, yprec;
! 70: mpfr_t z, t;
! 71: mp_rnd_t rnd;
! 72: int inexact, compare, compare2;
! 73: unsigned int n, err;
! 74:
! 75: int p0=2;
! 76: int p1=100;
! 77: int N=100;
! 78:
! 79: mpfr_init (z);
! 80: mpfr_init (t);
! 81:
! 82: /* generic test */
! 83: for (prec = p0; prec <= p1; prec++)
! 84: {
! 85: mpfr_set_prec (x, prec);
! 86: mpfr_set_prec (z, prec);
! 87: mpfr_set_prec (t, prec);
! 88: yprec = prec + 10;
! 89:
! 90: for (n=0; n<N; n++)
! 91: {
! 92: int nt;
! 93: nt = abs(random());
! 94: mpfr_random (x);
! 95: rnd = random () % 4;
! 96: mpfr_set_prec (y, yprec);
! 97: compare = mpfr_ui_pow (y, nt, x, rnd);
! 98: err = (rnd == GMP_RNDN) ? yprec + 1 : yprec;
! 99: if (mpfr_can_round (y, err, rnd, rnd, prec))
! 100: {
! 101: mpfr_set (t, y, rnd);
! 102: inexact = mpfr_ui_pow (z, nt, x, rnd);
! 103: if (mpfr_cmp (t, z))
! 104: {
! 105: printf ("results differ for x=");
! 106: mpfr_out_str (stdout, 2, prec, x, GMP_RNDN);
! 107: printf (" n= %i",nt);
! 108: printf (" prec=%u rnd_mode=%s\n", (unsigned) prec,
! 109: mpfr_print_rnd_mode (rnd));
! 110: printf ("got ");
! 111: mpfr_out_str (stdout, 2, prec, z, GMP_RNDN);
! 112: putchar ('\n');
! 113: printf ("expected ");
! 114: mpfr_out_str (stdout, 2, prec, t, GMP_RNDN);
! 115: putchar ('\n');
! 116: printf ("approx ");
! 117: mpfr_print_binary (y);
! 118: putchar ('\n');
! 119: exit (1);
! 120: }
! 121: compare2 = mpfr_cmp (t, y);
! 122: /* if rounding to nearest, cannot know the sign of t - f(x)
! 123: because of composed rounding: y = o(f(x)) and t = o(y) */
! 124: if ((rnd != GMP_RNDN) && (compare * compare2 >= 0))
! 125: compare = compare + compare2;
! 126: else
! 127: compare = inexact; /* cannot determine sign(t-f(x)) */
! 128: if (((inexact == 0) && (compare != 0)) ||
! 129: ((inexact > 0) && (compare <= 0)) ||
! 130: ((inexact < 0) && (compare >= 0)))
! 131: {
! 132: fprintf (stderr, "Wrong inexact flag for rnd=%s: expected %d, got %d\n",
! 133: mpfr_print_rnd_mode (rnd), compare, inexact);
! 134: printf ("x="); mpfr_print_binary (x); putchar ('\n');
! 135: printf ("y="); mpfr_print_binary (y); putchar ('\n');
! 136: printf ("t="); mpfr_print_binary (t); putchar ('\n');
! 137: exit (1);
! 138: }
! 139: }
! 140: }
! 141: }
! 142:
! 143: mpfr_clear (z);
! 144: mpfr_clear (t);
! 145: }
! 146:
! 147: mpfr_clear (x);
! 148: mpfr_clear (y);
! 149:
! 150: return 0;
! 151: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>