I've recently made a pi calculator out of a pretty rediculous algorithm (which is incredible accurate by the way). Unfortunately, C++ doubles can only hold 16 digits of it! I know that's plenty enough for any practical applications, but I want more accuracy! (Call me insane). I'm just looking for the simplest way to extend a number past this restriction (maybe with strings/calculating 16 digits at a time and parsing them to a string?). I tried using a float datatype, and while this gave me 2 more digits, it severely lowered the accuracy of the calculation. The actual formula isn't that big of a deal, but I'll show it for the publics amusement.

int iIterations = 0;
	double dPi = 0;
	double dAccuracy = 0;
	cin >> iIterations;
	cout << "\n\nCalculating...";
	for (double i = 0; i < iIterations; i++)
		dPi += (FACT(6 * i) * (13591409 + (545140134 * i))) / (FACT(3 * i) * pow(FACT(i), 3) * (pow(-640320, (3 * i))));
	
	dPi = dPi / (426880 * sqrt(10005));
	dPi = 1 / dPi;
	cout << "\nPi is: " << setprecision(16) << fixed << dPi;
	dAccuracy = ((dPi / PI) * 100);
	cout << "\nApproximate Accuracy: " << dAccuracy << "%" ;
	return;

Where FACT is a factorial function i made. (is there one include in the cmath library? i dont think there is...)
Feel free to steal the formula since it's been public domain for over 20 years =)

Recommended Answers

All 5 Replies

look at boost libraries and see if it has something useful in its math libraries. Or maybe mathlib has what you want.

Hmmm I checked out both of those libraries, and from the documentation I managed to find (on the homepage for boost, and a general reference for mathlib) there didn't seem to be any extended double/float capabilities. I'm thinking I'm going to have to write my own algorithm to do this. I've done it in QBasic when I was about 13, hah! So it must be possible! I'm just a bit inexperienced with C++, so maybe you or someone could give me a few pointers?

how did you do it in QBasic? did you keep the script? If you did maybe you can just port it to C.

Type float has less precision than double ( 7 digits versus 15). There is a long double type, but it varies among compilers/operating systems as to how much more precision you'll get ( no more at all on Visual C++, typically it's a 10 or 12 byte value on others. Use Solaris, you get 16 bytes, which works out to about 32 digits precision, if I counted right.)

Hmmm no I didn't keep the script. I'm thinking I sould maybe make my own number set, probably in binary. But that would be overwhelmingly complex to integrate into that formula...haha. I didn't keep the script, but I don't think it would hold the accuracy that I want. In the QBasic script it was implemented for an up counter that went higher than the max where I used strings as placeholders for digits past the maximum. The concept wouldn't work for this situation, since infinite decimals are calculated per iteration, and there's no way to put them in a string at 'a certain point' if that makes any sense. I don't want to use a different compiler, I just want to write some good code! Thanks for your help everyone though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.