Annotation of OpenXM_contrib/gmp/mpfr/tests/tset_si.c, Revision 1.1.1.2
1.1 maekawa 1: /* Test file for mpfr_set_si and mpfr_set_ui.
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 <time.h>
1.1 maekawa 25: #include "gmp.h"
26: #include "mpfr.h"
1.1.1.2 ! ohara 27: #include "mpfr-impl.h"
! 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;
! 34: long k, z, d;
! 35: unsigned long zl, dl, N;
! 36: int inex;
1.1 maekawa 37:
38: mpfr_init2(x, 100);
39:
1.1.1.2 ! ohara 40: SEED_RAND (time(NULL));
1.1 maekawa 41:
42: N = (argc==1) ? 1000000 : atoi(argv[1]);
43:
44: for (k = 1; k <= N; k++)
45: {
46: z = random() - (1 << 30);
1.1.1.2 ! ohara 47: inex = mpfr_set_si(x, z, GMP_RNDZ);
! 48: d = (long) mpfr_get_d1 (x);
! 49: if (d != z) {
! 50: fprintf(stderr, "Error in mpfr_set_si: expected %ld got %ld\n", z, d); exit(1);
! 51: }
! 52: if (inex)
! 53: {
! 54: fprintf(stderr,
! 55: "Error in mpfr_set_si: inex value incorrect for %ld: %d\n",
! 56: z, inex);
! 57: exit(1);
! 58: }
1.1 maekawa 59: }
60:
61: for (k = 1; k <= N; k++)
62: {
63: zl = random();
1.1.1.2 ! ohara 64: inex = mpfr_set_ui (x, zl, GMP_RNDZ);
! 65: dl = (unsigned long) mpfr_get_d1 (x);
! 66: if (dl != zl) {
! 67: fprintf(stderr, "Error in mpfr_set_ui: expected %lu got %lu\n", zl, dl); exit(1);
! 68: }
! 69: if (inex)
! 70: {
! 71: fprintf(stderr,
! 72: "Error in mpfr_set_ui: inex value incorrect for %lu: %d\n",
! 73: zl, inex);
! 74: exit(1);
! 75: }
! 76: }
! 77:
! 78: mpfr_set_prec (x, 2);
! 79: if (mpfr_set_si (x, 5, GMP_RNDZ) >= 0)
! 80: {
! 81: fprintf (stderr, "Wrong inexact flag for x=5, rnd=GMP_RNDZ\n");
! 82: exit (1);
! 83: }
! 84:
! 85: mpfr_set_prec (x, 2);
! 86: if (mpfr_set_si (x, -5, GMP_RNDZ) <= 0)
! 87: {
! 88: fprintf (stderr, "Wrong inexact flag for x=-5, rnd=GMP_RNDZ\n");
! 89: exit (1);
! 90: }
! 91:
! 92: mpfr_set_prec (x, 3);
! 93: inex = mpfr_set_si(x, 77617, GMP_RNDD); /* should be 65536 */
! 94: if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1))
! 95: || inex >= 0)
! 96: {
! 97: fprintf(stderr, "Error in mpfr_set_si(x:3, 77617, GMP_RNDD)\n");
! 98: mpfr_print_binary(x); putchar('\n');
! 99: exit(1);
! 100: }
! 101: inex = mpfr_set_ui(x, 77617, GMP_RNDD); /* should be 65536 */
! 102: if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1))
! 103: || inex >= 0)
! 104: {
! 105: fprintf(stderr, "Error in mpfr_set_ui(x:3, 77617, GMP_RNDD)\n");
! 106: mpfr_print_binary(x); putchar('\n');
! 107: exit(1);
! 108: }
! 109:
! 110: mpfr_set_prec(x, 2);
! 111: inex = mpfr_set_si(x, 33096, GMP_RNDU);
! 112: if (mpfr_get_d1 (x) != 49152.0 || inex <= 0)
! 113: {
! 114: fprintf(stderr, "Error in mpfr_set_si, expected 49152, got %lu, inex %d\n",
! 115: (unsigned long) mpfr_get_d1 (x), inex);
! 116: exit(1);
! 117: }
! 118: inex = mpfr_set_ui(x, 33096, GMP_RNDU);
! 119: if (mpfr_get_d1 (x) != 49152.0)
! 120: {
! 121: fprintf(stderr, "Error in mpfr_set_ui, expected 49152, got %lu, inex %d\n",
! 122: (unsigned long) mpfr_get_d1 (x), inex);
! 123: exit(1);
! 124: }
! 125:
! 126: mpfr_set_si (x, -1, GMP_RNDN);
! 127: mpfr_set_ui (x, 0, GMP_RNDN);
! 128: if (MPFR_SIGN (x) < 0)
! 129: {
! 130: fprintf (stderr, "mpfr_set_ui (x, 0) gives -0\n");
! 131: exit (1);
! 132: }
! 133:
! 134: mpfr_set_si (x, -1, GMP_RNDN);
! 135: mpfr_set_si (x, 0, GMP_RNDN);
! 136: if (MPFR_SIGN (x) < 0)
! 137: {
! 138: fprintf (stderr, "mpfr_set_si (x, 0) gives -0\n");
! 139: exit (1);
! 140: }
! 141:
! 142: /* check potential bug in case mp_limb_t is unsigned */
! 143: mpfr_set_emax (0);
! 144: mpfr_set_si (x, -1, GMP_RNDN);
! 145: if (mpfr_sgn (x) >= 0)
! 146: {
! 147: fprintf (stderr, "mpfr_set_si (x, -1) fails\n");
! 148: exit (1);
! 149: }
! 150:
! 151: mpfr_set_emax (5);
! 152: mpfr_set_prec (x, 2);
! 153: mpfr_set_si (x, -31, GMP_RNDN);
! 154: if (mpfr_sgn (x) >= 0)
! 155: {
! 156: fprintf (stderr, "mpfr_set_si (x, -31) fails\n");
! 157: exit (1);
1.1 maekawa 158: }
159:
160: mpfr_clear(x);
1.1.1.2 ! ohara 161:
! 162: return 0;
1.1 maekawa 163: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>