sample.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* This is the example given and commented on the MPFR web site:
  2. * http://www.mpfr.org/sample.html
  3. */
  4. /*
  5. Copyright 1999-2004, 2006-2016 Free Software Foundation, Inc.
  6. Contributed by the AriC and Caramba projects, INRIA.
  7. This file is part of the GNU MPFR Library.
  8. The GNU MPFR Library is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU Lesser General Public License as published by
  10. the Free Software Foundation; either version 3 of the License, or (at your
  11. option) any later version.
  12. The GNU MPFR Library is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  15. License for more details.
  16. You should have received a copy of the GNU Lesser General Public License
  17. along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
  18. http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
  19. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #include <stdio.h>
  22. #include <gmp.h>
  23. #include <mpfr.h>
  24. int main (void)
  25. {
  26. unsigned int i;
  27. mpfr_t s, t, u;
  28. mpfr_init2 (t, 200);
  29. mpfr_set_d (t, 1.0, MPFR_RNDD);
  30. mpfr_init2 (s, 200);
  31. mpfr_set_d (s, 1.0, MPFR_RNDD);
  32. mpfr_init2 (u, 200);
  33. for (i = 1; i <= 100; i++)
  34. {
  35. mpfr_mul_ui (t, t, i, MPFR_RNDU);
  36. mpfr_set_d (u, 1.0, MPFR_RNDD);
  37. mpfr_div (u, u, t, MPFR_RNDD);
  38. mpfr_add (s, s, u, MPFR_RNDD);
  39. }
  40. printf ("Sum is ");
  41. mpfr_out_str (stdout, 10, 0, s, MPFR_RNDD);
  42. putchar ('\n');
  43. mpfr_clear (s);
  44. mpfr_clear (t);
  45. mpfr_clear (u);
  46. return 0;
  47. }