# HELP with calculating Cos with out using Cos(x) funtion

• (2 Pages)
• 1
• 2

## 24 Replies - 2564 Views - Last Post: 27 March 2010 - 11:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=164632&amp;s=5437f4fadb151dcb5d40f5355a3d0989&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 keeper962

Reputation: -1
• Posts: 39
• Joined: 23-February 10

# HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 01:21 PM

Hi I need to find the cosine with out using the cos(x) function that is already in C++.

then I need to check my calculation with the cos(x) function that is in C++.

I am having problems with how to calculate cos

here is what I have so far:

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

int j, x;
double ans, xnum, sign,xden,dummy;

double expo(double x);

void output(double sum);

int main()
{
cout<<"Enter x: "<<endl;
cin>>x;

output(expo(x));

return 0;
}

double expo(double x)
{
ans=1;
xnum=1;
sign=1;

for (int i = 1; i <= 10; i++)

{
sign=sign*(-1);
xnum=x*x*xnum;
xden=1;
dummy=2*i;
}
for (int j = 1; j <= dummy; j++)
{
xden=xden*j;

ans=ans+sign*(xnum/xden);
}

return ans;
}

void output(double ans)
{
cout<<"The value of cos(x) is: " <<ans<<endl;
cout<<"The computation value of cos(x) is: " <<cos(x)<<endl;
}

```

Is This A Good Question/Topic? 0

## Replies To: HELP with calculating Cos with out using Cos(x) funtion

### #2 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 01:24 PM

Use the Taylor's Series for cosine.

An example snippet

However, it has been pointed out (in that thread) that there are faster ways to calculate it.

### #3 keeper962

Reputation: -1
• Posts: 39
• Joined: 23-February 10

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 01:51 PM

thank you I couldn't find that thread I tried the code but It says something is wrong with the factorial.

Here is the code I have so far:
```#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int x;
double ans, num;
double precision = 20
double factorial(int num);
double cosine(double num);

void output(double ans);

int main()
{
cout<<"Enter x: "<<endl;
cin>>x;

cosine(num);

output(ans);

return 0;
}

double factorial(int num)
{
double fact = 1;
for(int i = 1; i <= num; i++)
{
fact *= i;
}
return fact;
}

double cosine(double num)
{
double ans = 0;

for(int x = 0; x < precision; x++)
{

ans = pow(-1.0, x) * pow(num, 2*x)/factorial(2*x);
}

return ans;
}

void output(double ans)
{
cout<<"The value of cos(x) is: " <<ans<<endl;
cout<<"The computation value of cos(x) is: " <<cos(x)<<endl;
}
```

### #4 sarmanu

• D.I.C Lover

Reputation: 966
• Posts: 2,362
• Joined: 04-December 09

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 01:56 PM

```double precision = 20 // >>> Missing ;

```

And here, you have to cast the parameter of "cos", because a double is expected, not an integer:
```cout<<"The computation value of cos(x) is: " <<cos(double(x))<<endl;

```

### #5 keeper962

Reputation: -1
• Posts: 39
• Joined: 23-February 10

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 02:14 PM

It still says there is a problem
```#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int x;
double ans, num;
double precision = 20
double factorial(int num)// this is where the problem is;
double cosine(double num);
```

what do I need to do?

This post has been edited by keeper962: 27 March 2010 - 02:15 PM

### #6 sarmanu

• D.I.C Lover

Reputation: 966
• Posts: 2,362
• Joined: 04-December 09

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 02:16 PM

```double precision = 20; // >>> NOTICE added semicolon
double factorial(int num); // >>> ADDED semicolon

