Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and exploring the core foundations for app libraries.

lately, gaining intest in pi from some insight from a friend, i wrote this function to fill and array with digits of pi. it seems, however to work only for the first 17 digits of pi, yet my mathematical reasoning seems sound.

since pi is equal to:

∞

π = ∑ 2(-1)^k x 3^(1/2-k)

k=0 -----------------------

2k + 1

what i decided to do is to evaulate each term. one i found that the one's column was no longer affected, that i could round off the deciamals, so for the first case, get 3, subtract that from the current obtained value of pi, then multiply this value by 10 and any other caluclated terms now by 10... one i find that the ones now is no longer affected, i do that same thing over again... extract the 1, multiply pi by 10, and any other terms now by 100.

However... this system only works for calculating the first 17 or so digits of pi. what am i doing wrong? here is the code to aid u helping me ;)

CODE------------------------------------------------------------------------------------

// Evaluates the numerator for the calc_pi function; SEE BELOW

inline double numerator_pow(int k, int i)

{

double value = 2 * pow(3.0, (0.5 - double(k))) * pow(10.0, i);

return (k % 2 == 1) ? -1 * value : value;

}

// Fills an array with the various digits of pi

// ∞

// π = ∑ 2(-1)^k x 3^(1/2-k)

// k=0 -----------------------

// 2k + 1

//

// NOTE: Floating point impression prevents this line:

// double value = numerator(++k) / (2*k + 1) * pow(10.0, i);

// from producing a value greater than zero after the first 15 digits

void pi_digits(char* digits, int length)

{

int k = 0; // Term counter for the sumnation

double pi = 2 * sqrt(3.0); // Calculate the first term in the expansion

const double delta = 0.0000001; // Calculate a limiter value of the set precision

const double neg_delta = -1 * delta; // Negative limiter. Half the term are negative so lowers

// the number of operations if its calculated before

for(int i = 0; i < length; ++i)

{

do

{

double value = (numerator_pow(++k, i)) / (2*k + 1);

cout << i << " " << value << endl;

if(((k % 2 == 1) ? neg_delta : value) > ((k % 2 == 1) ? value : delta))

pi += value;

else

{

int digit = (int)pi;

double temp = (pi + value) * 10;

pi = temp - (digit * 10);

digits[i] = digit;

break;

}

} while(true);

}

}

since pi is equal to:

∞

π = ∑ 2(-1)^k x 3^(1/2-k)

k=0 -----------------------

2k + 1

what i decided to do is to evaulate each term. one i found that the one's column was no longer affected, that i could round off the deciamals, so for the first case, get 3, subtract that from the current obtained value of pi, then multiply this value by 10 and any other caluclated terms now by 10... one i find that the ones now is no longer affected, i do that same thing over again... extract the 1, multiply pi by 10, and any other terms now by 100.

However... this system only works for calculating the first 17 or so digits of pi. what am i doing wrong? here is the code to aid u helping me ;)

CODE----------------------

// Evaluates the numerator for the calc_pi function; SEE BELOW

inline double numerator_pow(int k, int i)

{

double value = 2 * pow(3.0, (0.5 - double(k))) * pow(10.0, i);

return (k % 2 == 1) ? -1 * value : value;

}

// Fills an array with the various digits of pi

// ∞

// π = ∑ 2(-1)^k x 3^(1/2-k)

// k=0 -----------------------

// 2k + 1

//

// NOTE: Floating point impression prevents this line:

// double value = numerator(++k) / (2*k + 1) * pow(10.0, i);

// from producing a value greater than zero after the first 15 digits

void pi_digits(char* digits, int length)

{

int k = 0; // Term counter for the sumnation

double pi = 2 * sqrt(3.0); // Calculate the first term in the expansion

const double delta = 0.0000001; // Calculate a limiter value of the set precision

const double neg_delta = -1 * delta; // Negative limiter. Half the term are negative so lowers

// the number of operations if its calculated before

for(int i = 0; i < length; ++i)

{

do

{

double value = (numerator_pow(++k, i)) / (2*k + 1);

cout << i << " " << value << endl;

if(((k % 2 == 1) ? neg_delta : value) > ((k % 2 == 1) ? value : delta))

pi += value;

else

{

int digit = (int)pi;

double temp = (pi + value) * 10;

pi = temp - (digit * 10);

digits[i] = digit;

break;

}

} while(true);

}

}

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

#include <limits>

const double delta = std::numeric_limits<double

You might want to use an arbitrary precision library such as GMP (http://www.swox.com/gmp/index.orig.html)

Or you could check this page, it seems to have lots of implementations for computing pi: http://projectpi.sourceforge.net/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
C++

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

// ∞

// π = ∑ 2(-1)^k x 3^(1/2-k)

// k=0 -----------------------

// 2k + 1

//

∞ = ifinity

π = pi

∑ = sigma/summnation notation