[BACK]Return to tabs.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpfr / tests

Annotation of OpenXM_contrib/gmp/mpfr/tests/tabs.c, Revision 1.1.1.1

1.1       ohara       1: /* Test file for mpfr_abs.
                      2:
                      3: Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
                      4:
                      5: This file is part of the MPFR Library.
                      6:
                      7: The MPFR Library is free software; you can redistribute it and/or modify
                      8: it under the terms of the GNU Lesser General Public License as published by
                      9: the Free Software Foundation; either version 2.1 of the License, or (at your
                     10: option) any later version.
                     11:
                     12: The MPFR Library is distributed in the hope that it will be useful, but
                     13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
                     15: License for more details.
                     16:
                     17: You should have received a copy of the GNU Lesser General Public License
                     18: along with the MPFR Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include "gmp.h"
                     25: #include "mpfr.h"
                     26: #include "mpfr-impl.h"
                     27: #include "mpfr-test.h"
                     28:
                     29: void check_inexact _PROTO((void));
                     30:
                     31: void
                     32: check_inexact (void)
                     33: {
                     34:   mp_prec_t p, q;
                     35:   mpfr_t x, y, absx;
                     36:   mp_rnd_t rnd;
                     37:   int inexact, cmp;
                     38:
                     39:   mpfr_init (x);
                     40:   mpfr_init (y);
                     41:   mpfr_init (absx);
                     42:
                     43:   for (p=2; p<500; p++)
                     44:     {
                     45:       mpfr_set_prec (x, p);
                     46:       mpfr_set_prec (absx, p);
                     47:       mpfr_random (x);
                     48:       if (LONG_RAND () % 2)
                     49:        {
                     50:          mpfr_set (absx, x, GMP_RNDN);
                     51:          mpfr_neg (x, x, GMP_RNDN);
                     52:        }
                     53:       else
                     54:        mpfr_set (absx, x, GMP_RNDN);
                     55:       for (q=2; q<2*p; q++)
                     56:        {
                     57:          mpfr_set_prec (y, q);
                     58:          for (rnd=0; rnd<4; rnd++)
                     59:            {
                     60:              inexact = mpfr_abs (y, x, rnd);
                     61:              cmp = mpfr_cmp (y, absx);
                     62:              if (((inexact == 0) && (cmp != 0)) ||
                     63:                  ((inexact > 0) && (cmp <= 0)) ||
                     64:                  ((inexact < 0) && (cmp >= 0)))
                     65:                {
                     66:                  fprintf (stderr, "Wrong inexact flag: expected %d, got %d\n", cmp, inexact);
                     67:                  printf ("x="); mpfr_print_binary (x); putchar ('\n');
                     68:                  printf ("absx="); mpfr_print_binary (absx); putchar ('\n');
                     69:                  printf ("y="); mpfr_print_binary (y); putchar ('\n');
                     70:                  exit (1);
                     71:                }
                     72:            }
                     73:        }
                     74:     }
                     75:
                     76:   mpfr_clear (x);
                     77:   mpfr_clear (y);
                     78:   mpfr_clear (absx);
                     79: }
                     80:
                     81: int
                     82: main (int argc, char *argv[])
                     83: {
                     84:    mpfr_t x;
                     85:    int n, k, rnd;
                     86:    double d, absd, dd;
                     87:
                     88:    mpfr_test_init ();
                     89:
                     90:    check_inexact ();
                     91:
                     92:    mpfr_init2(x, 53);
                     93:
                     94:    mpfr_set_d(x, 1.0, GMP_RNDN);
                     95:    mpfr_abs(x, x, GMP_RNDN);
                     96:    if (mpfr_get_d1 (x) != 1.0) {
                     97:      fprintf(stderr, "Error in mpfr_abs(1.0)\n"); exit(1);
                     98:    }
                     99:
                    100:    mpfr_set_d(x, -1.0, GMP_RNDN);
                    101:    mpfr_abs(x, x, GMP_RNDN);
                    102:    if (mpfr_get_d1 (x) != 1.0) {
                    103:      fprintf(stderr, "Error in mpfr_abs(-1.0)\n"); exit(1);
                    104:    }
                    105:
                    106:    mpfr_set_inf (x, 1);
                    107:    mpfr_abs (x, x, GMP_RNDN);
                    108:    if (!mpfr_inf_p(x) || (mpfr_sgn(x) <= 0))
                    109:      {
                    110:        fprintf (stderr, "Error in mpfr_abs(Inf).\n");
                    111:        exit (1);
                    112:      }
                    113:
                    114:    mpfr_set_inf (x, -1);
                    115:    mpfr_abs (x, x, GMP_RNDN);
                    116:    if (!mpfr_inf_p(x) || (mpfr_sgn(x) <= 0))
                    117:      {
                    118:        fprintf (stderr, "Error in mpfr_abs(-Inf).\n");
                    119:        exit (1);
                    120:      }
                    121:
                    122:    n = (argc==1) ? 1000000 : atoi(argv[1]);
                    123:    for (k = 1; k <= n; k++)
                    124:      {
                    125:        do
                    126:         {
                    127:           d = drand ();
                    128:           absd = ABS(d);
                    129:         }
                    130: #ifdef HAVE_DENORMS
                    131:        while (0);
                    132: #else
                    133:        while (absd <= 2.2e-307);
                    134: #endif
                    135:        rnd = LONG_RAND() % 4;
                    136:        mpfr_set_d (x, d, 0);
                    137:        mpfr_abs (x, x, rnd);
                    138:        dd = mpfr_get_d1 (x);
                    139:        if (!isnan(d) && (dd != absd))
                    140:         {
                    141:           fprintf(stderr,
                    142:                   "Mismatch on d = %.20e\n", d);
                    143:           fprintf(stderr, "dd=%.20e\n", dd);
                    144:           mpfr_print_binary(x); putchar('\n');
                    145:           exit(1);
                    146:         }
                    147:      }
                    148:
                    149:    mpfr_clear(x);
                    150:
                    151:    return 0;
                    152: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>