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

PLEASE see if you can HELP

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2236 Views - Last Post: 27 March 2010 - 11:35 PM Rate Topic: -----

#1 keeper962  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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;
}




Thank You for your help.

Is This A Good Question/Topic? 0
  • +

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

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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.

A recent thread asking about a similar thing

An example snippet

However, it has been pointed out (in that thread) that there are faster ways to calculate it.
Was This Post Helpful? 0
  • +
  • -

#3 keeper962  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • 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;


Was This Post Helpful? 0
  • +
  • -

#5 keeper962  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • 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

Add a semicolon here:
double precision = 20; // >>> NOTICE added semicolon
double factorial(int num); // >>> ADDED semicolon


Was This Post Helpful? 0
  • +
  • -

#7 keeper962  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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??
Was This Post Helpful? 0
  • +
  • -

#8 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 keeper962  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#11 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • 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

Function prototype header:
double cosine(double num);


Function implementation header:
double cosine(double num, double precision = 20)


Do they match ...? By the way, default parameter initialization is done ONLY in the function prototype.
Was This Post Helpful? 1
  • +
  • -

#12 keeper962  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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);


Was This Post Helpful? 0
  • +
  • -

#14 keeper962  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2