I've been looking for a way to do this for several years [albeit not very hard]. Anyways, I saw a great example at mpfr.org. The libraries were already installed in my FreeBSD version [others may need to install them]. This example calculates Euler's number to an arbitrary degree of precision.
[The order of the header files is important, mpfr.h uses functions from gmp.h and stdio.h]
[The order of the header files is important, mpfr.h uses functions from gmp.h and stdio.h]
C:
//This example of multiple precision variables was taken from: https://www.mpfr.org/sample.html
//Make sure the gmp and mpfr libraries are installed
///header files should be in: "/usr/local/include/mpfr.h" and "/usr/local/include/gmp.h"
//To compile:
//gcc -o sample sample.c -lmpfr
#include <stdio.h>
#include <gmp.h>
#include <mpfr.h>
int main (void)
{
unsigned int i;
mpfr_t s, t, u; //declares three floating-point variables s, t, u
mpfr_init2 (t, 200); //initializes the variable t with a 200-bit precision
mpfr_set_d (t, 1.0, MPFR_RNDD); //sets the value of t to the double-precision number 1.0 rounded toward minus infinity (here no rounding is involved since 1 is represented exactly as a double-precision number and also as a 200-bit MPFR number)
mpfr_init2 (s, 200);
mpfr_set_d (s, 1.0, MPFR_RNDD);
mpfr_init2 (u, 200);
for (i = 1; i <= 100; i++)
{
mpfr_mul_ui (t, t, i, MPFR_RNDU); //multiplies t in place by the unsigned integer i, where the result is rounded toward plus infinity
mpfr_set_d (u, 1.0, MPFR_RNDD);
mpfr_div (u, u, t, MPFR_RNDD); //divides u by t, rounding the result toward minus infinity, and stores it in u
mpfr_add (s, s, u, MPFR_RNDD);
}
printf ("Sum is ");
mpfr_out_str (stdout, 10, 0, s, MPFR_RNDD); //prints the value of s in base 10, rounded toward minus infinity, where the third argument 0 means that the number of printed digits is automatically chosen from the precision of s (note: mpfr_printf could also be used instead of printf, mpfr_out_str and putchar)
putchar ('\n');
mpfr_clear (s); // The mpfr_clear and mpfr_free_cache calls free the space used by the MPFR variables and caches
mpfr_clear (t);
mpfr_clear (u);
mpfr_free_cache ();
return 0;
}