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
|
/* Test file for mpfr_ui_div.
Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
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. 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 <time.h>
#include "gmp.h"
#include "gmp-impl.h"
#include "mpfr.h"
#include "mpfr-impl.h"
#include "mpfr-test.h"
void check _PROTO((unsigned long, double, mp_rnd_t, double));
void check_inexact _PROTO((void));
void check_nan _PROTO((void));
/* 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_div(zz, y, xx, rnd_mode);
#ifdef HAVE_FENV_H
mpfr_set_machine_rnd_mode(rnd_mode);
#endif
if (z1==0.0) z1 = y/x;
z2 = mpfr_get_d(zz);
if (z1!=z2 && !(isnan(z1) && isnan(z2))) {
printf("expected quotient is %1.20e, got %1.20e\n",z1,z2);
printf("mpfr_ui_div 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);
}
void
check_inexact (void)
{
mpfr_t x, y, z;
mp_prec_t px, py;
int inexact, cmp;
unsigned long int u;
mp_rnd_t rnd;
mpfr_init (x);
mpfr_init (y);
mpfr_init (z);
for (px=2; px<300; px++)
{
mpfr_set_prec (x, px);
mpfr_random (x);
u = LONG_RAND ();
for (py=2; py<300; py++)
{
mpfr_set_prec (y, py);
mpfr_set_prec (z, py + px);
for (rnd=0; rnd<4; rnd++)
{
inexact = mpfr_ui_div (y, u, x, rnd);
if (mpfr_mul (z, y, x, rnd))
{
fprintf (stderr, "z <- y * x should be exact\n");
exit (1);
}
cmp = mpfr_cmp_ui (z, u);
if (((inexact == 0) && (cmp != 0)) ||
((inexact > 0) && (cmp <= 0)) ||
((inexact < 0) && (cmp >= 0)))
{
fprintf (stderr, "Wrong inexact flag for u=%lu, rnd=%s\n", u,
mpfr_print_rnd_mode(rnd));
printf ("expected %d, got %d\n", cmp, inexact);
printf ("x="); mpfr_print_binary (x); putchar ('\n');
printf ("y="); mpfr_print_binary (y); putchar ('\n');
printf ("y*x="); mpfr_print_binary (z); putchar ('\n');
exit (1);
}
}
}
}
mpfr_clear (x);
mpfr_clear (y);
mpfr_clear (z);
}
void
check_nan (void)
{
mpfr_t d, q;
mpfr_init2 (d, 100L);
mpfr_init2 (q, 100L);
/* 1/+inf == 0 */
MPFR_CLEAR_FLAGS (d);
MPFR_SET_INF (d);
MPFR_SET_POS (d);
ASSERT_ALWAYS (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */
ASSERT_ALWAYS (mpfr_number_p (q));
ASSERT_ALWAYS (mpfr_sgn (q) == 0);
/* 1/-inf == -0 */
MPFR_CLEAR_FLAGS (d);
MPFR_SET_INF (d);
MPFR_SET_NEG (d);
ASSERT_ALWAYS (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */
ASSERT_ALWAYS (mpfr_number_p (q));
ASSERT_ALWAYS (mpfr_sgn (q) == 0);
/* 1/nan == nan */
MPFR_SET_NAN (d);
ASSERT_ALWAYS (mpfr_ui_div (q, 1L, d, GMP_RNDZ) == 0); /* exact */
ASSERT_ALWAYS (mpfr_nan_p (q));
/* 0/0 == nan */
mpfr_set_ui (d, 0L, GMP_RNDN);
ASSERT_ALWAYS (mpfr_ui_div (q, 0L, d, GMP_RNDZ) == 0); /* exact */
ASSERT_ALWAYS (mpfr_nan_p (q));
mpfr_clear (d);
mpfr_clear (q);
}
int
main (int argc, char *argv[])
{
#ifdef HAVE_FENV_H
double x;
unsigned long y, N;
int i, rnd_mode, rnd;
mpfr_test_init ();
SEED_RAND(time(NULL));
N = (argc<2) ? 1000000 : atoi(argv[1]);
rnd_mode = (argc<3) ? -1 : atoi(argv[2]);
for (i=0;i<1000000;i++)
{
x = drand();
y = LONG_RAND();
if (ABS(x)>4e-286)
{
/* avoid denormalized numbers and overflows */
rnd = (rnd_mode==-1) ? LONG_RAND()%4 : rnd_mode;
check(y, x, rnd, 0.0);
}
}
#endif
check_inexact ();
check(948002822, 1.22191250737771397120e+20, GMP_RNDN,
7.758352715731357946e-12);
check(1976245324, 1.25296395864546893357e+232, GMP_RNDZ,
1.5772563211925444801e-223);
check(740454110, 2.11496253355831863313e+183, GMP_RNDZ,
3.5010270784996976041e-175);
check(1690540942, 1.28278599852446657468e-276, GMP_RNDU,
1.3178666932321966062e285);
check(1476599377, -2.14191393656148625995e+305, GMP_RNDD,
-6.8938315017943889615e-297);
return 0;
}
|