By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Calculating SIN/COS without CMATH. Using DEV C++

P: 1
Hit me up on AIM/YIM (oipaloi). I'm not a code monkey and would like to get some advice. Or hit up this thread right here.

I'm using DEV C++ as my compiler.

I'm trying to calculate SIN/COS/TAN, etc without the use of CMATH. Using two recursive functions for the power and factorial. Gotta do some other things as well, but right now I'm just wondering why my values are off. Just wondering where I'm going wrong. Here's the code I've got so far. The first value in "ang.txt" is 90. When I get the number back (in radians), it's not one like it should be.

#include <cstdlib>
#include <iostream>
#include <fstream>

float cos(float x, int n);
float sin(float x, int n);
float fact(int n);
float power(float x, int n);


using namespace std;

int main(int argc, char *argv[])
{
const float pi(22.0/7);
float e, f, x;
int n, a;
ifstream fin("ang.txt");
fin>>a;
cout<<"How many terms do you want to sum to?"<<endl;
cin>>n;
e=sin(x,n)*180/pi;
f=cos(x,n)*180/pi;
cout<<endl<<"angle "<<a<<": sin = "<<e<<" cos = "<<f<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}



float fact(int n)
{
if (n<2)
return 1;
else

return n*fact(n-1);

}

float power(float x, int n)
{
ifstream fin("ang.txt");
fin>>x;
if (n==0)
return 1;
else
fin.close();
return x*power(x,n-1);

}

float sin(float x, int n)
{
ifstream fin("ang.txt");
fin>>x;
do
{
if (n==0)
return 1;
else
fin.close();
return (power(-1,n)*power(x,(2*n)+1))/fact((2*n)+1);
} while(!fin.eof());

}

float cos(float x, int n)
{
ifstream fin("ang.txt");
fin>>x;
if (n==0)
return 1;
else
fin.close();
return (power(-1,n)*power(x,2*n))/fact(2*n);

}

Nov 7 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.