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

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

1.1       maekawa     1: /* Test file for mpfr_set_str.
                      2:
1.1.1.2 ! ohara       3: Copyright 1999, 2001, 2002 Free Software Foundation, Inc.
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 <string.h>
        !            25: #include <time.h>
1.1       maekawa    26: #include "gmp.h"
                     27: #include "mpfr.h"
1.1.1.2 ! ohara      28: #include "mpfr-impl.h"
        !            29: #include "mpfr-test.h"
1.1       maekawa    30:
                     31: int
1.1.1.2 ! ohara      32: main (int argc, char *argv[])
1.1       maekawa    33: {
1.1.1.2 ! ohara      34:   mpfr_t x, y;
        !            35:   unsigned long k, bd, nc, i;
        !            36:   char *str, *str2;
        !            37:   mp_exp_t e;
        !            38:   int base, logbase, prec, baseprec;
1.1       maekawa    39:
1.1.1.2 ! ohara      40:   if (argc>=2) /* tset_str <string> <prec> */
        !            41:     {
        !            42:       prec = (argc>=3) ? atoi(argv[2]) : 53;
        !            43:       mpfr_init2 (x, prec);
        !            44:       mpfr_set_str_raw (x, argv[1]);
        !            45:       mpfr_out_str (stdout, 10, 0, x, GMP_RNDN);
        !            46:       putchar ('\n');
        !            47:       mpfr_clear (x);
        !            48:       return 0;
        !            49:     }
1.1       maekawa    50:
1.1.1.2 ! ohara      51:   SEED_RAND (time(NULL));
1.1       maekawa    52:
1.1.1.2 ! ohara      53:   nc = (argc > 1) ? atoi(argv[1]) : 53;
        !            54:   if (nc < 100)
        !            55:     nc = 100;
1.1       maekawa    56:
1.1.1.2 ! ohara      57:   bd = LONG_RAND() & 8;
1.1       maekawa    58:
1.1.1.2 ! ohara      59:   str2 = str = (char *) malloc (nc * sizeof(char));
1.1       maekawa    60:
1.1.1.2 ! ohara      61:   if (bd)
1.1       maekawa    62:     {
1.1.1.2 ! ohara      63:       for(k = 1; k <= bd; k++)
        !            64:        *(str2++) = (LONG_RAND() & 1) + '0';
1.1       maekawa    65:     }
1.1.1.2 ! ohara      66:   else
        !            67:     *(str2++) = '0';
1.1       maekawa    68:
                     69:   *(str2++) = '.';
                     70:
1.1.1.2 ! ohara      71:   for (k = 1; k < nc - 17 - bd; k++)
        !            72:     *(str2++) = '0' + (LONG_RAND() & 1);
        !            73:
        !            74:   *(str2++) = 'e';
        !            75:   sprintf (str2, "%d", (int) LONG_RAND() - (1 << 30));
        !            76:
        !            77:   mpfr_init2 (x, nc + 10);
        !            78:   mpfr_set_str_raw (x, str);
        !            79:
        !            80:   mpfr_set_prec (x, 54);
        !            81:   mpfr_set_str_raw (x, "0.100100100110110101001010010101111000001011100100101010E-529");
        !            82:   mpfr_init2 (y, 54);
        !            83:   mpfr_set_str (y, "4.936a52bc17254@-133", 16, GMP_RNDN);
        !            84:   if (mpfr_cmp (x, y))
1.1       maekawa    85:     {
1.1.1.2 ! ohara      86:       fprintf (stderr, "Error in mpfr_set_str\n");
        !            87:       mpfr_print_binary(x);
        !            88:       putchar('\n');
        !            89:       mpfr_print_binary(y);
        !            90:       putchar('\n');
        !            91:       mpfr_clear(x);
        !            92:       mpfr_clear(y);
        !            93:       exit(1);
1.1       maekawa    94:     }
                     95:
