We help IT Professionals succeed at work.

Approximation of COSINE equation

Medium Priority
680 Views
Last Modified: 2012-06-21
I was given the approximation of COS formula where you get a value close to COS

Here it is:

cos = (1 - (1/2)*radians^2) + ((1/24)*radians^4) - ((1/720)*radians^6)) + ((1/40320)*radians^8)

I always get 1.0 as my answer:(

anyone know the formula to see if I have it correct?
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Except for an extra ) it is correct assuming that ^ means exponentiation in the language that you are implementing it in
CERTIFIED EXPERT
Top Expert 2009

Commented:
and that ^ has a higher precedence than *

Author

Commented:
JAVA

sin = (radians - (1/6)*Math.pow(radians,3)) + ((1/120)*Math.pow(radians, 5)) - ((1/5040)*Math.pow(radians, 7) + ((1/362880)*Math.pow(radians,9);
cos = (1 - (1/2)*Math.pow(radians,2)) + ((1/24)*Math.pow(radians, 4)) - ((1/720)*Math.pow(radians, 6)) + ((1/40320)*Math.pow(radians,8);

Author

Commented:
There is a missing paranthese in the cos formula which has been corrected however I think the formula is wrong because the sin one works.
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> There is a missing paranthese in the cos formula which has been corrected

In your previous post there is still a ) missing at the end ... But I assume that's the one you fixed ?

        cos = (1 - (1/2)*Math.pow(radians,2)) + ((1/24)*Math.pow(radians, 4)) - ((1/720)*Math.pow(radians, 6)) + ((1/40320)*Math.pow(radians,8));

Looks fine to me, assuming that the fractions are accurate real values, and not rounded integer values. Try calculating one term at a time, and see what results you get for each.
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
cos = (1 - (1.0/2)*Math.pow(radians,2)) + ((1.0/24)*Math.pow(radians, 4)) - ((1.0/720)*Math.pow(radians, 6)) + ((1.0/40320)*Math.pow(radians,8));