source: Daodan/MSYS2/mingw32/share/doc/mpfr/examples/can_round.c

Last change on this file was 1166, checked in by rossy, 3 years ago

Daodan: Replace MinGW build env with an up-to-date MSYS2 env

File size: 2.1 KB
Line 
1/* Example illustrating how to use mpfr_can_round. */
2
3/*
4Copyright 2016-2020 Free Software Foundation, Inc.
5Contributed by the AriC and Caramba projects, INRIA.
6
7This file is part of the GNU MPFR Library.
8
9The GNU MPFR Library is free software; you can redistribute it and/or modify
10it under the terms of the GNU Lesser General Public License as published by
11the Free Software Foundation; either version 3 of the License, or (at your
12option) any later version.
13
14The GNU MPFR Library is distributed in the hope that it will be useful, but
15WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17License for more details.
18
19You should have received a copy of the GNU Lesser General Public License
20along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
21https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2251 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
23*/
24
25#include <stdio.h>
26#include <mpfr.h>
27
28int
29main (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}
Note: See TracBrowser for help on using the repository browser.