```

### #7 keeper962

Reputation: -1
• Posts: 39
• Joined: 23-February 10

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 02:23 PM

It is saying there is a building error.

I think about the exit code??

### #8 sarmanu

• D.I.C Lover

Reputation: 966
• Posts: 2,362
• Joined: 04-December 09

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 02:30 PM

I think I have explained everything very clear. If you are that slow, sorry. By the way, it's not hard to read: MISSING SEMICOLON BEFORE ___ .You should be able to figure out the problem.

This post has been edited by sarmanu: 27 March 2010 - 02:30 PM

### #9 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 02:33 PM

You're also defining stuff multiple times. You do not need global variables for this exercise.

### #10 keeper962

Reputation: -1
• Posts: 39
• Joined: 23-February 10

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 02:42 PM

It still says there is an error?
Does it work for you?
What do I need to fix?

Heres the code:

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

int x, ans, num;
double precision = 20;
double factorial(int num);
double cosine(double num);

void output(double ans);

int main()
{
cout<<"Enter x: "<<endl;
cin>>x;

cosine(num);

output(ans);
return 0;
}

double factorial(int num )
{
double fact = 1;
for(int i = 1; i <= num; i++)
{
fact *= i;
}
return fact;
}

double cosine(double num, double precision = 20)
{
ans = 0;

for(int x = 0; x < precision; x++)
{

ans = pow(-1.0, x) * pow(num, 2*x)/factorial(2*x);
}

return ans;
}

void output(double ans)
{
cout<<"The value of cos(x) is: " <<ans<<endl;
cout<<"The computation value of cos(x) is: " <<cos(double (x))<<endl;
}
```

It still says there is an error?
Does it work for you?
What do I need to fix?

Heres the code:

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

int x, ans, num;
double precision = 20;
double factorial(int num);
double cosine(double num);

void output(double ans);

int main()
{
cout<<"Enter x: "<<endl;
cin>>x;

cosine(num);

output(ans);
return 0;
}

double factorial(int num )
{
double fact = 1;
for(int i = 1; i <= num; i++)
{
fact *= i;
}
return fact;
}

double cosine(double num, double precision = 20)
{
ans = 0;

for(int x = 0; x < precision; x++)
{

ans = pow(-1.0, x) * pow(num, 2*x)/factorial(2*x);
}

return ans;
}

void output(double ans)
{
cout<<"The value of cos(x) is: " <<ans<<endl;
cout<<"The computation value of cos(x) is: " <<cos(double (x))<<endl;
}
```

### #11 sarmanu

• D.I.C Lover

Reputation: 966
• Posts: 2,362
• Joined: 04-December 09

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 02:46 PM

```double cosine(double num);

```

```double cosine(double num, double precision = 20)

```

Do they match ...? By the way, default parameter initialization is done ONLY in the function prototype.

### #12 keeper962

Reputation: -1
• Posts: 39
• Joined: 23-February 10

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 02:54 PM

Now it compiles but it gives me that my result for my cos is 0.

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

int x, ans, num;

double factorial(int num);
double cosine(double num);
double precision=20;
void output(double ans);

int main()
{
cout<<"Enter x: "<<endl;
cin>>x;

cosine(num);

output(ans);
return 0;
}

double factorial(int num )
{
double fact = 1;
for(int i = 1; i <= num; i++)
{
fact *= i;
}
return fact;
}

double cosine(double num)
{

ans = 0;

for(int x = 0; x < precision; x++)
{

ans = pow(-1.0, x) * pow(num, 2*x)/factorial(2*x);
}

return ans;
}

void output(double ans)
{
cout<<"The value of cos(x) is: " <<ans<<endl;
cout<<"The computation value of cos(x) is: " <<cos(double (x))<<endl;
}
```

### #13 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 03:56 PM

For the love of God please get rid of your global variables.

This is a summation series you need to add each successive term to the prior one:

```ans = pow(-1.0, x) * pow(num, 2*x)/factorial(2*x);

```

should be

```ans += pow(-1.0, x) * pow(num, 2*x)/factorial(2*x);

```

### #14 keeper962

Reputation: -1
• Posts: 39
• Joined: 23-February 10

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 04:07 PM

It still says the value is 0?

### #15 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

## Re: HELP with calculating Cos with out using Cos(x) funtion

Posted 27 March 2010 - 05:21 PM

```//feel free to implement this w/ recursion
double factorial(int num){
double fact = 1;
for(int i = 1; i <= num; i++){
fact *= i;
}
return fact;
}

//Taylor series cos(x) = (-1)^n*x^(2n)/(2n)!
double taylorCosine(double num, double precision = 20){
double value = 0;
for(int n = 0; n < precision; n++){
value += pow(-1.0, n) * pow(num, 2*n)/factorial(2*n);
}
return value;
}

int main(){
double val;
cin >> val;
cout << taylorCosine(val) << endl;
return 0;
}

```

Run that and tell me what you get.