Thread: User defined cos function

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    25

    User defined cos function

    I am having trouble with this problem. I have attached the assignment. My main problem is understanding what is being asked of me, I must have no idea. My professor told me I must use a loop because we don't know how many terms to use.

    Here is what I have for the function (sorry for the cheesy comments we have too):
    Code:
    float cosine(float rad, double a)
    {
    	//variable declarations
    	float cosineofradians;
    	float t2, t3, t4, t5;
    
    	t2 = (pow(rad, 2)/2.0);
    	t3 = (pow(rad, 4)/24.0);
    	t4 = (pow(rad, 6)/720.0);
    	t5 = (pow(rad, 8)/40320.0);
    
    	if(-t2<a||t2<a)
    	{
    		t2 = 0;
    	}
    	if(-t3<a||t3<a)
    	{
    		t3 = 0;
    	}
    	if(-t4<a||t4<a)
    	{
    		t4 = 0;
    	}
    	if(-t5<a||t5<a)
    	{
    		t5 = 0;
    	}
    
    	//calculating cosine of radians
    	cosineofradians = 1-t2+t3-t4+t5;
    
    	//returning the cos of radians
    	return cosineofradians;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    What part don't you understand?

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    What happens when you test the absolute value and when do you do it?

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You test it each time you generate a term.

    I'm not sure I understand what you mean when you ask what happens. What always happens when you perform a test? If the test succeeds, something happens, if it fails, something else might happen.

    Do you know what an absolute value is? Do you know what is meant by “terms”? Do you understand summation notation and the idea of a series? It's very hard to get a grasp of what is confusing you.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    I think I have the testing the absolute value of the terms down. Maybe I am confused on what a term is and the series.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I'd expect calculus to be taught in the same semester as (or before) an introduction to programming, but perhaps not...

    Google around for information on summation and series. You don't actually need to know calculus to understand what this question is asking; you just need to learn a little terminology.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    I am in calculus and I have an A with one month left in the semester so I think the problem is somewhere else. In the program I included I tested the absolute value up to five terms.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Instead of having separate variables for t2, t3, t4, t5 write a loop construct that computes 1 the first time through the loop, computes t2 the second time around, computes t3 the third time around, computes t4 the fourth time, computes t5 the fifth time ...... ad nauseum (or at least, until some criterion is met for stopping running around the loop).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I see no loop; so I do not think your understanding the problem the same way that I am understanding it. The way I read it it could run many times past the 5 terms you have in your code.

    Tim S.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    Ok I think I am getting close to the objective. I have made this, but it doesn't quite work.
    Just my function:


    Code:
    double cosine(double rad, double a)
    {
    	double cosineofradians=1;
    	int n=2;
    
    	
    	while(-(pow(rad, n)/factorial(n))<a||(pow(rad, n)/factorial(n))<a)
    	{
    		if(n%2==0)
    		{
    			cosineofradians-=(pow(rad, n))/factorial(n);
    		}
    		else
    		{
    			cosineofradians+=(pow(rad, n))/factorial(n);
    		}
    		n+=2;
    	}
    
    	return cosineofradians;
    }

  11. #11
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by zfite View Post
    Ok I think I am getting close to the objective. I have made this, but it doesn't quite work.
    Just my function:


    Code:
    double cosine(double rad, double a)
    {
    	double cosineofradians=1;
    	int n=2;
    
    	
    	while(-(pow(rad, n)/factorial(n))<a||(pow(rad, n)/factorial(n))<a)
    	{
    		if(n%2==0)
    		{
    			cosineofradians-=(pow(rad, n))/factorial(n);
    		}
    		else
    		{
    			cosineofradians+=(pow(rad, n))/factorial(n);
    		}
    		n+=2;
    	}
    
    	return cosineofradians;
    }
    The way you've written this is confusing because 'n' is not the same as the 'n' in the mathematical expression. In the normal summation notation, that n is always incremented by 1. You're supposed to do this and raise the power to 2 * n. In the way your code currently is n follows 2, 4, 6, etc. so n % 2 == 0 is always true.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You might want to consider the fact that factorial(n) will overflow for values of n that are not particularly large. Similarly pow(rad, n) will tend to overflow if rad is greater than 1 (or underflow if rad is less than 1) for values of n that are not particularly large.

    There is a relationship between consecutive terms in the Taylor series that can be used to reduce the need to compute either a power or a factorial - and therefore reduce chances of overflow or underflow.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can't compile library
    By pjs111 in forum C Programming
    Replies: 16
    Last Post: 05-24-2010, 01:42 PM
  2. random number gen difficult
    By Chris_1980 in forum C Programming
    Replies: 21
    Last Post: 04-30-2010, 09:02 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM