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

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

1.1       ohara       1: /* Test file for mpfr_div_ui.
                      2:
                      3: Copyright 1999, 2000, 2001, 2002 Free Software Foundation.
                      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 <time.h>
                     25: #include "gmp.h"
                     26: #include "mpfr.h"
                     27: #include "mpfr-test.h"
                     28:
                     29: void check _PROTO((double, unsigned long, mp_rnd_t, double));
                     30: void special _PROTO((void));
                     31: void check_inexact _PROTO((void));
                     32:
                     33: void
                     34: check (double d, unsigned long u, mp_rnd_t rnd, double e)
                     35: {
                     36:   mpfr_t x, y;
                     37:   double f;
                     38:
                     39:   mpfr_init2(x, 53); mpfr_init2(y, 53);
                     40: #ifdef MPFR_HAVE_FESETROUND
                     41:   mpfr_set_machine_rnd_mode(rnd);
                     42: #endif
                     43:   if (e==0.0) e = d / u;
                     44:   mpfr_set_d(x, d, rnd);
                     45:   mpfr_div_ui(y, x, u, rnd);
                     46:   f = mpfr_get_d1 (y);
                     47:   if (f != e && (!isnan(f) || !isnan(e))) {
                     48:     printf("mpfr_div_ui failed for x=%1.20e, u=%lu, rnd=%s\n", d, u,
                     49:           mpfr_print_rnd_mode(rnd));
                     50:     printf("expected result is %1.20e, got %1.20e, dif=%d ulp\n",e,f,ulp(e,f));
                     51:     exit(1);
                     52:   }
                     53:   mpfr_clear(x); mpfr_clear(y);
                     54: }
                     55:
                     56: void
                     57: special (void)
                     58: {
                     59:   mpfr_t x, y;
                     60:   unsigned xprec, yprec;
                     61:
                     62:   mpfr_init (x);
                     63:   mpfr_init (y);
                     64:
                     65:   mpfr_set_prec (x, 32);
                     66:   mpfr_set_prec (y, 32);
                     67:   mpfr_set_ui (x, 1, GMP_RNDN);
                     68:   mpfr_div_ui (y, x, 3, GMP_RNDN);
                     69:
                     70:   mpfr_set_prec (x, 100);
                     71:   mpfr_set_prec (y, 100);
                     72:   mpfr_random (x);
                     73:   mpfr_div_ui (y, x, 123456, GMP_RNDN);
                     74:   mpfr_set_ui (x, 0, GMP_RNDN);
                     75:   mpfr_div_ui (y, x, 123456789, GMP_RNDN);
                     76:   if (mpfr_cmp_ui (y, 0))
                     77:     {
                     78:       fprintf (stderr, "mpfr_div_ui gives non-zero for 0/ui\n");
                     79:       exit (1);
                     80:     }
                     81:
                     82:   /* bug found by Norbert Mueller, 21 Aug 2001 */
                     83:   mpfr_set_prec (x, 110);
                     84:   mpfr_set_prec (y, 60);
                     85:   mpfr_set_str_raw (x, "0.110101110011111110011111001110011001110111000000111110001000111011000011E-44");
                     86:   mpfr_div_ui (y, x, 17, GMP_RNDN);
                     87:   mpfr_set_str_raw (x, "0.11001010100101100011101110000001100001010110101001010011011E-48");
                     88:   if (mpfr_cmp (x, y))
                     89:     {
                     90:       fprintf (stderr, "Error in x/17 for x=1/16!\n");
                     91:       fprintf (stderr, "Expected ");
                     92:       mpfr_out_str (stderr, 2, 0, x, GMP_RNDN);
                     93:       fprintf (stderr, "\nGot      ");
                     94:       mpfr_out_str (stderr, 2, 0, y, GMP_RNDN);
                     95:       fprintf (stderr, "\n");
                     96:       exit (1);
                     97:     }
                     98:
                     99:   for (xprec = 53; xprec <= 128; xprec++)
                    100:     {
                    101:       mpfr_set_prec (x, xprec);
                    102:       mpfr_set_str_raw (x, "0.1100100100001111110011111000000011011100001100110111E2");
                    103:       for (yprec = 53; yprec <= 128; yprec++)
                    104:        {
                    105:          mpfr_set_prec (y, yprec);
                    106:          mpfr_div_ui (y, x, 1, GMP_RNDN);
                    107:          if (mpfr_get_d1 (x) != mpfr_get_d1 (y))
                    108:            {
                    109:              fprintf (stderr, "division by 1.0 fails for xprec=%u, yprec=%u\n", xprec, yprec);
                    110:              printf ("expected "); mpfr_print_binary (x); putchar ('\n');
                    111:              printf ("got      "); mpfr_print_binary (y); putchar ('\n');
                    112:              exit (1);
                    113:            }
                    114:        }
                    115:     }
                    116:
                    117:   mpfr_clear (x);
                    118:   mpfr_clear (y);
                    119: }
                    120:
                    121: void
                    122: check_inexact (void)
                    123: {
                    124:   mpfr_t x, y, z;
                    125:   mp_prec_t px, py;
                    126:   int inexact, cmp;
                    127:   unsigned long int u;
                    128:   mp_rnd_t rnd;
                    129:
                    130:   mpfr_init (x);
                    131:   mpfr_init (y);
                    132:   mpfr_init (z);
                    133:
                    134:   for (px=2; px<300; px++)
                    135:     {
                    136:       mpfr_set_prec (x, px);
                    137:       mpfr_random (x);
                    138:       do { u = LONG_RAND (); } while (u == 0);
                    139:       for (py=2; py<300; py++)
                    140:        {
                    141:          mpfr_set_prec (y, py);
                    142:          mpfr_set_prec (z, py + mp_bits_per_limb);
                    143:          for (rnd=0; rnd<4; rnd++)
                    144:            {
                    145:              inexact = mpfr_div_ui (y, x, u, rnd);
                    146:              if (mpfr_mul_ui (z, y, u, rnd))
                    147:                {
                    148:                  fprintf (stderr, "z <- y * u should be exact for u=%lu\n", u);
                    149:                  printf ("y="); mpfr_print_binary (y); putchar ('\n');
                    150:                  printf ("z="); mpfr_print_binary (z); putchar ('\n');
                    151:                  exit (1);
                    152:                }
                    153:              cmp = mpfr_cmp (z, x);
                    154:              if (((inexact == 0) && (cmp != 0)) ||
                    155:                  ((inexact > 0) && (cmp <= 0)) ||
                    156:                  ((inexact < 0) && (cmp >= 0)))
                    157:                {
                    158:                  fprintf (stderr, "Wrong inexact flag for u=%lu, rnd=%s\n", u,
                    159:                           mpfr_print_rnd_mode(rnd));
                    160:                  printf ("x="); mpfr_print_binary (x); putchar ('\n');
                    161:                  printf ("y="); mpfr_print_binary (y); putchar ('\n');
                    162:                  exit (1);
                    163:                }
                    164:            }
                    165:        }
                    166:     }
                    167:
                    168:   mpfr_clear (x);
                    169:   mpfr_clear (y);
                    170:   mpfr_clear (z);
                    171: }
                    172:
                    173: int
                    174: main (int argc, char **argv)
                    175: {
                    176:   mpfr_t x;
                    177: #ifdef MPFR_HAVE_FESETROUND
                    178:   int i;
                    179:   unsigned long u;
                    180:   double d;
                    181:
                    182:   mpfr_test_init ();
                    183:
                    184:   SEED_RAND (time(NULL));
                    185:   for (i=0;i<1000000;i++)
                    186:     {
                    187:       do { u = LONG_RAND(); } while (u==0);
                    188:       do { d = drand(); } while (ABS(d/u)<2.2e-307);
                    189:       check (d, u, LONG_RAND() % 4, 0.0);
                    190:     }
                    191: #endif
                    192:
                    193:   check_inexact ();
                    194:
                    195:   special ();
                    196:
                    197:   check(1.0, 3, GMP_RNDN, 3.3333333333333331483e-1);
                    198:   check(1.0, 3, GMP_RNDZ, 3.3333333333333331483e-1);
                    199:   check(1.0, 3, GMP_RNDU, 3.3333333333333337034e-1);
                    200:   check(1.0, 3, GMP_RNDD, 3.3333333333333331483e-1);
                    201:   check(1.0, 2116118, GMP_RNDN, 4.7256343927890600483e-7);
                    202:   check(1.098612288668109782, 5, GMP_RNDN, 0.21972245773362195087);
                    203:
                    204:   mpfr_init2(x, 100);
                    205:   mpfr_set_prec(x, 53);
                    206:   mpfr_set_ui(x, 3, GMP_RNDD);
                    207:   mpfr_log(x, x, GMP_RNDD);
                    208:   mpfr_div_ui(x, x, 5, GMP_RNDD);
                    209:   if (mpfr_get_d1 (x) != 0.21972245773362189536) {
                    210:     fprintf(stderr, "Error in mpfr_div_ui for x=ln(3), u=5\n"); exit(1);
                    211:   }
                    212:   mpfr_clear(x);
                    213:
                    214:   return 0;
                    215: }

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