October 9th, 2003, 09:31 PM

A program for cos x... i thought odd.
How would you write a program for the cos x. This is just something I overheard in class and got interested. But yet, I would have not been able to think of how to write it. Anyone know? I at least found out that the formula for cos x is 1/0!  x^2/2! + x^4/4!  x^6/6! etc. etc. etc. Although to begin with, I am a little confused on the etc. etc. etc. part with what I saw today. Anyways... to me, sounds interesting. Could I see a program maybe. Bye.
October 9th, 2003, 09:42 PM

that's not a formula, it's an approxamation. It's been a while since my calculus days, but that looks like Taylor's. I could be confusing it with trapezoidal or something else, though.
As for code, as a general rule we don't just right a program. We let you start it and then help you find your bugs.
October 9th, 2003, 10:05 PM

As for the etc. etc..., if you are using floats, they are only accurate to 6 decimal places if I recall correctly. I think doubles go to 14, but this is all from dim memory. In any case, whatever they are, there is no point in carrying on the approximation past the point where they flip any bits. My guess is you could just hardcode x levels of 'etc' and just be done with it. You could do one for floats and another for doubles or just one for doubles and truncate for floats.
Generally, however, we only help those that help themselves.
October 9th, 2003, 10:17 PM

Sorry
I sowwy. I was not thinking about doing it myself first. I just hopped onto here after I remebered the thing. I have tried, and so far this is what I have come up with.
PHP Code:
#include <iostream>
#include <iostream>
using namespace std;
double factorial(double n);
double cos(double n);
int main()
{
double num;
double answer;
cout << "This program will return cos and factorial of a number.\n";
cout << "Enter a number: ";
cin >> num;
answer = factorial(num);
cout << "Factorial of " << num << " is " << answer << '\n';
return 0;
}
double factorial(double n)
{
double ans;
if(n==1) return 1;
ans = factorial(n1)*n;
return ans;
}
double cos(double n)
{
double cos_x;
do
{
1/0(ans)  n * n / 2(ans), n+2, ans+2;
}while (n != 0);
}
...
}
Getting the actual factorial of a number was not hard, but now it seems rough to be able to come up with cos. For one thing, I don't know how to write it in code, it may be obvious... but I'm not sure.
October 9th, 2003, 11:01 PM

It's a summation, so do it in forloop.
Actually, it's a Maclaurin series, which is a special case of a Taylor series. Look it up in a calculus textbook in the chapter on power series. Also look in a book of tables to find series for approximating several trig, exponential, and logrithmic functions. From what I was taught, computers and calculators use Maclaurin series to calculate values for those functions within our programs.
A couple things to remember, though. The trig functions require that the angle be in radians, not degrees (1 degree = pi / 180).
The other thing is that this is an infinite series that we're dealing with, so you have to decide when to stop. Because they converge, each term is smaller than the previous. So one technique is to decide how much error you want to allow in your answer and when you read a term that is smaller than that threshhold you stop. Or you could arbitrarily decide on a fixed number of terms to add together.
Have fun.