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);
}