Annotation of OpenXM_contrib/gmp/mpfr/tests/tget_str.c, Revision 1.1
1.1 ! ohara 1: /* Test file for mpfr_get_str.
! 2:
! 3: Copyright 1999, 2001, 2002 Free Software Foundation, Inc.
! 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 <math.h>
! 23: #include <stdio.h>
! 24: #include <stdlib.h>
! 25: #include <string.h>
! 26: #include <time.h>
! 27: #include "gmp.h"
! 28: #include "mpfr.h"
! 29: #ifdef MPFR_HAVE_FESETROUND
! 30: #include "mpfr-test.h"
! 31: #endif
! 32:
! 33: void check _PROTO((double, mp_rnd_t));
! 34: void check3 _PROTO((double, mp_rnd_t, char *));
! 35: void check_small _PROTO((void));
! 36:
! 37: void
! 38: check (double d, mp_rnd_t rnd)
! 39: {
! 40: mpfr_t x; char *str; mp_exp_t e;
! 41:
! 42: mpfr_init2(x, 53);
! 43: mpfr_set_d(x, d, rnd);
! 44: str = mpfr_get_str(NULL, &e, 10, 5, x, rnd);
! 45: mpfr_clear(x);
! 46: free(str);
! 47: }
! 48:
! 49: void
! 50: check3 (double d, mp_rnd_t rnd, char *res)
! 51: {
! 52: mpfr_t x; char *str; mp_exp_t e;
! 53:
! 54: mpfr_init2 (x, 53);
! 55: mpfr_set_d (x, d, rnd);
! 56: str = mpfr_get_str (NULL, &e, 10, 5, x, rnd);
! 57: if (strcmp(str, res))
! 58: {
! 59: fprintf (stderr, "Error in mpfr_get_str for x=%1.20e\n", d);
! 60: fprintf (stderr, "got %s instead of %s\n", str, res);
! 61: }
! 62: mpfr_clear (x);
! 63: free (str);
! 64: }
! 65:
! 66: void
! 67: check_small (void)
! 68: {
! 69: mpfr_t x;
! 70: char *s;
! 71: mp_exp_t e;
! 72:
! 73: mpfr_init(x);
! 74:
! 75: /* problem found by Fabrice Rouillier */
! 76: mpfr_set_prec(x, 63);
! 77: mpfr_set_d(x, 5e14, GMP_RNDN);
! 78: s = mpfr_get_str(NULL, &e, 10, 18, x, GMP_RNDU);
! 79: free(s);
! 80:
! 81: /* bug found by Johan Vervloet */
! 82: mpfr_set_prec(x, 6);
! 83: mpfr_set_d(x, 688.0, GMP_RNDN);
! 84: s = mpfr_get_str(NULL, &e, 2, 4, x, GMP_RNDU);
! 85: if (strcmp(s, "1011") || (e!=10)) {
! 86: fprintf(stderr, "Error in mpfr_get_str: 688 printed up to 4 bits should give 1.011e9\ninstead of ");
! 87: mpfr_out_str(stderr, 2, 4, x, GMP_RNDU); putchar('\n');
! 88: exit(1);
! 89: }
! 90: free(s);
! 91:
! 92: mpfr_set_prec (x, 38);
! 93: mpfr_set_str_raw (x, "1.0001110111110100011010100010010100110e-6");
! 94: s = mpfr_get_str (NULL, &e, 8, 10, x, GMP_RNDU);
! 95: if (strcmp (s, "1073721522") || (e != -1))
! 96: {
! 97: fprintf (stderr, "Error in mpfr_get_str (3): s=%s e=%d\n", s, (int) e);
! 98: exit (1);
! 99: }
! 100: free (s);
! 101:
! 102: mpfr_clear(x);
! 103: }
! 104:
! 105: int
! 106: main (int argc, char *argv[])
! 107: {
! 108: #ifdef MPFR_HAVE_FESETROUND
! 109: int i;
! 110: double d;
! 111:
! 112: SEED_RAND (time(NULL));
! 113: for (i=0;i<100000;i++) {
! 114: do { d = drand(); } while (isnan(d));
! 115: check(d, GMP_RNDN);
! 116: }
! 117: #endif
! 118: check_small();
! 119: check3(4.059650008e-83, GMP_RNDN, "40597");
! 120: check3(-6.606499965302424244461355e233, GMP_RNDN, "-66065");
! 121: check3(-7.4, GMP_RNDN, "-74000");
! 122: check3(0.997, GMP_RNDN, "99700");
! 123: check3(-4.53063926135729747564e-308, GMP_RNDN, "-45306");
! 124: check3(2.14478198760196000000e+16, GMP_RNDN, "21448");
! 125: check3(7.02293374921793516813e-84, GMP_RNDN, "70229");
! 126: check3(-6.7274500420134077e-87, GMP_RNDN, "-67275");
! 127:
! 128: return 0;
! 129: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>