Home Forums Gamescan Chat42 About
* Login   * Register * FAQ    * Search
It is currently Fri 11-21-2025 4:10AM

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: C++ Question
PostPosted: Thu 02-24-2005 6:16PM 
Offline
Lieutenant

Joined: Wed 11-17-2004 6:51PM
Posts: 98

Source: TJ North
Code:
if (check == 1)
            {
              r=0;
              n=1;
              do
               {

                pi += 4*(1-((pow(-1.0,r)))/((2*r)+1));
                r++;
                pi2 += 4*(1-((pow(-1.0,n)))/((2*n)+1));
                n++;
                cout<< pi<<endl;
                cout<<pi2<<endl;

               }
               while ((pi2-pi)>= tol || (pi2-pi)<=tol);
               cout<< ((pi2-pi)/2)<<endl;
            }

Is there a reason that this would spit out a infinite list of zeroes?


Top
 Profile  
    
 Post subject: Re: C++ Question
PostPosted: Thu 02-24-2005 6:31PM 
Offline
Captain

Joined: Mon 01-12-2004 2:59PM
Posts: 109

Source: TJ North
Quote:
while ((pi2-pi)>= tol || (pi2-pi)<=tol)


?


Top
 Profile E-mail  
    
 Post subject:
PostPosted: Thu 02-24-2005 6:33PM 
Offline
"para-dime"
User avatar

Joined: Tue 09-11-2001 2:34PM
Posts: 1084
Location: Off Campus (i.e. not hell)

Source: Off Campus
Ummm... might have taken care of this already, but you need to initialize pi and pi2

_________________
People with doctorate degrees get to be called Doctor. So yes, I guess I am your Master... bitch


Top
 Profile E-mail  
    
 Post subject:
PostPosted: Thu 02-24-2005 6:45PM 
Offline
Lieutenant

Joined: Wed 11-17-2004 6:51PM
Posts: 98

Source: TJ North
while ((pi2-pi)>= tol || (pi2-pi)<=tol);

this is meant so that it runs the loop until pi2 - pi is in a certain tolerance, variable tol. For some reason it doesnt seem right but I don't know of any other way to run a loop until the difference of two numbers falls within a certain range.

What I don't understand is even if I remove the delete the cout statements from the loop, it still outputs 0 even though there is no ouptput statement. What is causing this?


Last edited by mattyharge on Thu 02-24-2005 6:48PM, edited 1 time in total.

Top
 Profile  
    
 Post subject:
PostPosted: Thu 02-24-2005 6:45PM 
Offline
Captain
User avatar

Joined: Tue 02-24-2004 10:30PM
Posts: 150

Source: TJ South
I'm not in that CS 78 , but I helped somebody earlier with that assignment. For starters, you don't need that 1 in 4*(1-((pow.....stuff). If you evaluate (-1)^n / (2*n+1) at n=0 it gives you 1. The 1 is part of the approximation series.

As to why it's zero:

First time through loop:
pi = 1-(-1)^0 = 0. Anything to the zero is 1. So you have 1-1 for the numerator. 0/1 = 0; First part of your loop yields pi = 0

pi2 = 1-(-1) = 2 for numerator, 3 for denomiator, pi2=2/3

Since you #include<cmath> you can use the absolute value function. Makes the condtional statement of the do-while a little easier to read.
while(abs((pi2-pi)) >= tolerance) .........2/3-0

check conditional , 2/3 > tol, repeat loop again
2nd Time through loop:
now r=1 , n=2
pi = 0+2/3=2/3

pi2's numerator :1- (-1)^2 .....1-1 = 0, 0/5 = 0
pi2 = 2/3 + 0 = 2/3

pi2-pi = 2/3-2/3 = 0 ......This is why you're getting zeros for your answer. You keep this pattern up, you add a value, and immediately subtract it which will always give zero.

It's an infinite loop because of your conditional statement. Read it carefully. If pi2-pi > tol, repeat. OR if pi2-pi < tol, repeat. OR if pi2-pi = tol, repeat. Well, no matter what pi2-pi gives, you'll always repeat because pi2-pi will always evaluate to being greater than tol, less than tol, or equal to tol. What you meant to say was


do{
//stuff
}while(fabs(pi2-pi) >= tol);


Hope this helps clear things up some.


Last edited by tansir1 on Fri 02-25-2005 10:55AM, edited 1 time in total.

Top
 Profile E-mail  
    
 Post subject:
PostPosted: Fri 02-25-2005 12:18AM 
Offline
Brigadier General

Joined: Tue 01-22-2002 12:35PM
Posts: 1057
Location: Shawnee Mission, KS

Source: Off Campus
It's been a while since I've used abs, labs, and fabs, but make sure you use the right one. labs is for longs, fabs is for floats, but I don't remember if abs is just for ints or not.


Top
 Profile E-mail  
    
 Post subject:
PostPosted: Fri 02-25-2005 9:35AM 
Offline
Major
User avatar

Joined: Fri 10-25-2002 1:08PM
Posts: 356
Location: St. Louis, MO

Source: Fidelity
http://www.cplusplus.com/ref/cmath/


Top
 Profile  
    
 Post subject:
PostPosted: Fri 02-25-2005 3:22PM 
Offline
Penis Hater
User avatar

Joined: Mon 02-16-2004 1:47PM
Posts: 2106

Source: Off Campus
Put that link away!

...

Give a man a link, help him for a day.
Teach a man to search, help him for life.

*shifty eyes*

_________________
My girlfriend went to London and all I got was this lousy sig.

My new title was my idea...


Top
 Profile  
    
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group