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

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

1.1       maekawa     1: /* Test file for mpfr_set_d and mpfr_get_d.
                      2:
1.1.1.2 ! ohara       3: Copyright 1999, 2000, 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:
1.1.1.2 ! ohara      22: #include <float.h>
1.1       maekawa    23: #include <stdio.h>
                     24: #include <stdlib.h>
                     25: #include <time.h>
                     26: #include "gmp.h"
                     27: #include "mpfr.h"
1.1.1.2 ! ohara      28: #include "mpfr-test.h"
1.1       maekawa    29:
                     30: int
1.1.1.2 ! ohara      31: main (int argc, char *argv[])
1.1       maekawa    32: {
1.1.1.2 ! ohara      33:   mpfr_t x, y, z;
        !            34:   unsigned long k, n;
        !            35:   double d, dd;
        !            36:
        !            37:   mpfr_test_init ();
        !            38:
        !            39:   mpfr_init2 (x, 2);
        !            40:
        !            41:   /* checks that denormalized are not flushed to zero */
        !            42:   d = DBL_MIN; /* 2^(-1022) */
        !            43:   for (n=0; n<52; n++, d /= 2.0)
        !            44:     if (d != 0.0) /* should be 2^(-1022-n) */
        !            45:       {
        !            46:         mpfr_set_d (x, d, GMP_RNDN);
        !            47:         if (mpfr_cmp_ui_2exp (x, 1, -1022-n))
        !            48:           {
        !            49:             fprintf (stderr, "Wrong result for d=2^(%ld), ", -1022-n);
        !            50:             fprintf (stderr, "got ");
        !            51:             mpfr_out_str (stderr, 10, 10, x, GMP_RNDN);
        !            52:             fprintf (stderr, "\n");
        !            53:             mpfr_print_binary (x); putchar ('\n');
        !            54:             exit (1);
        !            55:           }
        !            56:       }
        !            57:
        !            58:    /* checks that rounds to nearest sets the last
        !            59:      bit to zero in case of equal distance */
        !            60:    mpfr_set_d (x, 5.0, GMP_RNDN);
        !            61:    if (mpfr_get_d1 (x) != 4.0)
        !            62:      {
        !            63:        fprintf (stderr, "Error in tset_d: got %g instead of 4.0\n",
        !            64:               mpfr_get_d1 (x));
        !            65:        exit (1);
        !            66:      }
        !            67:    mpfr_set_d (x, -5.0, GMP_RNDN);
        !            68:    if (mpfr_get_d1 (x) != -4.0)
        !            69:      {
        !            70:        fprintf (stderr, "Error in tset_d: got %g instead of -4.0\n",
        !            71:               mpfr_get_d1 (x));
        !            72:        exit (1);
        !            73:      }
        !            74:
        !            75:    mpfr_set_d (x, 9.84891017624509146344e-01, GMP_RNDU);
        !            76:    if (mpfr_get_d1 (x) != 1.0)
        !            77:      {
        !            78:        fprintf (stderr, "Error in tset_d: got %g instead of 1.0\n",
        !            79:                mpfr_get_d1 (x));
        !            80:        exit (1);
        !            81:      }
1.1       maekawa    82:
                     83:   mpfr_init2(z, 32);
                     84:   mpfr_set_d(z, 1.0, 0);
1.1.1.2 ! ohara      85:   if (mpfr_get_d1 (z) != 1.0) {
        !            86:     mpfr_print_binary(z); putchar('\n');
1.1       maekawa    87:     printf("Error: 1.0 != 1.0\n"); exit(1);
                     88:   }
1.1.1.2 ! ohara      89:   mpfr_set_prec(x, 53); mpfr_init2(y, 53);
1.1       maekawa    90:   mpfr_set_d(x, d=-1.08007920352320089721e+150, 0);
1.1.1.2 ! ohara      91:   if (mpfr_get_d1 (x) != d) {
        !            92:     mpfr_print_binary(x); putchar('\n');
1.1       maekawa    93:     printf("Error: get_d o set_d <> identity for d = %1.20e %1.20e\n",d,
1.1.1.2 ! ohara      94:           mpfr_get_d1 (x)); exit(1);
1.1       maekawa    95:   }
1.1.1.2 ! ohara      96:
        !            97:   SEED_RAND (time(NULL));
1.1       maekawa    98:   mpfr_set_d(x, 8.06294740693074521573e-310, 0);
                     99:   d = -6.72658901114033715233e-165;
                    100:   mpfr_set_d(x, d, 0);
1.1.1.2 ! ohara     101:   if (d != mpfr_get_d1 (x)) {
        !           102:     mpfr_print_binary(x); putchar('\n');
1.1       maekawa   103:     printf("Error: get_d o set_d <> identity for d = %1.20e %1.20e\n",d,
1.1.1.2 ! ohara     104:           mpfr_get_d1 (x)); exit(1);
1.1       maekawa   105:   }
                    106:   n = (argc==1) ? 1000000 : atoi(argv[1]);
                    107:   for (k = 1; k <= n; k++)
                    108:     {
1.1.1.2 ! ohara     109:       do
        !           110:        {
        !           111:          d = drand();
        !           112:        }
        !           113: #ifdef HAVE_DENORMS
        !           114:       while (0);
        !           115: #else
        !           116:       while (ABS(d) <= 2.2e-307);
        !           117: #endif
        !           118:       mpfr_set_d (x, d, 0);
        !           119:       dd = mpfr_get_d1 (x);
1.1       maekawa   120:       if (d != dd && (!isnan(d) || !isnan(dd)))
                    121:        {
                    122:          fprintf(stderr,
1.1.1.2 ! ohara     123:                  "Mismatch on : %1.18g != %1.18g\n", d, mpfr_get_d1 (x));
        !           124:          mpfr_print_binary(x); putchar('\n');
        !           125:          exit(1);
        !           126:        }
1.1       maekawa   127:     }
                    128:
                    129:   mpfr_clear(x); mpfr_clear(y); mpfr_clear(z);
                    130:
1.1.1.2 ! ohara     131:   return 0;
        !           132: }

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