rndo-add.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. */
  9. /*
  10. Copyright 2009-2016 Free Software Foundation, Inc.
  11. Contributed by the AriC and Caramba projects, INRIA.
  12. This file is part of the GNU MPFR Library.
  13. The GNU MPFR Library is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU Lesser General Public License as published by
  15. the Free Software Foundation; either version 3 of the License, or (at your
  16. option) any later version.
  17. The GNU MPFR Library is distributed in the hope that it will be useful, but
  18. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  19. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
  23. http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
  24. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <gmp.h>
  29. #include <mpfr.h>
  30. #define LIST x, y, d, u, e, z
  31. int main (int argc, char **argv)
  32. {
  33. mpfr_t LIST;
  34. mpfr_prec_t prec;
  35. int pprec; /* will be prec - 1 for mpfr_printf */
  36. if (argc != 4)
  37. {
  38. fprintf (stderr, "Usage: rndo-add <prec> <x> <y>\n");
  39. exit (1);
  40. }
  41. prec = atoi (argv[1]);
  42. if (prec < 2)
  43. {
  44. fprintf (stderr, "rndo-add: bad precision\n");
  45. exit (1);
  46. }
  47. pprec = prec - 1;
  48. mpfr_inits2 (prec, LIST, (mpfr_ptr) 0);
  49. if (mpfr_set_str (x, argv[2], 0, MPFR_RNDN))
  50. {
  51. fprintf (stderr, "rndo-add: bad x value\n");
  52. exit (1);
  53. }
  54. mpfr_printf ("x = %.*Rb\n", pprec, x);
  55. if (mpfr_set_str (y, argv[3], 0, MPFR_RNDN))
  56. {
  57. fprintf (stderr, "rndo-add: bad y value\n");
  58. exit (1);
  59. }
  60. mpfr_printf ("y = %.*Rb\n", pprec, y);
  61. mpfr_add (d, x, y, MPFR_RNDD);
  62. mpfr_printf ("d = %.*Rb\n", pprec, d);
  63. mpfr_add (u, x, y, MPFR_RNDU);
  64. mpfr_printf ("u = %.*Rb\n", pprec, u);
  65. mpfr_add (e, d, u, MPFR_RNDN);
  66. mpfr_div_2ui (e, e, 1, MPFR_RNDN);
  67. mpfr_printf ("e = %.*Rb\n", pprec, e);
  68. mpfr_sub (z, u, e, MPFR_RNDN);
  69. mpfr_add (z, z, d, MPFR_RNDN);
  70. mpfr_printf ("z = %.*Rb\n", pprec, z);
  71. mpfr_clears (LIST, (mpfr_ptr) 0);
  72. return 0;
  73. }