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
|
/* Test file for mpfr_nextabove, mpfr_nextbelow, mpfr_nexttoward.
Copyright 2003-2016 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.
This file is part of the GNU MPFR Library.
The GNU 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 3 of the License, or (at your
option) any later version.
The GNU 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 GNU MPFR Library; see the file COPYING.LESSER. If not, see
http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
#include "mpfr-test.h"
/* Generic tests for mpfr_nextabove and mpfr_nextbelow */
static void
generic_abovebelow (void)
{
int i;
for (i = 0; i < 20000; i++)
{
mpfr_t x, y, z, t;
mpfr_prec_t prec;
int neg, below;
prec = (randlimb () % 300) + MPFR_PREC_MIN;
mpfr_inits2 (prec, x, y, z, (mpfr_ptr) 0);
mpfr_init2 (t, 3);
/* special tests (executed once is enough) */
if (i == 0)
{
mpfr_set_nan (x);
mpfr_nextabove (x);
MPFR_ASSERTN(mpfr_nan_p (x));
mpfr_nextbelow (x);
MPFR_ASSERTN(mpfr_nan_p (x));
mpfr_nexttoward (x, y);
MPFR_ASSERTN(mpfr_nan_p (x));
mpfr_set_ui (y, 1, MPFR_RNDN);
mpfr_nexttoward (y, x);
MPFR_ASSERTN(mpfr_nan_p (y));
mpfr_set_ui (x, 1, MPFR_RNDN);
mpfr_set_ui (y, 1, MPFR_RNDN);
mpfr_nexttoward (x, y);
MPFR_ASSERTN(mpfr_cmp_ui (x, 1) == 0);
}
do
mpfr_urandomb (x, RANDS);
while (mpfr_cmp_ui (x, 0) == 0);
neg = randlimb () & 1;
if (neg)
mpfr_neg (x, x, MPFR_RNDN);
mpfr_set (y, x, MPFR_RNDN);
below = randlimb () & 1;
if (below)
mpfr_nextbelow (y);
else
mpfr_nextabove (y);
mpfr_set_si (t, below ? -5 : 5, MPFR_RNDN);
mpfr_mul_2si (t, t, (mpfr_get_exp) (x) - prec - 3, MPFR_RNDN);
/* t = (1/2 + 1/8) ulp(x) */
mpfr_add (z, x, t, MPFR_RNDN);
if (!mpfr_number_p (y) || mpfr_cmp (y, z) != 0)
{
printf ("Error in mpfr_next%s for\n",
below ? "below" : "above");
mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
printf (", got\n");
mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
printf (" instead of\n");
mpfr_out_str (stdout, 2, 0, z, MPFR_RNDN);
printf ("\n");
exit (1);
}
mpfr_clears (x, y, z, t, (mpfr_ptr) 0);
}
}
static void
inverse_test (void)
{
static const char *tests[] = { "0", "1", "2", "3.1", "Inf" };
int i, neg, below;
mpfr_prec_t prec;
for (i = 0; i < numberof (tests); i++)
for (neg = 0; neg <= 1; neg++)
for (below = 0; below <= 1; below++)
for (prec = MPFR_PREC_MIN; prec < 200; prec += 3)
{
mpfr_t x, y;
int sign;
mpfr_inits2 (prec, x, y, (mpfr_ptr) 0);
mpfr_set_str (x, tests[i], 10, MPFR_RNDN);
if (neg)
mpfr_neg (x, x, MPFR_RNDN);
mpfr_set (y, x, MPFR_RNDN);
if (below)
mpfr_nextbelow (y);
else
mpfr_nextabove (y);
sign = MPFR_SIGN (y);
if (!(neg == below && mpfr_inf_p (x))) /* then x = y */
{
if (mpfr_cmp (x, y) == 0)
{
printf ("Error in inverse_test for %s, neg = %d,"
" below = %d, prec = %d: x = y", tests[i],
neg, below, (int) prec);
printf ("\nx = ");
mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
printf ("\ny = ");
mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
printf ("\n");
exit (1);
}
mpfr_nexttoward (y, x);
if (mpfr_cmp_ui (y, 0) == 0 && MPFR_SIGN (y) != sign)
{
printf ("Sign error in inverse_test for %s, neg = %d,"
" below = %d, prec = %d\n", tests[i], neg,
below, (int) prec);
mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
printf ("\n");
exit (1);
}
}
if (mpfr_cmp (x, y) != 0)
{
printf ("Error in inverse_test for %s, neg = %d, below = %d,"
" prec = %d", tests[i], neg, below, (int) prec);
printf ("\nx = ");
mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
printf ("\ny = ");
mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
printf ("\n");
exit (1);
}
mpfr_clears (x, y, (mpfr_ptr) 0);
}
}
int
main (void)
{
tests_start_mpfr ();
generic_abovebelow ();
inverse_test ();
tests_end_mpfr ();
return 0;
}
|