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