### Taylor Series

How would you find the Taylor series: sin(x) = x - (x^3/3!) + (x^5/5!) - (x^7/7!) + ...
if you could not use the standard library routines such as exp() or pow() or third party API's?
The input for x is in radians and is from a text file and consists of a single row of values.

If I had the following code, how could i compress it?

 ``1234567`` `````` for (int i=0; i
Last edited on
Take another look at that sequence, you have:
 `` `` `` sin(x) = x^1/1! + x^3/3! + x^5/5! + ...``

You don't have to keep calculating those exponents and factorials from scratch every time, you just use the results from the previous term to help calculate the current term.

For example, just looking at the numerator, you'll calculate:
 ``123`` `````` x^1 x^3 = x^1 * x * x x^5 = x^3 * x * x``````

So if you store the result from the previous term, you just multiply by x twice each time.

The denominator is similar.

EDIT: Remember to do the divide using double (not int) or else you'll get a truncated result.
Last edited on
How would i store the previous term?
In two variables, one fo the numerator, the other for the denominator. They're updated on each iteration.
Last edited on
Topic archived. No new replies allowed.