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

Annotation of OpenXM_contrib/gmp/mpfr/tests/tmul_ui.c, Revision 1.1.1.2

1.1       maekawa     1: /* Test file for mpfr_mul_ui.
                      2:
1.1.1.2 ! ohara       3: Copyright 1999, 2000, 2001, 2002 Free Software Foundation.
1.1       maekawa     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
1.1.1.2 ! ohara       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
1.1       maekawa    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
1.1.1.2 ! ohara      14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    15: License for more details.
                     16:
1.1.1.2 ! ohara      17: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    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>
1.1.1.2 ! ohara      24: #include <math.h>
1.1       maekawa    25: #include "gmp.h"
                     26: #include "gmp-impl.h"
                     27: #include "mpfr.h"
1.1.1.2 ! ohara      28: #include "mpfr-impl.h"
        !            29: #include "mpfr-test.h"
        !            30:
        !            31: void check_inexact _PROTO((mp_prec_t));
        !            32:
        !            33: void
        !            34: check_inexact (mp_prec_t p)
        !            35: {
        !            36:   mpfr_t x, y, z;
        !            37:   unsigned long u;
        !            38:   mp_prec_t q;
        !            39:   int inexact, cmp;
        !            40:   mp_rnd_t rnd;
        !            41:
        !            42:   mpfr_init2 (x, p);
        !            43:   mpfr_init (y);
        !            44:   mpfr_init2 (z, p + mp_bits_per_limb);
        !            45:   mpfr_random (x);
        !            46:   u = LONG_RAND();
        !            47:   if (mpfr_mul_ui (z, x, u, GMP_RNDN))
        !            48:     {
        !            49:       fprintf (stderr, "Error: result should be exact\n");
        !            50:       exit (1);
        !            51:     }
        !            52:
        !            53:   for (q=2; q<=p; q++)
        !            54:     for (rnd=0; rnd<4; rnd++)
        !            55:       {
        !            56:        mpfr_set_prec (y, q);
        !            57:        inexact = mpfr_mul_ui (y, x, u, rnd);
        !            58:        cmp = mpfr_cmp (y, z);
        !            59:        if (((inexact == 0) && (cmp != 0)) ||
        !            60:            ((inexact < 0) && (cmp >= 0)) ||
        !            61:            ((inexact > 0) && (cmp <= 0)))
        !            62:          {
        !            63:            fprintf (stderr, "Wrong inexact flag for p=%u, q=%u, rnd=%s\n",
        !            64:                     (unsigned) p, (unsigned) q, mpfr_print_rnd_mode (rnd));
        !            65:            exit (1);
        !            66:          }
        !            67:       }
        !            68:
        !            69:   mpfr_set_prec (x, 2);
        !            70:   mpfr_set_ui (x, 1, GMP_RNDN);
        !            71:   if (mpfr_mul_ui (x, x, 5, GMP_RNDZ) == 0)
        !            72:     {
        !            73:       fprintf (stderr, "mul_ui(1, 5) cannot be exact with prec=2\n");
        !            74:       exit (1);
        !            75:     }
        !            76:
        !            77:   mpfr_clear (x);
        !            78:   mpfr_clear (y);
        !            79:   mpfr_clear (z);
        !            80: }
1.1       maekawa    81:
                     82: int
1.1.1.2 ! ohara      83: main (int argc, char *argv[])
1.1       maekawa    84: {
                     85:   mpfr_t x, y;
1.1.1.2 ! ohara      86:   unsigned int xprec, yprec, i;
        !            87:   mp_prec_t p;
        !            88:
        !            89:   for (p=2; p<100; p++)
        !            90:     for (i=1; i<50; i++)
        !            91:       check_inexact (p);
1.1       maekawa    92:
1.1.1.2 ! ohara      93:   mpfr_init2 (x, 53);
        !            94:   mpfr_init2 (y, 53);
1.1       maekawa    95:
                     96:   /* checks that result is normalized */
