Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Infinite Series 2

Status
Not open for further replies.

drbk563

IS-IT--Management
Nov 21, 2006
194
0
0
US
I am a c++ program beginner. I am learning c++ on my own. One of the exercise is to calculate pie=4-4/3+4/5-4/7+4/9-4/11.... I have try many ways and I cannot get it to work. Can you please tell me what is wrong? Thank you.



#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

float pie=0;

int count = 0;

for(int i=0,j=1;i<=1000;i++,j=-j)
{
if (i%2==0)

if
pie += (4/((i*2)+1));

else
pie-= (4/((i*2)+1));


// float t=2*i+1;

// pie=pie+4*(j/t);

cout<<count++<<setw(30)<<fixed<<setprecision(16)<<pie<<endl;

}

return 0;
}
 
First, use [ignore]
Code:
[/ignore] tags when posting code.

Code:
if
     pie += (4/((i*2)+1));
            
            else
             pie-= (4/((i*2)+1));
if what? this should always be true since it will default to:
Code:
if pie += (4/((i*2)+1));
and can only be false if pie == 0.
You need to fix this algorithm to get it to work right.

BTW, if you declare pie as a double instead of a float you can increase its accuracy.

Code:
i<=1000
If would be nice if we could get that many digits of precision out of a C++ type, but even a double will only be accurate up to 15 digits.
 
In practice it's a very bad series to calculate pi number. You must calculate over 1000000 members for 5 digits, 100000000 for 7 etc...
 
I got the results I was looking for thank you all for your help.
 
Thank you for (undeserved) star. Better try to implement much more interesting and instructive Wallis' identity calculation:
Code:
        2*2*4*4*6*6...
pi = 2* --------------
        1*3*3*5*5*7...
It yields at least 9 digits for 100000 iterations, 10 for 1e6 and so on (how far?). It's not the best pi alg but it lives near real world computing areal...
 
Just out of curiosity, I coded the wallis identity

Code:
double calculatePiWallis(double iterations)
{
	double retval = 2.0;

	for(int i=2;i<=iterations;i++)
	{
		if (i%2==0)
		{
			retval *= i;
			retval *= i;
		}
		else
		{
			retval /= i;
			retval /= i;
		}
	}
	while (retval > 10)
	{
		retval /= 10;
	}
	return retval;
}

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
Why double iterations? int, of course.
That's not the point.

This implementation has a very low accuracy. For example, I have 10 digits after decimal point (and it's not an optimal alg;) but yours has only 5 digits on 1e6 iterations!

It's clear demonstration of the fact: float point arith != math arith, and (as usually) we can't compensate this fact with double precision only.

You have (subtle) round errors on every iteration (then cumilative effect). Try to keep 100% accuracy as long as you can (for example, calculate separately numerator and denominator - but prevent overflow).

Yes, Wallis' identity is a wonderful training ground for real math (phys etc) computing.

Apropos, serious pi calculations - see, for example:

Note (pi with %.100f):
3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 06286 20899 86280 34825 34211 70679
 
Obviously rounding errors and numerical problems have highest priority, but once you've got them sorted out, speed can be fun, too.

The speed-merchant in me panics a bit when I see a floating-point versus integer compare, followed by an even-odd test, in a loop that you intend to carry out millions of times, and which should be consituting most of the work of your program. Ark's suggestion of sticking to integers makes the compare much quicker; the even-odd test can be got rid of if you simply do both the multiplication and the division in one pass of the loop. This is particularly valuable since the even-odd "if" jumps in a different direction each time the processor hits it; I haven't kept up to date since early Pentiums, but that always used to be a bad thing for pipe-lined processing.

Good luck!
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top