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
|
/* Test file for mpfr_set_ld and mpfr_get_ld.
Copyright 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.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <time.h>
#include "gmp.h"
#include "mpfr.h"
#include "mpfr-test.h"
void check_set_get _PROTO((long double, mpfr_t));
/* checks that a long double converted to a mpfr (with precision >=113),
then converted back to a long double gives the initial value,
or in other words mpfr_get_ld(mpfr_set_ld(d)) = d.
*/
void
check_set_get (long double d, mpfr_t x)
{
mp_rnd_t r;
long double e;
for (r=0; r<4; r++)
{
if (mpfr_set_ld (x, d, r))
{
fprintf (stderr, "Error: mpfr_set_ld should be exact\n");
exit (1);
}
e = mpfr_get_ld (x, r);
if (e != d && !(isnan(e) && isnan(d)))
{
fprintf (stderr, "Error: mpfr_get_ld o mpfr_set_ld <> Id\n");
fprintf (stderr, "d=%1.30Le get_ld(set_ld(d))=%1.30Le\n", d, e);
exit (1);
}
}
}
int
main (int argc, char *argv[])
{
long double d, e;
double f;
mpfr_t x;
int i;
tests_start_mpfr ();
mpfr_test_init ();
mpfr_init2 (x, 113);
/* checks NaN, Inf and -Inf */
mpfr_set_nan (x);
d = mpfr_get_ld (x, GMP_RNDN);
check_set_get (d, x);
mpfr_set_inf (x, 1);
d = mpfr_get_ld (x, GMP_RNDN);
check_set_get (d, x);
mpfr_set_inf (x, -1);
d = mpfr_get_ld (x, GMP_RNDN);
check_set_get (d, x);
/* checks the largest power of two */
d = 1.0; while ((e = d + d) != d) d = e;
check_set_get (d, x);
check_set_get (-d, x);
/* checks largest long double */
d = 0.0; while ((e = 1.0 + d / 2.0) != (long double) 2.0) d = e;
/* now d is the machine number just before 2.0 */
while ((e = d + d) != d) d = e;
check_set_get (d, x);
check_set_get (-d, x);
/* checks the smallest power of two */
d = 1.0; while ((e = d / 2.0) != (long double) 0.0) d = e;
check_set_get (d, x);
check_set_get (-d, x);
/* checks 2^1024 */
f = 1.3407807929942597100e155; /* 2^512 */
d = (long double) f;
d = d * d; /* 2^1024 */
check_set_get (d, x);
/* checks that 2^i, 2^i+1 and 2^i-1 are correctly converted */
d = 1.0;
for (i=1; i<=113; i++)
{
d = 2.0 * d; /* d = 2^i */
check_set_get (d, x);
check_set_get (d + 1.0, x);
check_set_get (d - 1.0, x);
}
for (i=0; i<10000; i++)
{
mpfr_random (x);
d = mpfr_get_ld (x, GMP_RNDN);
check_set_get (d, x);
}
mpfr_clear (x);
tests_end_mpfr ();
return 0;
}
|