1.1.1.2 ! ohara      97:   mpfr_set_d (y, 6.93147180559945286227e-01, GMP_RNDZ);
        !            98:   mpfr_mul_ui (x, y, 1, GMP_RNDZ);
        !            99:   if (MPFR_MANT(x)[MPFR_PREC(x)/mp_bits_per_limb] >> (mp_bits_per_limb-1) == 0)
        !           100:     {
        !           101:       fprintf (stderr, "Error in mpfr_mul_ui: result not normalized\n");
        !           102:       exit (1);
        !           103:     }
        !           104:   if (mpfr_cmp (x, y))
        !           105:     {
        !           106:       fprintf (stderr, "Error in mpfr_mul_ui: 1*y != y\n");
        !           107:       printf ("y=  "); mpfr_print_binary (y); putchar ('\n');
        !           108:       printf ("1*y="); mpfr_print_binary (x); putchar ('\n');
        !           109:       exit (1);
1.1       maekawa   110:   }
                    111:
1.1.1.2 ! ohara     112:
        !           113:   mpfr_set_inf (x, 1);
        !           114:   mpfr_mul_ui (x, x, 3, GMP_RNDU);
        !           115:   if (!mpfr_inf_p (x) || (mpfr_sgn (x) <= 0))
        !           116:     {
        !           117:       fprintf (stderr, "Error in mpfr_mul_ui: +Inf*3 does not give +Inf\n");
        !           118:       exit (1);
        !           119:     }
        !           120:
        !           121:   mpfr_set_inf (x, -1);
        !           122:   mpfr_mul_ui (x, x, 3, GMP_RNDU);
        !           123:   if (!mpfr_inf_p (x) || (mpfr_sgn (x) >= 0))
        !           124:     {
        !           125:       fprintf (stderr, "Error in mpfr_mul_ui: -Inf*3 does not give -Inf\n");
        !           126:       exit (1);
        !           127:     }
        !           128:
        !           129:   mpfr_set_nan (x);
        !           130:   mpfr_mul_ui (x, x, 3, GMP_RNDU);
        !           131:   if (!mpfr_nan_p(x))
        !           132:     {
        !           133:       fprintf (stderr, "Error in mpfr_mul_ui: NaN*3 does not give NaN\n");
        !           134:       exit (1);
        !           135:     }
        !           136:
        !           137:   mpfr_set_d (x, 1.0/3.0, GMP_RNDZ);
        !           138:   mpfr_mul_ui (x, x, 3, GMP_RNDU);
        !           139:   if (mpfr_get_d1 (x) != 1.0)
        !           140:     {
        !           141:       fprintf (stderr, "Error in mpfr_mul_ui: U(Z(1/3)*3) does not give 1\n");
        !           142:       exit (1);
        !           143:     }
1.1       maekawa   144:
                    145:   /* checks sign is correct */
                    146:   mpfr_set_d(x, -2.0, GMP_RNDZ);
                    147:   mpfr_set_d(y, 3.0, GMP_RNDZ);
                    148:   mpfr_mul_ui(x, y, 4, GMP_RNDZ);
1.1.1.2 ! ohara     149:   if (mpfr_cmp_ui(x, 0) <= 0) {
        !           150:     fprintf(stderr, "Error in mpfr_mul_ui: 4*3.0 does not give a positive result:\n");
        !           151:     mpfr_print_binary(x); putchar('\n');
        !           152:     printf("mpfr_cmp_ui(x, 0) = %d\n", mpfr_cmp_ui(x, 0));
        !           153:     exit(1);
        !           154:   }
        !           155:
        !           156:   mpfr_set_prec (x, 9);
        !           157:   mpfr_set_prec (y, 9);
        !           158:   mpfr_set_str_raw (y, "0.100001111E9"); /* 271 */
        !           159:   mpfr_mul_ui (x, y, 1335, GMP_RNDN);
        !           160:   mpfr_set_str_raw (y, "0.101100001E19"); /* 361472 */
        !           161:   if (mpfr_cmp (x, y))
        !           162:     {
        !           163:       fprintf (stderr, "Error in mul_ui for 1335*(0.100001111E9)\n");
        !           164:       printf ("got "); mpfr_print_binary (x); putchar ('\n');
        !           165:       exit(1);
        !           166:     }
        !           167:
        !           168:   mpfr_set_prec(y, 100);
        !           169:   mpfr_set_prec(x, 100);
        !           170:   /* y = 1199781142214086656 */
        !           171:   mpfr_set_str_raw(y, "0.1000010100110011110101001011110010101111000100001E61");
        !           172:   mpfr_mul_ui(x, y, 121, GMP_RNDD);
        !           173:   /* 121*y = 145173518207904485376, representable exactly */
        !           174:   mpfr_set_str_raw(y, "0.1111101111010101111111100011010010111010111110110011001E67");
        !           175:   if (mpfr_cmp(x, y)) {
        !           176:     printf("Error for 121*y: expected result is:\n");
        !           177:     mpfr_print_binary(y); putchar('\n');
1.1       maekawa   178:   }
                    179:
