MPFR

      Comments Off on MPFR

Description

MPFR is an open-source C library dedicated to arbitrary-precision floating-point arithmetic with correctly defined rounding.

It provides data types and functions for performing arithmetic operations, elementary mathematical functions, and numerical computations with precise control over precision and rounding modes.

MPFR relies on the GMP library for the management of integers and arbitrary-precision numbers, ensuring reliable results that comply with floating-point arithmetic standards.

Its goal is to provide a portable, robust, and accurate library suitable for scientific computing, numerical analysis, cryptography, and all applications requiring high numerical precision.

Set up the environment

  • gmp is required.
ml libs/mpfr
  • Available version(s) : 4.2.2 (built with GCC 15.2.0 and GMP 6.3.0)

Tutorials

Use case – computation of √2

  • Create a file named sqrt2.c
#include <stdio.h>
#include <mpfr.h>

int main()
{
    mpfr_t x, y;

    mpfr_init2(x, 200);
    mpfr_init2(y, 200);

    mpfr_set_ui(x, 2, MPFR_RNDN);

    mpfr_sqrt(y, x, MPFR_RNDN);

    mpfr_printf("sqrt(2)=%.60Rf\n", y);

    mpfr_clear(x);
    mpfr_clear(y);

    return 0;
}
  • Compile
ml libs/mpfr
gcc sqrt2.c -lmpfr -lgmp -o sqrt2
  • Run in an interactive job
srun ./sqrt2

Documentation