Annotation of OpenXM_contrib/gmp/mpfr/tests/tui_sub.c, Revision 1.1.1.1
1.1 ohara 1: /* Test file for mpfr_ui_sub.
2:
3: Copyright 2000, 2001, 2002 Free Software Foundation.
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 <time.h>
26: #include "gmp.h"
27: #include "mpfr.h"
28: #include "mpfr-impl.h"
29: #include "mpfr-test.h"
30:
31: void special _PROTO ((void));
32: void check _PROTO ((unsigned long, double, mp_rnd_t, double));
33: void check_two_sum _PROTO ((mp_prec_t));
34:
35: void
36: special (void)
37: {
38: mpfr_t x, y, res;
39: int inexact;
40:
41: mpfr_init (x);
42: mpfr_init (y);
43: mpfr_init (res);
44:
45: mpfr_set_prec (x, 24);
46: mpfr_set_prec (y, 24);
47: mpfr_set_str_raw (y, "0.111100110001011010111");
48: inexact = mpfr_ui_sub (x, 1, y, GMP_RNDN);
49: if (inexact)
50: {
51: fprintf (stderr, "Wrong inexact flag: got %d, expected 0\n", inexact);
52: exit (1);
53: }
54:
55: mpfr_set_prec (x, 24);
56: mpfr_set_prec (y, 24);
57: mpfr_set_str_raw (y, "0.111100110001011010111");
58: if ((inexact = mpfr_ui_sub (x, 38181761, y, GMP_RNDN)) >= 0)
59: {
60: fprintf (stderr, "Wrong inexact flag: got %d, expected -1\n", inexact);
61: exit (1);
62: }
63:
64: mpfr_set_prec (x, 63);
65: mpfr_set_prec (y, 63);
66: mpfr_set_str_raw (y, "0.111110010010100100110101101010001001100101110001000101110111111E-1");
67: if ((inexact = mpfr_ui_sub (x, 1541116494, y, GMP_RNDN)) <= 0)
68: {
69: fprintf (stderr, "Wrong inexact flag: got %d, expected +1\n", inexact);
70: exit (1);
71: }
72:
73: mpfr_set_prec (x, 32);
74: mpfr_set_prec (y, 32);
75: mpfr_set_str_raw (y, "0.11011000110111010001011100011100E-1");
76: if ((inexact = mpfr_ui_sub (x, 2000375416, y, GMP_RNDN)) >= 0)
77: {
78: fprintf (stderr, "Wrong inexact flag: got %d, expected -1\n", inexact);
79: exit (1);
80: }
81:
82: mpfr_set_prec (x, 24);
83: mpfr_set_prec (y, 24);
84: mpfr_set_str_raw (y, "0.110011011001010011110111E-2");
85: if ((inexact = mpfr_ui_sub (x, 927694848, y, GMP_RNDN)) <= 0)
86: {
87: fprintf (stderr, "Wrong inexact flag: got %d, expected +1\n", inexact);
88: exit (1);
89: }
90:
91: /* bug found by Mathieu Dutour, 12 Apr 2001 */
92: mpfr_set_prec (x, 5);
93: mpfr_set_prec (y, 5);
94: mpfr_set_prec (res, 5);
95: mpfr_set_str_raw (x, "1e-12");
96:
97: mpfr_ui_sub (y, 1, x, GMP_RNDD);
98: mpfr_set_str_raw (res, "0.11111");
99: if (mpfr_cmp (y, res))
100: {
101: fprintf (stderr, "Error in mpfr_ui_sub (y, 1, x, GMP_RNDD) for x=2^(-12)\nexpected 1.1111e-1, got ");
102: mpfr_out_str (stderr, 2, 0, y, GMP_RNDN);
103: fprintf (stderr, "\n");
104: exit (1);
105: }
106:
107: mpfr_ui_sub (y, 1, x, GMP_RNDU);
108: mpfr_set_str_raw (res, "1.0");
109: if (mpfr_cmp (y, res))
110: {
111: fprintf (stderr, "Error in mpfr_ui_sub (y, 1, x, GMP_RNDU) for x=2^(-12)\nexpected 1.0, got ");
112: mpfr_out_str (stderr, 2, 0, y, GMP_RNDN);
113: fprintf (stderr, "\n");
114: exit (1);
115: }
116:
117: mpfr_ui_sub (y, 1, x, GMP_RNDN);
118: mpfr_set_str_raw (res, "1.0");
119: if (mpfr_cmp (y, res))
120: {
121: fprintf (stderr, "Error in mpfr_ui_sub (y, 1, x, GMP_RNDN) for x=2^(-12)\nexpected 1.0, got ");
122: mpfr_out_str (stderr, 2, 0, y, GMP_RNDN);
123: fprintf (stderr, "\n");
124: exit (1);
125: }
126:
127: mpfr_clear (x);
128: mpfr_clear (y);
129: mpfr_clear (res);
130: }
131:
132: /* checks that y/x gives the same results in double
133: and with mpfr with 53 bits of precision */
134: void
135: check (unsigned long y, double x, mp_rnd_t rnd_mode, double z1)
136: {
137: double z2; mpfr_t xx, zz;
138:
139: mpfr_init2(xx, 53);
140: mpfr_init2(zz, 53);
141: mpfr_set_d(xx, x, rnd_mode);
142: mpfr_ui_sub(zz, y, xx, rnd_mode);
143: #ifdef MPFR_HAVE_FESETROUND
144: mpfr_set_machine_rnd_mode(rnd_mode);
145: #endif
146: if (z1==0.0) z1 = y-x;
147: z2 = mpfr_get_d1 (zz);
148: if (z1!=z2 && !(isnan(z1) && isnan(z2))) {
149: printf("expected difference is %1.20e, got %1.20e\n",z1,z2);
150: printf("mpfr_ui_sub failed for y=%lu x=%1.20e with rnd_mode=%s\n",
151: y, x, mpfr_print_rnd_mode(rnd_mode));
152: exit(1);
153: }
154: mpfr_clear(xx); mpfr_clear(zz);
155: }
156:
157: /* if u = o(x-y), v = o(u-x), w = o(v+y), then x-y = u-w */
158: void
159: check_two_sum (mp_prec_t p)
160: {
161: unsigned int x;
162: mpfr_t y, u, v, w;
163: mp_rnd_t rnd;
164: int inexact;
165:
166: mpfr_init2 (y, p);
167: mpfr_init2 (u, p);
168: mpfr_init2 (v, p);
169: mpfr_init2 (w, p);
170: do
171: {
172: x = LONG_RAND ();
173: }
174: while (x < 1);
175: mpfr_random (y);
176: rnd = LONG_RAND() % 4;
177: rnd = GMP_RNDN;
178: inexact = mpfr_ui_sub (u, x, y, GMP_RNDN);
179: mpfr_sub_ui (v, u, x, GMP_RNDN);
180: mpfr_add (w, v, y, GMP_RNDN);
181: /* as u = (x-y) + w, we should have inexact and w of same sign */
182: if (((inexact == 0) && mpfr_cmp_ui (w, 0)) ||
183: ((inexact > 0) && (mpfr_cmp_ui (w, 0) <= 0)) ||
184: ((inexact < 0) && (mpfr_cmp_ui (w, 0) >= 0)))
185: {
186: fprintf (stderr, "Wrong inexact flag for prec=%u, rnd=%s\n", (unsigned)p,
187: mpfr_print_rnd_mode (rnd));
188: printf ("x=%u\n", x);
189: printf ("y="); mpfr_print_binary(y); putchar('\n');
190: printf ("u="); mpfr_print_binary(u); putchar('\n');
191: printf ("v="); mpfr_print_binary(v); putchar('\n');
192: printf ("w="); mpfr_print_binary(w); putchar('\n');
193: printf ("inexact = %d\n", inexact);
194: exit (1);
195: }
196: mpfr_clear (y);
197: mpfr_clear (u);
198: mpfr_clear (v);
199: mpfr_clear (w);
200: }
201:
202: int
203: main (int argc, char *argv[])
204: {
205: mp_prec_t p;
206: unsigned k;
207: #ifdef MPFR_HAVE_FESETROUND
208: double x;
209: unsigned long y, N;
210: int i, rnd_mode, rnd;
211:
212: mpfr_test_init ();
213:
214: SEED_RAND (time(NULL));
215: N = (argc<2) ? 1000000 : atoi(argv[1]);
216: rnd_mode = (argc<3) ? -1 : atoi(argv[2]);
217: for (i=0;i<1000000;i++) {
218: x = drand ();
219: y = LONG_RAND ();
220: if (ABS(x)>2.2e-307) {
221: /* avoid denormalized numbers and overflows */
222: rnd = (rnd_mode==-1) ? LONG_RAND()%4 : rnd_mode;
223: check(y, x, rnd, 0.0);
224: }
225: }
226: #endif
227: special ();
228: for (p=2; p<100; p++)
229: for (k=0; k<100; k++)
230: check_two_sum (p);
231: #ifdef HAVE_INFS
232: check (1, DBL_POS_INF, GMP_RNDN, DBL_NEG_INF);
233: check (1, DBL_NEG_INF, GMP_RNDN, DBL_POS_INF);
234: check (1, DBL_NAN, GMP_RNDN, DBL_NAN);
235: #endif
236: check(1196426492, 1.4218093058435347e-3, GMP_RNDN, 1.1964264919985781e9);
237: check(1092583421, -1.0880649218158844e9, GMP_RNDN, 2.1806483428158845901e9);
238: check(948002822, 1.22191250737771397120e+20, GMP_RNDN,
239: -1.2219125073682338611e20);
240: check(832100416, 4.68311314939691330000e-215, GMP_RNDD,
241: 8.3210041599999988079e8);
242: check(1976245324, 1.25296395864546893357e+232, GMP_RNDZ,
243: -1.2529639586454686577e232);
244: check(2128997392, -1.08496826129284207724e+187, GMP_RNDU,
245: 1.0849682612928422704e187);
246: check(293607738, -1.9967571564050541e-5, GMP_RNDU, 2.9360773800002003e8);
247: check(354270183, 2.9469161763489528e3, GMP_RNDN, 3.5426723608382362e8);
248:
249: return 0;
250: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>