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

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

1.1       ohara       1: /* Test file for mpfr_ui_div.
                      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 <math.h>
                     23: #include <stdio.h>
                     24: #include <stdlib.h>
                     25: #include <time.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: void check _PROTO((unsigned long, double, mp_rnd_t, double));
                     33: void check_inexact _PROTO((void));
                     34: void check_nan _PROTO((void));
                     35:
                     36: /* checks that y/x gives the same results in double
                     37:    and with mpfr with 53 bits of precision */
                     38: void
                     39: check (unsigned long y, double x, mp_rnd_t rnd_mode, double z1)
                     40: {
                     41:   double z2; mpfr_t xx, zz;
                     42:
                     43:   mpfr_init2(xx, 53);
                     44:   mpfr_init2(zz, 53);
                     45:   mpfr_set_d(xx, x, rnd_mode);
                     46:   mpfr_ui_div(zz, y, xx, rnd_mode);
                     47: #ifdef MPFR_HAVE_FESETROUND
                     48:   mpfr_set_machine_rnd_mode(rnd_mode);
                     49: #endif
                     50:   if (z1==0.0) z1 = y/x;
                     51:   z2 = mpfr_get_d1 (zz);
                     52:   if (z1!=z2 && !(isnan(z1) && isnan(z2))) {
                     53:     printf("expected quotient is %1.20e, got %1.20e\n",z1,z2);
                     54:     printf("mpfr_ui_div failed for y=%lu x=%1.20e with rnd_mode=%s\n",
                     55:           y, x, mpfr_print_rnd_mode(rnd_mode));
                     56:     exit(1);
                     57:   }
                     58:   mpfr_clear(xx);
                     59:   mpfr_clear(zz);
                     60: }
                     61:
                     62: void
                     63: check_inexact (void)
                     64: {
                     65:   mpfr_t x, y, z;
                     66:   mp_prec_t px, py;
                     67:   int inexact, cmp;
                     68:   unsigned long int u;
                     69:   mp_rnd_t rnd;
                     70:
                     71:   mpfr_init (x);
                     72:   mpfr_init (y);
                     73:   mpfr_init (z);
                     74:
                     75:   for (px=2; px<300; px++)
                     76:     {
                     77:       mpfr_set_prec (x, px);
                     78:       mpfr_random (x);
                     79:       u = LONG_RAND ();
                     80:       for (py=2; py<300; py++)
                     81:        {
                     82:          mpfr_set_prec (y, py);
                     83:          mpfr_set_prec (z, py + px);
                     84:          for (rnd=0; rnd<4; rnd++)
                     85:            {
                     86:              inexact = mpfr_ui_div (y, u, x, rnd);
                     87:              if (mpfr_mul (z, y, x, rnd))
                     88:                {
                     89:                  fprintf (stderr, "z <- y * x should be exact\n");
                     90:                  exit (1);
                     91:                }
                     92:              cmp = mpfr_cmp_ui (z, u);
                     93:              if (((inexact == 0) && (cmp != 0)) ||
                     94:                  ((inexact > 0) && (cmp <= 0)) ||
                     95:                  ((inexact < 0) && (cmp >= 0)))
                     96:                {
                     97:                  fprintf (stderr, "Wrong inexact flag for u=%lu, rnd=%s\n", u,
                     98:                           mpfr_print_rnd_mode(rnd));
                     99:                  printf ("expected %d, got %d\n", cmp, inexact);
                    100:                  printf ("x="); mpfr_print_binary (x); putchar ('\n');
                    101:                  printf ("y="); mpfr_print_binary (y); putchar ('\n');
                    102:                  printf ("y*x="); mpfr_print_binary (z); putchar ('\n');
                    103:                  exit (1);
                    104:                }
                    105:            }
                    106:        }
                    107:     }
                    108:
                    109:   mpfr_clear (x);
                    110:   mpfr_clear (y);
                    111:   mpfr_clear (z);
                    112: }
                    113:
                    114: void
                    115: check_nan (void)
                    116: {
                    117:   mpfr_t  d, q;
                    118:
                    119:   mpfr_init2 (d, 100L);
                    120:   mpfr_init2 (q, 100L);
                    121:
                    122:   /* 1/+inf == 0 */
                    123:   MPFR_CLEAR_FLAGS (d);
                    124:   MPFR_SET_INF (d);
                    125:   MPFR_SET_POS (d);
                    126:   ASSERT_ALWAYS (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */
                    127:   ASSERT_ALWAYS (mpfr_number_p (q));
                    128:   ASSERT_ALWAYS (mpfr_sgn (q) == 0);
                    129:
                    130:   /* 1/-inf == -0 */
                    131:   MPFR_CLEAR_FLAGS (d);
                    132:   MPFR_SET_INF (d);
                    133:   MPFR_SET_NEG (d);
                    134:   ASSERT_ALWAYS (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */
                    135:   ASSERT_ALWAYS (mpfr_number_p (q));
                    136:   ASSERT_ALWAYS (mpfr_sgn (q) == 0);
                    137:
                    138:   /* 1/nan == nan */
                    139:   MPFR_SET_NAN (d);
                    140:   ASSERT_ALWAYS (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */
                    141:   ASSERT_ALWAYS (mpfr_nan_p (q));
                    142:
                    143:   /* 0/0 == nan */
                    144:   mpfr_set_ui (d, 0L, GMP_RNDN);
                    145:   ASSERT_ALWAYS (mpfr_ui_div (q, 0L, d, GMP_RNDZ) == 0); /* exact */
                    146:   ASSERT_ALWAYS (mpfr_nan_p (q));
                    147:
                    148:   mpfr_clear (d);
                    149:   mpfr_clear (q);
                    150: }
                    151:
                    152: int
                    153: main (int argc, char *argv[])
                    154: {
                    155: #ifdef MPFR_HAVE_FESETROUND
                    156:   double x;
                    157:   unsigned long y, N;
                    158:   int i, rnd_mode, rnd;
                    159:
                    160:   mpfr_test_init ();
                    161:
                    162:   SEED_RAND(time(NULL));
                    163:   N = (argc<2) ? 1000000 : atoi(argv[1]);
                    164:   rnd_mode = (argc<3) ? -1 : atoi(argv[2]);
                    165:   for (i=0;i<1000000;i++)
                    166:     {
                    167:       x = drand();
                    168:       y = LONG_RAND();
                    169:       if (ABS(x)>4e-286)
                    170:         {
                    171:           /* avoid denormalized numbers and overflows */
                    172:           rnd = (rnd_mode==-1) ? LONG_RAND()%4 : rnd_mode;
                    173:           check(y, x, rnd, 0.0);
                    174:         }
                    175:     }
                    176: #endif
                    177:   check_inexact ();
                    178:   check(948002822, 1.22191250737771397120e+20, GMP_RNDN,
                    179:        7.758352715731357946e-12);
                    180:   check(1976245324, 1.25296395864546893357e+232, GMP_RNDZ,
                    181:        1.5772563211925444801e-223);
                    182:   check(740454110, 2.11496253355831863313e+183, GMP_RNDZ,
                    183:        3.5010270784996976041e-175);
                    184:   check(1690540942, 1.28278599852446657468e-276, GMP_RNDU,
                    185:        1.3178666932321966062e285);
                    186:   check(1476599377, -2.14191393656148625995e+305, GMP_RNDD,
                    187:        -6.8938315017943889615e-297);
                    188:
                    189:   return 0;
                    190: }

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