1.1.1.2 ! ohara     180:   mpfr_set_prec (x, 32);
        !           181:   mpfr_set_str_raw (x, "0.10000000000000000000000000000000E1");
        !           182:   mpfr_set_prec (y, 93);
        !           183:   mpfr_mul_ui (y, x, 1, GMP_RNDN);
        !           184:
        !           185:   mpfr_set_prec (x, 287);
        !           186:   mpfr_set_str_raw (x, "0.1111E7");
        !           187:   mpfr_set_prec (y, 289);
        !           188:   mpfr_mul_ui (y, x, 6, GMP_RNDN);
        !           189:   mpfr_set_str_raw (x, "0.101101E10");
        !           190:   if (mpfr_cmp (x, y))
        !           191:     {
        !           192:       printf ("Error for 6 * 120\n");
        !           193:       exit (1);
        !           194:     }
        !           195:
        !           196:   mpfr_set_prec (x, 68);
        !           197:   mpfr_set_prec (y, 64);
        !           198:   mpfr_set_d (x, 2143861251406875.0, GMP_RNDN);
        !           199:   mpfr_mul_ui (y, x, 23, GMP_RNDN);
        !           200:   mpfr_set_str_raw (x, "10101111001011100001100110101111110001010010011001101101.0");
        !           201:   if (mpfr_cmp (x, y))
        !           202:     {
        !           203:       printf ("Error for 23 * 2143861251406875.0\n");
        !           204:       printf ("expected "); mpfr_print_binary (x); putchar ('\n');
        !           205:       printf ("got      "); mpfr_print_binary (y); putchar ('\n');
        !           206:       exit (1);
        !           207:     }
        !           208:
        !           209:
        !           210:   for (xprec = 53; xprec <= 128; xprec++)
        !           211:     {
        !           212:       mpfr_set_prec (x, xprec);
        !           213:       mpfr_set_str_raw (x, "0.1100100100001111110011111000000011011100001100110111E2");
        !           214:       for (yprec = 53; yprec <= 128; yprec++)
        !           215:        {
        !           216:          mpfr_set_prec (y, yprec);
        !           217:          mpfr_mul_ui (y, x, 1, GMP_RNDN);
        !           218:          if (mpfr_get_d1 (x) != mpfr_get_d1 (y))
        !           219:            {
        !           220:              fprintf (stderr, "multiplication by 1.0 fails for xprec=%u, yprec=%u\n", xprec, yprec);
        !           221:              printf ("expected "); mpfr_print_binary (x); putchar ('\n');
        !           222:              printf ("got      "); mpfr_print_binary (y); putchar ('\n');
        !           223:              exit (1);
        !           224:            }
        !           225:        }
        !           226:     }
        !           227:
        !           228:   mpfr_set_prec (x, 128);
        !           229:   mpfr_set_ui (x, 17, GMP_RNDN);
        !           230:   mpfr_mul_ui (x, x, ULONG_HIGHBIT, GMP_RNDN);
        !           231:   mpfr_set_prec (y, 128);
        !           232:   mpfr_set_ui (y, ULONG_HIGHBIT, GMP_RNDN);
        !           233:   mpfr_mul_ui (y, y, 17, GMP_RNDN);
        !           234:   if (mpfr_cmp (x, y))
        !           235:     {
        !           236:       printf ("Error for 17 * ULONG_HIGHBIT\n");
        !           237:       exit (1);
        !           238:     }
        !           239:
        !           240:
1.1       maekawa   241:   mpfr_clear(x); mpfr_clear(y);
1.1.1.2 ! ohara     242:
        !           243:   return 0;
1.1       maekawa   244: }

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