source: Daodan/MSYS2/mingw32/share/doc/mpfr/examples/sample.c

Last change on this file was 1166, checked in by rossy, 3 years ago

Daodan: Replace MinGW build env with an up-to-date MSYS2 env

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