1efee5258Smrg /* This example was presented at the CNC'2 summer school on MPFR and MPC at
2efee5258Smrg * LORIA, Nancy, France. It shows how one can use different rounding modes.
3efee5258Smrg * This example implements the OddRoundedAdd algorithm, which returns the
4efee5258Smrg * sum z = x + y rounded-to-odd:
5efee5258Smrg * * RO(z) = z if z is exactly representable;
6efee5258Smrg * * otherwise RO(z) is the value among RD(z) and RU(z) whose
7efee5258Smrg * least significant bit is a one.
8efee5258Smrg */
9efee5258Smrg
10efee5258Smrg /*
11*ba125506Smrg Copyright 2009-2023 Free Software Foundation, Inc.
12efdec83bSmrg Contributed by the AriC and Caramba projects, INRIA.
13efee5258Smrg
14efee5258Smrg This file is part of the GNU MPFR Library.
15efee5258Smrg
16efee5258Smrg The GNU MPFR Library is free software; you can redistribute it and/or modify
17efee5258Smrg it under the terms of the GNU Lesser General Public License as published by
18efee5258Smrg the Free Software Foundation; either version 3 of the License, or (at your
19efee5258Smrg option) any later version.
20efee5258Smrg
21efee5258Smrg The GNU MPFR Library is distributed in the hope that it will be useful, but
22efee5258Smrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23efee5258Smrg or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24efee5258Smrg License for more details.
25efee5258Smrg
26efee5258Smrg You should have received a copy of the GNU Lesser General Public License
27efee5258Smrg along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
282ba2404bSmrg https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
29efee5258Smrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
30efee5258Smrg */
31efee5258Smrg
32efee5258Smrg #include <stdio.h>
33efee5258Smrg #include <stdlib.h>
34efee5258Smrg #include <gmp.h>
35efee5258Smrg #include <mpfr.h>
36efee5258Smrg
37efee5258Smrg #define LIST x, y, d, u, e, z
38efee5258Smrg
main(int argc,char ** argv)39efee5258Smrg int main (int argc, char **argv)
40efee5258Smrg {
41efee5258Smrg mpfr_t LIST;
42efee5258Smrg mpfr_prec_t prec;
43efee5258Smrg int pprec; /* will be prec - 1 for mpfr_printf */
44efee5258Smrg
45efee5258Smrg if (argc != 4)
46efee5258Smrg {
47efee5258Smrg fprintf (stderr, "Usage: rndo-add <prec> <x> <y>\n");
48efee5258Smrg exit (1);
49efee5258Smrg }
50efee5258Smrg
51efee5258Smrg prec = atoi (argv[1]);
52efee5258Smrg if (prec < 2)
53efee5258Smrg {
54efee5258Smrg fprintf (stderr, "rndo-add: bad precision\n");
55efee5258Smrg exit (1);
56efee5258Smrg }
57efee5258Smrg pprec = prec - 1;
58efee5258Smrg
59efee5258Smrg mpfr_inits2 (prec, LIST, (mpfr_ptr) 0);
60efee5258Smrg
61d59437c0Smrg if (mpfr_set_str (x, argv[2], 0, MPFR_RNDN))
62efee5258Smrg {
63efee5258Smrg fprintf (stderr, "rndo-add: bad x value\n");
64efee5258Smrg exit (1);
65efee5258Smrg }
66efee5258Smrg mpfr_printf ("x = %.*Rb\n", pprec, x);
67efee5258Smrg
68d59437c0Smrg if (mpfr_set_str (y, argv[3], 0, MPFR_RNDN))
69efee5258Smrg {
70efee5258Smrg fprintf (stderr, "rndo-add: bad y value\n");
71efee5258Smrg exit (1);
72efee5258Smrg }
73efee5258Smrg mpfr_printf ("y = %.*Rb\n", pprec, y);
74efee5258Smrg
75d59437c0Smrg mpfr_add (d, x, y, MPFR_RNDD);
76efee5258Smrg mpfr_printf ("d = %.*Rb\n", pprec, d);
77efee5258Smrg
78d59437c0Smrg mpfr_add (u, x, y, MPFR_RNDU);
79efee5258Smrg mpfr_printf ("u = %.*Rb\n", pprec, u);
80efee5258Smrg
81d59437c0Smrg mpfr_add (e, d, u, MPFR_RNDN);
82d59437c0Smrg mpfr_div_2ui (e, e, 1, MPFR_RNDN);
83efee5258Smrg mpfr_printf ("e = %.*Rb\n", pprec, e);
84efee5258Smrg
85d59437c0Smrg mpfr_sub (z, u, e, MPFR_RNDN);
86d59437c0Smrg mpfr_add (z, z, d, MPFR_RNDN);
87efee5258Smrg mpfr_printf ("z = %.*Rb\n", pprec, z);
88efee5258Smrg
89efee5258Smrg mpfr_clears (LIST, (mpfr_ptr) 0);
902ba2404bSmrg mpfr_free_cache ();
91efee5258Smrg return 0;
92efee5258Smrg }
93