[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GMP precision problem - only getting 15 places of PI

From: nmenuj
Subject: Re: GMP precision problem - only getting 15 places of PI
Date: 19 Sep 2005 07:59:42 -0700
User-agent: G2/0.2

The output is correct after incorporating the change David pointed out.
Mensanator, please see below for the changed program using precomputed
mpf's for the float literals in the loop. There is some speedup I
think. I thought the type promotion for a const would occur just once
in a C++ program, had no idea it runs inside the loop code.

I appreciate your comments.

Before (changing constants to mpfs):
real    0m1.236s
user    0m1.191s
sys     0m0.001s

real    0m1.225s
user    0m1.177s
sys     0m0.002s

I know the loop bounds are way more than required for just 100 digits,
but I suppose they are good for performance checking.

#include <iostream>
#include <gmpxx.h>

using namespace std;
const int PREC=20000;

mpf_class pival(0.0, PREC);
mpf_class d2(1.0, PREC), d3(1.0,PREC);

const mpf_class const_1(1.0, PREC);
const mpf_class const_2(2.0, PREC);
const mpf_class const_3(3.0, PREC);
const mpf_class const_4(4.0, PREC);
const mpf_class const_9(9.0, PREC);
const mpf_class const_16(16.0, PREC);
const mpf_class const_81(81.0, PREC);

int main()
        int i = 0;
        d2 = const_1/const_2;
        pival = d2 - (d2/const_4)/const_3;
        for (i=5;i<20000;i+=4)
                d2 /= const_16;
                pival = pival + d2/i;
                pival = pival - (d2/const_4)/(i + 2);
        cout << "def prec " << mpf_get_default_prec() << '\n';
        d3 = mpf_class(1.0, PREC)/const_3;
        pival = pival +  d3 - (d3/const_9)/const_3;
        for (i=5;i<20000;i+=4)
                d3 /= const_81;
                pival = pival + d3/i;
                pival = pival - (d3/const_9)/(i + 2);
        cout << pival * 4.0 << '\n';
        return 0;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]