RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / toolchains / hndtools-arm-linux-2.6.36-uclibc-4.5.3 / share / doc / mpfr / examples / rndo-add.c
blob50dc15d9842b303bfc18a125cfdebf4876a8a45f
1 /* This example was presented at the CNC'2 summer school on MPFR and MPC at
2 * LORIA, Nancy, France. It shows how one can use different rounding modes.
3 * This example implements the OddRoundedAdd algorithm, which returns the
4 * sum z = x + y rounded-to-odd:
5 * * RO(z) = z if z is exactly representable;
6 * * otherwise RO(z) is the value among RD(z) and RU(z) whose
7 * least significant bit is a one.
8 */
11 Copyright 2009, 2010, 2011 Free Software Foundation, Inc.
12 Contributed by the Arenaire and Cacao projects, INRIA.
14 This file is part of the GNU MPFR Library.
16 The GNU MPFR Library is free software; you can redistribute it and/or modify
17 it under the terms of the GNU Lesser General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or (at your
19 option) any later version.
21 The GNU MPFR Library is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
26 You should have received a copy of the GNU Lesser General Public License
27 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
28 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
29 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <gmp.h>
35 #include <mpfr.h>
37 #define LIST x, y, d, u, e, z
39 int main (int argc, char **argv)
41 mpfr_t LIST;
42 mpfr_prec_t prec;
43 int pprec; /* will be prec - 1 for mpfr_printf */
45 if (argc != 4)
47 fprintf (stderr, "Usage: rndo-add <prec> <x> <y>\n");
48 exit (1);
51 prec = atoi (argv[1]);
52 if (prec < 2)
54 fprintf (stderr, "rndo-add: bad precision\n");
55 exit (1);
57 pprec = prec - 1;
59 mpfr_inits2 (prec, LIST, (mpfr_ptr) 0);
61 if (mpfr_set_str (x, argv[2], 0, GMP_RNDN))
63 fprintf (stderr, "rndo-add: bad x value\n");
64 exit (1);
66 mpfr_printf ("x = %.*Rb\n", pprec, x);
68 if (mpfr_set_str (y, argv[3], 0, GMP_RNDN))
70 fprintf (stderr, "rndo-add: bad y value\n");
71 exit (1);
73 mpfr_printf ("y = %.*Rb\n", pprec, y);
75 mpfr_add (d, x, y, GMP_RNDD);
76 mpfr_printf ("d = %.*Rb\n", pprec, d);
78 mpfr_add (u, x, y, GMP_RNDU);
79 mpfr_printf ("u = %.*Rb\n", pprec, u);
81 mpfr_add (e, d, u, GMP_RNDN);
82 mpfr_div_2ui (e, e, 1, GMP_RNDN);
83 mpfr_printf ("e = %.*Rb\n", pprec, e);
85 mpfr_sub (z, u, e, GMP_RNDN);
86 mpfr_add (z, z, d, GMP_RNDN);
87 mpfr_printf ("z = %.*Rb\n", pprec, z);
89 mpfr_clears (LIST, (mpfr_ptr) 0);
90 return 0;