### cos, sin and tan

I found the following information

sin(x) = x - (x^3)/3! + (x^5)/5! + ... +
(-1)^i*(x^(2*i+1))/(2*i+1)!

cos(x) = 1 + (x^2)/2! - (x^4)/4! + ... +
(-1)^i*(x^(2*i))/(2*i)!

sum up to i = 10 should be enough.

Now my problem is I can't read it...

can somebody explain what it means?
I have to write my own cos, sin, and tan functions for Heap Variable.
That's a Taylor expansion. When people write ^ they often mean "raised to the power of", so x^3 means "x raised to the power of 3". (In C and C++, ^ means XOR.)

http://stackoverflow.com/questions/2284860/how-does-c-compute-sin-and-other-math-functions
closed account (o1vk4iN6)
It's a summation used to calculate sin(), it's a lot easier to read in this form:

You would keep subbing in values for "n" or "i" in your function, until the value you are adding is less than what you can store in a value for your precision.

So you would use the end of what was given to you:

 (-1)^i*(x^(2*i+1))/(2*i+1)!

You sub in number's from 1 to infinite for i and keep adding the result into a variable.

 ``12345678`` `````` double total = 0; double x; for(int i = 0; i < 10; ++i) { total += pow(-1.0, i) * ( pow(x, 2 * i + 1) / factorial(2 * i + 1) ); } ``````

That's how it would look, of coarse you could probably optimize the calculation in some places and that for loop is only for 10 iterations idk how many you would actually need, you should do a comparison and determine the lowest possible precision that can be added to total or do a set number of iterations your choice really.
Last edited on
How REALsilly programmers calculate sine and cosine:
 ``12345678910`` ``````std::complex const e (2.7182818284590452); std::complex const i (0.0, 1.0); std::complex theta (3.1415926535897932/6.0); std::complex result = std::pow(e, i*theta); double cosine = result.real(); double sine = result.imag(); double tangent = sine/cosine;``````
Demo: http://ideone.com/zWkEI
Reference: http://www.cplusplus.com/reference/std/complex/

Note: I understand if you have to calculate it with the algorithm above, I am just showing this in case you can use it ;)
Last edited on
Now that I have cos and sin, what is the formula for tan?
Tan(x) = Sin(x) / Cos(x)
Thank you, this resolves my problem on this topic.
By the way, L B, silly programmers would prefer something like this:

 ``123456`` ``````std::complex const i (0.0, 1.0); std::complex x (0.25); // Vary from 0 to 2 double sine = (std::pow(i, 4*x-1)+i)/(2*std::pow(i, 2*x)); double cosine = (std::pow(i, 4*x)+1)/(2*std::pow(i, 2*x)); double tangent = (std::pow(i, 4*x-1)+i)/(std::pow(i, 4*x)+1); // Will divide by zero at x = 0.5 or x = 1.5 ``````

Or:
 ``12345678`` ``````std::complex const i (0.0, 1.0); std::complex x (0.25); // Vary from 0 to 2 std::complex result = std::pow(i, 2*x); double sine = result.imag(); double cosine = result.real(); double tangent = sine/cosine;``````

These functions are of the form sin(x*pi), cos(x*pi) and tan(x*pi).
Last edited on
Topic archived. No new replies allowed.