1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/* Test file for mpfr_ui_sub.
Copyright 2000, 2001, 2002 Free Software Foundation.
This file is part of the MPFR Library.
The MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
The MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the MPFR Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <time.h>
#include "gmp.h"
#include "mpfr.h"
#include "mpfr-impl.h"
#include "mpfr-test.h"
void special _PROTO ((void));
void check _PROTO ((unsigned long, double, mp_rnd_t, double));
void check_two_sum _PROTO ((mp_prec_t));
void
special (void)
{
mpfr_t x, y, res;
int inexact;
mpfr_init (x);
mpfr_init (y);
mpfr_init (res);
mpfr_set_prec (x, 24);
mpfr_set_prec (y, 24);
mpfr_set_str_raw (y, "0.111100110001011010111");
inexact = mpfr_ui_sub (x, 1, y, GMP_RNDN);
if (inexact)
{
fprintf (stderr, "Wrong inexact flag: got %d, expected 0\n", inexact);
exit (1);
}
mpfr_set_prec (x, 24);
mpfr_set_prec (y, 24);
mpfr_set_str_raw (y, "0.111100110001011010111");
if ((inexact = mpfr_ui_sub (x, 38181761, y, GMP_RNDN)) >= 0)
{
fprintf (stderr, "Wrong inexact flag: got %d, expected -1\n", inexact);
exit (1);
}
mpfr_set_prec (x, 63);
mpfr_set_prec (y, 63);
mpfr_set_str_raw (y, "0.111110010010100100110101101010001001100101110001000101110111111E-1");
if ((inexact = mpfr_ui_sub (x, 1541116494, y, GMP_RNDN)) <= 0)
{
fprintf (stderr, "Wrong inexact flag: got %d, expected +1\n", inexact);
exit (1);
}
mpfr_set_prec (x, 32);
mpfr_set_prec (y, 32);
mpfr_set_str_raw (y, "0.11011000110111010001011100011100E-1");
if ((inexact = mpfr_ui_sub (x, 2000375416, y, GMP_RNDN)) >= 0)
{
fprintf (stderr, "Wrong inexact flag: got %d, expected -1\n", inexact);
exit (1);
}
mpfr_set_prec (x, 24);
mpfr_set_prec (y, 24);
mpfr_set_str_raw (y, "0.110011011001010011110111E-2");
if ((inexact = mpfr_ui_sub (x, 927694848, y, GMP_RNDN)) <= 0)
{
fprintf (stderr, "Wrong inexact flag: got %d, expected +1\n", inexact);
exit (1);
}
/* bug found by Mathieu Dutour, 12 Apr 2001 */
mpfr_set_prec (x, 5);
mpfr_set_prec (y, 5);
mpfr_set_prec (res, 5);
mpfr_set_str_raw (x, "1e-12");
mpfr_ui_sub (y, 1, x, GMP_RNDD);
mpfr_set_str_raw (res, "0.11111");
if (mpfr_cmp (y, res))
{
fprintf (stderr, "Error in mpfr_ui_sub (y, 1, x, GMP_RNDD) for x=2^(-12)\nexpected 1.1111e-1, got ");
mpfr_out_str (stderr, 2, 0, y, GMP_RNDN);
fprintf (stderr, "\n");
exit (1);
}
mpfr_ui_sub (y, 1, x, GMP_RNDU);
mpfr_set_str_raw (res, "1.0");
if (mpfr_cmp (y, res))
{
fprintf (stderr, "Error in mpfr_ui_sub (y, 1, x, GMP_RNDU) for x=2^(-12)\nexpected 1.0, got ");
mpfr_out_str (stderr, 2, 0, y, GMP_RNDN);
fprintf (stderr, "\n");
exit (1);
}
mpfr_ui_sub (y, 1, x, GMP_RNDN);
mpfr_set_str_raw (res, "1.0");
if (mpfr_cmp (y, res))
{
fprintf (stderr, "Error in mpfr_ui_sub (y, 1, x, GMP_RNDN) for x=2^(-12)\nexpected 1.0, got ");
mpfr_out_str (stderr, 2, 0, y, GMP_RNDN);
fprintf (stderr, "\n");
exit (1);
}
mpfr_clear (x);
mpfr_clear (y);
mpfr_clear (res);
}
/* checks that y/x gives the same results in double
and with mpfr with 53 bits of precision */
void
check (unsigned long y, double x, mp_rnd_t rnd_mode, double z1)
{
double z2; mpfr_t xx, zz;
mpfr_init2(xx, 53);
mpfr_init2(zz, 53);
mpfr_set_d(xx, x, rnd_mode);
mpfr_ui_sub(zz, y, xx, rnd_mode);
z2 = mpfr_get_d1 (zz);
if (z1!=z2 && !(isnan(z1) && isnan(z2))) {
printf("expected difference is %1.20e, got %1.20e\n",z1,z2);
printf("mpfr_ui_sub failed for y=%lu x=%1.20e with rnd_mode=%s\n",
y, x, mpfr_print_rnd_mode(rnd_mode));
exit(1);
}
mpfr_clear(xx); mpfr_clear(zz);
}
/* if u = o(x-y), v = o(u-x), w = o(v+y), then x-y = u-w */
void
check_two_sum (mp_prec_t p)
{
unsigned int x;
mpfr_t y, u, v, w;
mp_rnd_t rnd;
int inexact;
mpfr_init2 (y, p);
mpfr_init2 (u, p);
mpfr_init2 (v, p);
mpfr_init2 (w, p);
do
{
x = LONG_RAND ();
}
while (x < 1);
mpfr_random (y);
rnd = LONG_RAND() % 4;
rnd = GMP_RNDN;
inexact = mpfr_ui_sub (u, x, y, GMP_RNDN);
mpfr_sub_ui (v, u, x, GMP_RNDN);
mpfr_add (w, v, y, GMP_RNDN);
/* as u = (x-y) + w, we should have inexact and w of same sign */
if (((inexact == 0) && mpfr_cmp_ui (w, 0)) ||
((inexact > 0) && (mpfr_cmp_ui (w, 0) <= 0)) ||
((inexact < 0) && (mpfr_cmp_ui (w, 0) >= 0)))
{
fprintf (stderr, "Wrong inexact flag for prec=%u, rnd=%s\n", (unsigned)p,
mpfr_print_rnd_mode (rnd));
printf ("x=%u\n", x);
printf ("y="); mpfr_print_binary(y); putchar('\n');
printf ("u="); mpfr_print_binary(u); putchar('\n');
printf ("v="); mpfr_print_binary(v); putchar('\n');
printf ("w="); mpfr_print_binary(w); putchar('\n');
printf ("inexact = %d\n", inexact);
exit (1);
}
mpfr_clear (y);
mpfr_clear (u);
mpfr_clear (v);
mpfr_clear (w);
}
int
main (int argc, char *argv[])
{
mp_prec_t p;
unsigned k;
tests_start_mpfr ();
special ();
for (p=2; p<100; p++)
for (k=0; k<100; k++)
check_two_sum (p);
#ifdef HAVE_INFS
check (1, DBL_POS_INF, GMP_RNDN, DBL_NEG_INF);
check (1, DBL_NEG_INF, GMP_RNDN, DBL_POS_INF);
check (1, DBL_NAN, GMP_RNDN, DBL_NAN);
#endif
check(1196426492, 1.4218093058435347e-3, GMP_RNDN, 1.1964264919985781e9);
check(1092583421, -1.0880649218158844e9, GMP_RNDN, 2.1806483428158845901e9);
check(948002822, 1.22191250737771397120e+20, GMP_RNDN,
-1.2219125073682338611e20);
check(832100416, 4.68311314939691330000e-215, GMP_RNDD,
8.3210041599999988079e8);
check(1976245324, 1.25296395864546893357e+232, GMP_RNDZ,
-1.2529639586454686577e232);
check(2128997392, -1.08496826129284207724e+187, GMP_RNDU,
1.0849682612928422704e187);
check(293607738, -1.9967571564050541e-5, GMP_RNDU, 2.9360773800002003e8);
check(354270183, 2.9469161763489528e3, GMP_RNDN, 3.5426723608382362e8);
tests_end_mpfr ();
return 0;
}
|