Sine (Sin) Algorithm Help

This is a discussion on Sine (Sin) Algorithm Help within the C Programming forums, part of the General Programming Boards category; So I'm a college student down in Texas and I'm crunching for time atm with finals coming up and programs ...

Sine (Sin) Algorithm Help

So I'm a college student down in Texas and I'm crunching for time atm with finals coming up and programs due in a few days.

Anyways, my professor gave me this:

double sin( float value angle){
Compute the sine of “angle” using as many terms in the series as required till two successive approximations differ by no more than 0.00000001. Hint: see the example for computing square roots. The sine of a number may be found using the following series.
Sin( x ) = x – x3/3! + x5/5! – x7/7! + ●●●
}

To figure out the sin function for my program I need to figure out the algorithm to calc a sin.

We were also given this snipet of code to help us try to do this, but it is wiht square roots and is very little help to me if I can't figure out how to put the Sin(x)... into algorithm format.
Here is the code snipet.

This will give you the formula for one term. With that formula, you can easily write a for loop. Instead of looping to infinity, loop to some arbitrary maximum. Test that your implementation works. Only when you have checked that your implementation at this point is correct then begin to think about how to implement the requirement to terminate when "two successive approximations differ by no more than 0.00000001".

Summation of ((x^n)/n!) - ((x^n+2)/(n+2)!) ? Or something close to it?

You're close, but the problem is that that is two terms. You can still write a for loop with 2 terms, but it will affect the "two successive approximations differ by no more than 0.00000001" requirement since you would only be able to terminate when two successive pairs of approximations differ by no more than some value.

Ah, you have the right idea. It would be more like:
n = 0 to infinity
sine(x) = (-1)^n * (x^(2n+1) / (2n+1)!)

Now, note that you do not actually need to compute (-1)^n. Rather, note that when n is even you add to the sum, and when it is odd, you subtract from the sum.

You also do not need to compute x^(2n+1) in full on each iteration of the loop. Rather, note that on the previous iteration you already calculated x^(2n-1), so now you just need to multiply that with x^2. Likewise, you do not need to compute (2n+1)! in full on each iteration of the loop.

Obviously, you don't want to update x (as that is your input of the angle in radians), but I take that's a typo.

You can do the if(i % 2 == 0) "automatically" by just having a variable sign that you set to 1 to begin with, then alternate the sign as you loop through (sign = -sign), and multiply into the calculation. That is highly likely to be faster than the %2 check (even if the compiler optimizes it to better than "divide by two").

You can also calculate the (2n+1)! as part of your loop, rather than doing a factorial each iteration. d = d * i * (i +1) or something like that.

Well ** doesn't work in C either - but if you are just writing it symbolically, then that's fine.

The x2p1 is "x to the power of 2n+1" - perhaps there is a better name for that, but my point is that you don't need to calculate x**(2n+1) each time you do the loop, but instead, you can calculate x**(2n+1) iteratively by just multiplying the current value by x *x - and to fix up the sign, multiply by -1 as well.

The ** doesn't work in C? Lol.. I've been programming in Ada for too long then.

So, instead of the x**(2n+1) I just use x*x *-1, or would it be y = y *x*x*-1? (Again, sorry if it is annoying, I'm just trying to learn C, and to pass my class with an understanding of the language. But C is very new to me)