aboutsummaryrefslogtreecommitdiff
path: root/challenge-006/paulo-custodio/cpp/ch-2.cpp
blob: b408b41945e3e71b64c6b7f1ce26d237cbadbee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
Challenge 006

Challenge #2
Create a script to calculate Ramanujan's constant with at least 32 digits of
precision. Find out more about it here.

The standard IEEE 754 double-precision binary floating-point format: binary64
gives only 15 to 17 significant decimal digits
*/

#include <iostream>
#include <gmp.h>
#include <mpfr.h>

int main() {
    mpfr_set_default_prec(256);  // 107 bits needed to represent 32 decimal digits

    mpfr_t pi, e, k;
    mpfr_inits(pi, e, k, NULL);

    mpfr_const_pi(pi, MPFR_RNDN);

    mpfr_set_str(e, "163", 10, MPFR_RNDN);  // e = 163
    mpfr_sqrt(e, e, MPFR_RNDN);             // e = sqr(163)
    mpfr_mul(e, pi, e, MPFR_RNDN);          // e = pi*sqr(163)

    mpfr_exp(k, e, MPFR_RNDN);              // k = e^(pi*sqr(163))

    mpfr_printf("%32.12Rf\n", k);           // 18.12 = 32 precision

    mpfr_clears(pi, e, k, NULL);
}