# User defined cos function

• 04-02-2011
zfite
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; }```
• 04-02-2011
cas
What part don't you understand?
• 04-02-2011
zfite
What happens when you test the absolute value and when do you do it?
• 04-02-2011
cas
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.
• 04-02-2011
zfite
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.
• 04-02-2011
cas
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.
• 04-02-2011
zfite
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.
• 04-02-2011
grumpy
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).
• 04-02-2011
stahta01
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.
• 04-02-2011
zfite
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; }```
• 04-03-2011
Mozza314
Quote:

Originally Posted by zfite
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.
• 04-03-2011
grumpy
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.