473,394 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
0 4095

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

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
4
by: Edwin Young | last post by:
Hi, I think there might (emphasize might) be a minor bug in Python's cmath library. Basically, Python says cmath.asin(0) = 0-0j (+0 real part, -0 imaginary part). I think it should be 0+0j...
3
by: David | last post by:
How can you write a program in C++ by using loop to control the calculation without the use of library function pwr .For example 3 to the power of 4 (ie 3x3x3x3). Any help will be appreciated
14
by: Protoman | last post by:
How do I write a function to calculate the nth root of a number; I'm sick of using pow() with a fractional exponent to do the job. Got any ideas on how to do this? Thanks for the help!!!
21
by: Protoman | last post by:
How do I calculate the log of a number? Don't tell me to use log in cmath; I'm trying to learn complex math in C++. Got any tips/ideas on how to do this? Thanks for the help!!!
38
by: jdcrief | last post by:
Complier: Visual C++ 2005 Express Edition The program I wrote will compile and execute, but the output is always the same, no matter what number is entered in for the radius of the circle. ...
1
by: Mark Dickinson | last post by:
I was a little surprised by the following behaviour: Python 2.5 (r25, Oct 30 2006, 20:50:32) on darwin Type "help", "copyright", "credits" or "license" for more information. .... def...
25
by: Umesh | last post by:
i want to calculate the time required to execute a program. Also i want to calcute the time remaining for the execution of the program. how can i do that? pl mention some good websites for...
2
by: becky808 | last post by:
Hi, I'm trying to write a program to calculate n factorial but it won't compile. Can anyone tell me what I'm doing wrong? #include <iostream> #include <cmath> using namespace std; int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.