Question
Hello, I am having trouble with my C++ homework where I am using several user-defined functions to compute the cosine/sine of an angle with taylor
Hello, I am having trouble with my C++ homework where I am using several user-defined functions to compute the cosine/sine of an angle with taylor series given the angle in degrees. The problem lies in the fact that my sine function works while my cosine function does not. No matter the input in radians I always get infinity. I've deducted that it most likely isn't my radians or my factorial functions that are causing my issue due to the fact that they work in my sine function.
Here is my cosine and sine functions:
double mySine(double radians);
double myCosine(double radians);
int main()
{
double radians;
cout << "Enter a value for theta in radians: ";
cin >> radians;
cout << endl;
double sine = mySine(radians);
double cosine = myCosine(radians);
cout << "Sine is: " << sine << endl;
cout << "Cosine is: " << cosine << endl;
return 0;
}
double myCosine(double radians)
{
double difference = 1;
double series;
double i = 0;
double cosSum = 0;
while(difference > 0.00001)
{
series = (pow(-1, i) / (factorial(2.0 * i))) * pow(radians, (2 * i));
cosSum += series;
difference = fabs(series/cosSum);
++i;
}
return cosSum;
}
double mySine(double radians)
{
double difference = 1;
double series;
double i = 0;
double sinSum = 0;
while(difference > 0.00001)
{
series = (pow(-1, i) / (factorial((2.0 * i) + 1))) * pow(radians, ((2 * i) + 1));
sinSum += series;
difference = fabs(series/sinSum);
++i;
}
return sinSum;
}
Here is my output for pi/2:
Enter a value for theta in radians: 1.57
Sine is: 1 Cosine is: inf
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started