1.1.1.2 ! ohara      96:   free(str);
        !            97:
        !            98:   mpfr_set_prec (x, 53);
        !            99:   mpfr_set_str_raw (x, "+110101100.01010000101101000000100111001000101011101110E00");
        !           100:
        !           101:   mpfr_set_str_raw (x, "1.0");
        !           102:   if (mpfr_get_d1 (x) != 1.0)
        !           103:     {
        !           104:       fprintf (stderr, "Error in mpfr_set_str_raw for s=1.0\n");
        !           105:       mpfr_clear(x);
        !           106:       mpfr_clear(y);
        !           107:       exit(1);
        !           108:     }
        !           109:
        !           110:   mpfr_set_str_raw (x, "+0000");
        !           111:   mpfr_set_str_raw (x, "+0000E0");
        !           112:   mpfr_set_str_raw (x, "0000E0");
        !           113:   if (mpfr_get_d1 (x) != 0.0)
        !           114:     {
        !           115:       fprintf (stderr, "Error in mpfr_set_str_raw for s=0.0\n");
        !           116:       mpfr_clear (x);
        !           117:       mpfr_clear (y);
        !           118:       exit (1);
        !           119:     }
        !           120:
        !           121:   mpfr_set_str (x, "+243495834958.53452345E1", 10, GMP_RNDN);
        !           122:   mpfr_set_str (x, "9007199254740993", 10, GMP_RNDN);
        !           123:   mpfr_set_str (x, "9007199254740992", 10, GMP_RNDU);
        !           124:   mpfr_set_str (x, "9007199254740992", 10, GMP_RNDD);
        !           125:   mpfr_set_str (x, "9007199254740992", 10, GMP_RNDZ);
        !           126:
        !           127:   /* check a random number printed and read is not modified */
        !           128:   prec = 53;
        !           129:   mpfr_set_prec (x, prec);
        !           130:   mpfr_set_prec (y, prec);
        !           131:   for (i=0;i<100000;i++)
        !           132:     {
        !           133:       mpfr_random (x);
        !           134:       k = LONG_RAND() % 4;
        !           135:       logbase = (LONG_RAND() % 5) + 1;
        !           136:       base = 1 << logbase;
        !           137:       /* Warning: the number of bits needed to print exactly a number of
        !           138:         'prec' bits in base 2^logbase may be greater than ceil(prec/logbase),
        !           139:         for example 0.11E-1 in base 2 cannot be written exactly with only
        !           140:         one digit in base 4 */
        !           141:       if (base == 2)
        !           142:        baseprec = prec;
        !           143:       else
        !           144:        baseprec = 1 + (prec - 2 + logbase) / logbase;
        !           145:       str = mpfr_get_str (NULL, &e, base, baseprec, x, k);
        !           146:       mpfr_set_str (y, str, base, k);
        !           147:       MPFR_EXP(y) += logbase * (e - strlen (str));
        !           148:       if (mpfr_cmp (x, y))
        !           149:        {
        !           150:          fprintf (stderr, "mpfr_set_str o mpfr_get_str <> id for rnd_mode=%s\n",
        !           151:                   mpfr_print_rnd_mode (k));
        !           152:          printf ("x=");
        !           153:          mpfr_print_binary (x);
        !           154:          putchar('\n');
        !           155:          printf ("s=%s, exp=%d, base=%d\n", str, (int) e, base);
        !           156:          printf ("y=");
        !           157:          mpfr_print_binary (y);
        !           158:          putchar('\n');
        !           159:          mpfr_clear (x);
        !           160:          mpfr_clear (y);
        !           161:          exit (1);
        !           162:        }
        !           163:       free (str);
        !           164:     }
        !           165:
        !           166:   if (mpfr_set_str (x, "NaN", 10, GMP_RNDN) != 0 || !mpfr_nan_p(x))
        !           167:     {
        !           168:       fprintf (stderr, "mpfr_set_str failed on NaN\n");
        !           169:       exit (1);
        !           170:     }
        !           171:
        !           172:   if (mpfr_set_str (x, "Inf", 10, GMP_RNDN) != 0 || !mpfr_inf_p(x) ||
        !           173:       MPFR_SIGN(x) < 0)
        !           174:     {
        !           175:       fprintf (stderr, "mpfr_set_str failed on Inf\n");
        !           176:       exit (1);
        !           177:     }
        !           178:
        !           179:   if (mpfr_set_str (x, "-Inf", 10, GMP_RNDN) != 0 || !mpfr_inf_p(x) ||
        !           180:       MPFR_SIGN(x) > 0)
        !           181:     {
        !           182:       fprintf (stderr, "mpfr_set_str failed on -Inf\n");
        !           183:       exit (1);
        !           184:     }
        !           185:
        !           186:   if (mpfr_set_str (x, "+Inf", 10, GMP_RNDN) != 0 || !mpfr_inf_p(x) ||
        !           187:       MPFR_SIGN(x) < 0)
        !           188:     {
        !           189:       fprintf (stderr, "mpfr_set_str failed on +Inf\n");
        !           190:       exit (1);
        !           191:     }
1.1       maekawa   192:
1.1.1.2 ! ohara     193:   mpfr_clear (x);
        !           194:   mpfr_clear (y);
1.1       maekawa   195:
1.1.1.2 ! ohara     196:   return 0;
1.1       maekawa   197: }

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