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

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

1.1       ohara       1: /* Test file for mpfr_fma.
                      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:
                     33: int
                     34: main (int argc, char *argv[])
                     35: {
                     36:   mpfr_t x, y,z,s;
                     37:   mpfr_init (x);
                     38:   mpfr_init (s);
                     39:   mpfr_init (y);
                     40:   mpfr_init (z);
                     41:
                     42:   /* check special cases */
                     43:   mpfr_set_prec (x, 2);
                     44:   mpfr_set_prec (y, 2);
                     45:   mpfr_set_prec (z, 2);
                     46:   mpfr_set_prec (s, 2);
                     47:   mpfr_set_d (x, -0.75, GMP_RNDN);
                     48:   mpfr_set_d (y, 0.5, GMP_RNDN);
                     49:   mpfr_set_d (z, 0.375, GMP_RNDN);
                     50:   mpfr_fma (s, x, y, z, GMP_RNDU); /* result is 0 */
                     51:
                     52:   mpfr_set_prec (x, 27);
                     53:   mpfr_set_prec (y, 27);
                     54:   mpfr_set_prec (z, 27);
                     55:   mpfr_set_prec (s, 27);
                     56:   mpfr_set_str_raw (x, "1.11111111111111111111111111e-1");
                     57:   mpfr_set (y, x, GMP_RNDN);
                     58:   mpfr_set_str_raw (z, "-1.00011110100011001011001001e-1");
                     59:   if (mpfr_fma (s, x, y, z, GMP_RNDN) >= 0)
                     60:     {
                     61:       fprintf (stderr, "Wrong inexact flag for x=y=1-2^(-27)\n");
                     62:       exit (1);
                     63:     }
                     64:
                     65:   MPFR_SET_NAN(x);
                     66:   mpfr_random(y);
                     67:   mpfr_random(z);
                     68:   mpfr_fma (s,x, y,z, GMP_RNDN);
                     69:   if(!MPFR_IS_NAN(s))
                     70:     {
                     71:       fprintf (stderr, "evaluation of function in x=NAN does not return NAN");
                     72:       exit (1);
                     73:     }
                     74:
                     75:   MPFR_CLEAR_FLAGS(x);
                     76:   MPFR_CLEAR_FLAGS(y);
                     77:   MPFR_CLEAR_FLAGS(z);
                     78:
                     79:   MPFR_SET_NAN(y);
                     80:   mpfr_random(x);
                     81:   mpfr_random(z);
                     82:   mpfr_fma (s,x, y,z, GMP_RNDN);
                     83:   if(!MPFR_IS_NAN(s))
                     84:     {
                     85:       printf ("evaluation of function in y=NAN does not return NAN");
                     86:       exit (1);
                     87:     }
                     88:
                     89:   MPFR_CLEAR_FLAGS(x);
                     90:   MPFR_CLEAR_FLAGS(y);
                     91:   MPFR_CLEAR_FLAGS(z);
                     92:
                     93:   MPFR_SET_NAN(z);
                     94:   mpfr_random(y);
                     95:   mpfr_random(x);
                     96:   mpfr_fma (s,x, y,z, GMP_RNDN);
                     97:   if(!MPFR_IS_NAN(s))
                     98:     {
                     99:       printf ("evaluation of function in z=NAN does not return NAN");
                    100:       exit (1);
                    101:     }
                    102:
                    103:   MPFR_CLEAR_FLAGS(x);
                    104:   MPFR_CLEAR_FLAGS(y);
                    105:   MPFR_CLEAR_FLAGS(z);
                    106:
                    107:   MPFR_SET_INF(x);
                    108:   MPFR_SET_ZERO(y);
                    109:   mpfr_random(z);
                    110:   mpfr_fma (s,x, y,z, GMP_RNDN);
                    111:   if(!MPFR_IS_NAN(s))
                    112:     {
                    113:       printf ("evaluation of function in x=INF y=0  does not return NAN");
                    114:       exit (1);
                    115:     }
                    116:
                    117:   MPFR_CLEAR_FLAGS(z);
                    118:   MPFR_CLEAR_FLAGS(y);
                    119:   MPFR_CLEAR_FLAGS(x);
                    120:
                    121:   MPFR_SET_INF(y);
                    122:   MPFR_SET_ZERO(x);
                    123:   mpfr_random(z);
                    124:   mpfr_fma (s,x, y,z, GMP_RNDN);
                    125:   if(!MPFR_IS_NAN(s))
                    126:     {
                    127:       printf ("evaluation of function in x=0 y=INF does not return NAN");
                    128:       exit (1);
                    129:     }
                    130:
                    131:   MPFR_CLEAR_FLAGS(z);
                    132:   MPFR_CLEAR_FLAGS(y);
                    133:   MPFR_CLEAR_FLAGS(x);
                    134:
                    135:   MPFR_SET_INF(x);
                    136:   mpfr_random(y);
                    137:   MPFR_SET_INF(z);
                    138:   if((MPFR_SIGN(x) * MPFR_SIGN(y)) == MPFR_SIGN(z))
                    139:     MPFR_CHANGE_SIGN(z);
                    140:   mpfr_fma (s,x, y,z, GMP_RNDN);
                    141:   if(!MPFR_IS_NAN(s))
                    142:     {
                    143:       printf ("evaluation of function in x=INF z=(-sign(x)*sign(y))INF does not return NAN");
                    144:       exit (1);
                    145:     }
                    146:
                    147:   MPFR_CLEAR_FLAGS(z);
                    148:   MPFR_CLEAR_FLAGS(y);
                    149:   MPFR_CLEAR_FLAGS(x);
                    150:
                    151:   MPFR_SET_INF(y);
                    152:   mpfr_random(x);
                    153:   MPFR_SET_INF(z);
                    154:   if((MPFR_SIGN(x) * MPFR_SIGN(y)) == MPFR_SIGN(z))
                    155:     MPFR_CHANGE_SIGN(z);
                    156:
                    157:   mpfr_fma (s,x, y,z, GMP_RNDN);
                    158:   if(!MPFR_IS_NAN(s))
                    159:     {
                    160:       printf ("evaluation of function in y=INF z=(-sign(x)*sign(y))INF does not return NAN");
                    161:       exit (1);
                    162:     }
                    163:
                    164:   MPFR_CLEAR_FLAGS(z);
                    165:   MPFR_CLEAR_FLAGS(y);
                    166:   MPFR_CLEAR_FLAGS(x);
                    167:
                    168:   MPFR_SET_INF(x);
                    169:   mpfr_random(y);
                    170:   mpfr_random(z);
                    171:   mpfr_fma (s,x, y,z, GMP_RNDN);
                    172:   if(!MPFR_IS_INF(s))
                    173:     {
                    174:       printf ("evaluation of function in x=INF does not return INF");
                    175:       exit (1);
                    176:     }
                    177:
                    178:   MPFR_CLEAR_FLAGS(z);
                    179:   MPFR_CLEAR_FLAGS(y);
                    180:   MPFR_CLEAR_FLAGS(x);
                    181:
                    182:   MPFR_SET_INF(y);
                    183:   mpfr_random(x);
                    184:   mpfr_random(z);
                    185:   mpfr_fma (s,x, y,z, GMP_RNDN);
                    186:   if(!MPFR_IS_INF(s))
                    187:     {
                    188:       printf ("evaluation of function in y=INF does not return INF");
                    189:       exit (1);
                    190:     }
                    191:
                    192:   MPFR_CLEAR_FLAGS(z);
                    193:   MPFR_CLEAR_FLAGS(y);
                    194:   MPFR_CLEAR_FLAGS(x);
                    195:
                    196:   MPFR_SET_INF(z);
                    197:   mpfr_random(x);
                    198:   mpfr_random(y);
                    199:   mpfr_fma (s,x, y,z, GMP_RNDN);
                    200:   if(!MPFR_IS_INF(s))
                    201:     {
                    202:       printf ("evaluation of function in z=INF does not return INF");
                    203:       exit (1);
                    204:     }
                    205:
                    206:   MPFR_CLEAR_FLAGS(z);
                    207:   MPFR_CLEAR_FLAGS(y);
                    208:   MPFR_CLEAR_FLAGS(x);
                    209:
                    210:   MPFR_SET_ZERO(x);
                    211:   mpfr_random(y);
                    212:   mpfr_random(z);
                    213:   mpfr_fma (s,x, y,z, GMP_RNDN);
                    214:   if(mpfr_cmp(s,z)!=0)
                    215:     {
                    216:       printf ("evaluation of function in x=0 does not return z\n");
                    217:       exit (1);
                    218:     }
                    219:
                    220:   MPFR_CLEAR_FLAGS(z);
                    221:   MPFR_CLEAR_FLAGS(y);
                    222:   MPFR_CLEAR_FLAGS(x);
                    223:
                    224:   MPFR_SET_ZERO(y);
                    225:   mpfr_random(x);
                    226:   mpfr_random(z);
                    227:   mpfr_fma (s,x, y,z, GMP_RNDN);
                    228:   if(mpfr_cmp(s,z)!=0)
                    229:     {
                    230:       printf ("evaluation of function in y=0 does not return z\n");
                    231:       exit (1);
                    232:     }
                    233:
                    234:   MPFR_CLEAR_FLAGS(z);
                    235:   MPFR_CLEAR_FLAGS(y);
                    236:   MPFR_CLEAR_FLAGS(x);
                    237:
                    238:   {
                    239:     mp_prec_t prec;
                    240:     mpfr_t t, slong;
                    241:     mp_rnd_t rnd;
                    242:     int inexact, compare;
                    243:     unsigned int n;
                    244:
                    245:     int p0=2;
                    246:     int p1=200;
                    247:     int N=200;
                    248:
                    249:     mpfr_init (t);
                    250:     mpfr_init (slong);
                    251:
                    252:     /* generic test */
                    253:     for (prec = p0; prec <= p1; prec++)
                    254:     {
                    255:       mpfr_set_prec (x, prec);
                    256:       mpfr_set_prec (y, prec);
                    257:       mpfr_set_prec (z, prec);
                    258:       mpfr_set_prec (s, prec);
                    259:       mpfr_set_prec (t, prec);
                    260:
                    261:       for (n=0; n<N; n++)
                    262:        {
                    263:           mpfr_random (x);
                    264:          mpfr_random (y);
                    265:          mpfr_random (z);
                    266:
                    267:           if (random() % 2)
                    268:             mpfr_neg (x, x, GMP_RNDN);
                    269:           if (random() % 2)
                    270:             mpfr_neg (y, y, GMP_RNDN);
                    271:           if (random() % 2)
                    272:             mpfr_neg (z, z, GMP_RNDN);
                    273:
                    274:          rnd = random () % 4;
                    275:          mpfr_set_prec (slong, 2 * prec);
                    276:          if (mpfr_mul (slong, x, y, rnd))
                    277:            {
                    278:              fprintf (stderr, "x*y should be exact\n");
                    279:              exit (1);
                    280:            }
                    281:          compare = mpfr_add (t, slong, z, rnd);
                    282:          inexact = mpfr_fma (s, x, y, z, rnd);
                    283:          if (mpfr_cmp (s, t))
                    284:            {
                    285:              printf ("results differ for x=");
                    286:              mpfr_out_str (stdout, 2, prec, x, GMP_RNDN);
                    287:              printf ("  y=");
                    288:              mpfr_out_str (stdout, 2, prec, y, GMP_RNDN);
                    289:              printf ("  z=");
                    290:              mpfr_out_str (stdout, 2, prec, z, GMP_RNDN);
                    291:              printf (" prec=%u rnd_mode=%s\n", (unsigned) prec,
                    292:                      mpfr_print_rnd_mode (rnd));
                    293:              printf ("got      ");
                    294:              mpfr_out_str (stdout, 2, prec, s, GMP_RNDN);
                    295:              putchar ('\n');
                    296:              printf ("expected ");
                    297:              mpfr_out_str (stdout, 2, prec, t, GMP_RNDN);
                    298:              putchar ('\n');
                    299:              printf ("approx  ");
                    300:              mpfr_print_binary (slong);
                    301:              putchar ('\n');
                    302:              exit (1);
                    303:            }
                    304:          if (((inexact == 0) && (compare != 0)) ||
                    305:              ((inexact < 0) && (compare >= 0)) ||
                    306:              ((inexact > 0) && (compare <= 0)))
                    307:           {
                    308:             fprintf (stderr, "Wrong inexact flag for rnd=%s: expected %d, got %d\n",
                    309:                       mpfr_print_rnd_mode (rnd), compare, inexact);
                    310:             fprintf (stderr, "x="); mpfr_out_str (stderr, 2, 0, x, GMP_RNDN);
                    311:             fprintf (stderr, " y="); mpfr_out_str (stderr, 2, 0, y, GMP_RNDN);
                    312:             fprintf (stderr, " z="); mpfr_out_str (stderr, 2, 0, z, GMP_RNDN);
                    313:             fprintf (stderr, " s="); mpfr_out_str (stderr, 2, 0, s, GMP_RNDN);
                    314:             fprintf (stderr, "\n");
                    315:             fprintf (stderr, "z=%1.20e s=%1.20e\n", mpfr_get_d1 (z),
                    316:                      mpfr_get_d1 (s));
                    317:             exit (1);
                    318:            }
                    319:        }
                    320:     }
                    321:   mpfr_clear (t);
                    322:   mpfr_clear (slong);
                    323:
                    324:   }
                    325:   mpfr_clear (x);
                    326:   mpfr_clear (y);
                    327:   mpfr_clear (z);
                    328:   mpfr_clear (s);
                    329:
                    330:   return 0;
                    331: }

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