Annotation of OpenXM_contrib/gmp/mpfr/tests/thypot.c, Revision 1.1
1.1 ! ohara 1: /* Test file for mpfr_hypot.
! 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 <limits.h>
! 25: #include <stdlib.h>
! 26: #include "gmp.h"
! 27: #include "gmp-impl.h"
! 28: #include "mpfr.h"
! 29: #include "mpfr-impl.h"
! 30: #include "mpfr-test.h"
! 31:
! 32: #define TEST_FUNCTION mpfr_hypot
! 33:
! 34: int
! 35: main (int argc, char *argv[])
! 36: {
! 37: unsigned int prec, err, yprec, n, p0 = 2, p1 = 100, N = 100;
! 38: mp_rnd_t rnd;
! 39: mpfr_t x1, x2, y, z, t;
! 40: int inexact, compare, compare2;
! 41:
! 42: mpfr_init (x1);
! 43: mpfr_init (x2);
! 44: mpfr_init (y);
! 45: mpfr_init (z);
! 46: mpfr_init (t);
! 47:
! 48: /* thypot prec - perform one random computation with precision prec */
! 49: if (argc >= 2)
! 50: {
! 51: p0 = p1 = atoi (argv[1]);
! 52: N = 1;
! 53: }
! 54:
! 55: for (prec = p0; prec <= p1; prec++)
! 56: {
! 57: mpfr_set_prec (x1, prec);
! 58: mpfr_set_prec (x2, prec);
! 59: mpfr_set_prec (z, prec);
! 60: mpfr_set_prec (t, prec);
! 61: yprec = prec + 10;
! 62:
! 63: for (n=0; n<N; n++)
! 64: {
! 65: mpfr_random(x1);
! 66: mpfr_random(x2);
! 67: if (random() % 2)
! 68: mpfr_neg (x1, x1, GMP_RNDN);
! 69: if (random() % 2)
! 70: mpfr_neg (x2, x2, GMP_RNDN);
! 71: rnd = random () % 4;
! 72: mpfr_set_prec (y, yprec);
! 73:
! 74: compare =TEST_FUNCTION (y, x1,x2, rnd);
! 75: err = (rnd == GMP_RNDN) ? yprec + 1 : yprec;
! 76: if (mpfr_can_round (y, err, rnd, rnd, prec))
! 77: {
! 78: mpfr_set (t, y, rnd);
! 79: inexact = TEST_FUNCTION (z, x1,x2, rnd);
! 80: if (mpfr_cmp (t, z))
! 81: {
! 82: printf ("results differ for x1=");
! 83: mpfr_out_str (stdout, 2, prec, x1, GMP_RNDN);
! 84: printf ("\n et x2=");
! 85: mpfr_out_str (stdout, 2, prec, x2, GMP_RNDN);
! 86: printf (" \n prec=%u rnd_mode=%s\n", prec,
! 87: mpfr_print_rnd_mode (rnd));
! 88: printf (" got ");
! 89: mpfr_out_str (stdout, 2, prec, z, GMP_RNDN);
! 90: putchar ('\n');
! 91: printf (" expected ");
! 92: mpfr_out_str (stdout, 2, prec, t, GMP_RNDN);
! 93: putchar ('\n');
! 94: printf (" approximation was ");
! 95: mpfr_print_binary (y);
! 96: putchar ('\n');
! 97: exit (1);
! 98: }
! 99: compare2 = mpfr_cmp (t, y);
! 100: /* if rounding to nearest, cannot know the sign of t - f(x)
! 101: because of composed rounding: y = o(f(x)) and t = o(y) */
! 102: if ((rnd != GMP_RNDN) && (compare * compare2 >= 0))
! 103: compare = compare + compare2;
! 104: else
! 105: compare = inexact; /* cannot determine sign(t-f(x)) */
! 106: if (((inexact == 0) && (compare != 0)) ||
! 107: ((inexact > 0) && (compare <= 0)) ||
! 108: ((inexact < 0) && (compare >= 0)))
! 109: {
! 110: fprintf (stderr, "Wrong inexact flag for rnd=%s: expected %d, got %d\n",
! 111: mpfr_print_rnd_mode (rnd), compare, inexact);
! 112: printf ("x1="); mpfr_print_binary (x1); putchar ('\n');
! 113: printf ("x2="); mpfr_print_binary (x2); putchar ('\n');
! 114: printf ("t="); mpfr_print_binary (t); putchar ('\n');
! 115: exit (1);
! 116: }
! 117: }
! 118: }
! 119: }
! 120:
! 121: mpfr_clear (x1);
! 122: mpfr_clear (x2);
! 123: mpfr_clear (y);
! 124: mpfr_clear (z);
! 125: mpfr_clear (t);
! 126:
! 127: return 0;
! 128: }
! 129:
! 130:
! 131:
! 132:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>