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
|
/* Test file for mpfr_trunc, mpfr_floor, mpfr_ceil, mpfr_round.
Copyright (C) 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 <stdio.h>
#include <stdlib.h>
#include "gmp.h"
#include "mpfr.h"
int
main (void)
{
mp_size_t s;
mpz_t z;
mp_prec_t p;
mpfr_t x, y, t;
mp_rnd_t r;
int inexact, sign_t;
mpfr_init (x);
mpfr_init (y);
mpz_init (z);
mpfr_init (t);
mpz_set_ui (z, 1);
for (s = 2; s < 100; s++)
{
/* z has exactly s bits */
mpz_mul_2exp (z, z, 1);
if (rand () % 2)
mpz_add_ui (z, z, 1);
mpfr_set_prec (x, s);
mpfr_set_prec (t, s);
if (mpfr_set_z (x, z, GMP_RNDN))
{
fprintf (stderr, "Error: mpfr_set_z should be exact (s = %u)\n",
(unsigned int) s);
exit (1);
}
for (p=2; p<100; p++)
{
mpfr_set_prec (y, p);
for (r=0; r<4; r++)
{
if (r == GMP_RNDN)
inexact = mpfr_round (y, x);
else if (r == GMP_RNDZ)
inexact = mpfr_trunc (y, x);
else if (r == GMP_RNDU)
inexact = mpfr_ceil (y, x);
else /* r = GMP_RNDD */
inexact = mpfr_floor (y, x);
if (mpfr_sub (t, y, x, GMP_RNDN))
{
fprintf (stderr, "Error: subtraction should be exact\n");
exit (1);
}
sign_t = mpfr_cmp_ui (t, 0);
if (((inexact == 0) && (sign_t != 0)) ||
((inexact < 0) && (sign_t >= 0)) ||
((inexact > 0) && (sign_t <= 0)))
{
fprintf (stderr, "Wrong inexact flag\n");
exit (1);
}
}
}
}
mpfr_clear (x);
mpfr_clear (y);
mpz_clear (z);
mpfr_clear (t);
return 0;
}
|