1 | /* Example illustrating how to use mpfr_can_round. */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | Copyright 2016-2020 Free Software Foundation, Inc.
|
---|
5 | Contributed by the AriC and Caramba projects, INRIA.
|
---|
6 |
|
---|
7 | This file is part of the GNU MPFR Library.
|
---|
8 |
|
---|
9 | The GNU MPFR Library is free software; you can redistribute it and/or modify
|
---|
10 | it under the terms of the GNU Lesser General Public License as published by
|
---|
11 | the Free Software Foundation; either version 3 of the License, or (at your
|
---|
12 | option) any later version.
|
---|
13 |
|
---|
14 | The GNU MPFR Library is distributed in the hope that it will be useful, but
|
---|
15 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
---|
16 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
---|
17 | License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License
|
---|
20 | along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
|
---|
21 | https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
|
---|
22 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include <stdio.h>
|
---|
26 | #include <mpfr.h>
|
---|
27 |
|
---|
28 | int
|
---|
29 | main (void)
|
---|
30 | {
|
---|
31 | mpfr_t x, y;
|
---|
32 | mpfr_prec_t px = 53, py = 50;
|
---|
33 | mpfr_rnd_t r1, r2;
|
---|
34 | int ok;
|
---|
35 |
|
---|
36 | /* Given an approximation of Pi to px bits computed with rounding mode r1,
|
---|
37 | we call mpfr_can_round() to see if we can deduced the correct rounding
|
---|
38 | of Pi to py bits with rounding mode r2.
|
---|
39 | The error is at most 1 = 2^0 ulp. This translates into err = prec(x). */
|
---|
40 | mpfr_init2 (x, px);
|
---|
41 | mpfr_init2 (y, py);
|
---|
42 | for (r1 = 0; r1 < 4; r1++)
|
---|
43 | {
|
---|
44 | mpfr_const_pi (x, r1);
|
---|
45 | printf ("r1=%s approx=", mpfr_print_rnd_mode (r1));
|
---|
46 | mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN);
|
---|
47 | printf ("\n");
|
---|
48 | for (r2 = 0; r2 < 4; r2++)
|
---|
49 | {
|
---|
50 | ok = mpfr_can_round (x, mpfr_get_prec (x), r1, r2, py);
|
---|
51 | printf ("r2=%s ok=%d", mpfr_print_rnd_mode (r2), ok);
|
---|
52 | if (ok)
|
---|
53 | {
|
---|
54 | mpfr_set (y, x, r2);
|
---|
55 | printf (" ");
|
---|
56 | mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
|
---|
57 | }
|
---|
58 | printf ("\n");
|
---|
59 | }
|
---|
60 | }
|
---|
61 | mpfr_clear (x);
|
---|
62 | mpfr_clear (y);
|
---|
63 | mpfr_free_cache ();
|
---|
64 | return 0;
|
---|
65 | }
|
---|