Sine and cosine function

The SIN(X) function for any numeric X (in radians) is computed by the formula

X-X**3/FACT(3))+(X**5/FACT(5))-X**7/FACT(7))+...

where FACT is the factorial function. It computes the sine until the last computed term is less than the tolerance value. Likewise the COS(X) function is computed by the formula

1-(X**2/FACT(2))+(X**4/FACT(4))-X**6/FACT(6))+...

We will use the following formulas for computing the sine or cosine function:

SIN(-X)=-SIN(X)SIN(2*Pi+X)=SIN(X)
COS(-X) =COS(X)COS(2*Pi+X)=COS(X)
 
pro X z (Pi,2*Pi)
pro X z (Pi/2,Pi)
SIN(Pi+X)=-SIN(X)SIN(X)=SIN(Pi-X)
COS(Pi+X)=-COS(X)COS(X)=-COS(Pi-X)

 

IMPLEMENTATION
Unit: internal function, external function without procedure statement
 
Parameters: a number X (radians), a positive number P - number of significant digits of result, default is 9
 
Interfaces: PICONST for P<=200, for P>200 we can use the PI function
 
Returns: sine and cosine of X
 


SIN: procedure
parse arg X, P
if P = "" then P = 9; numeric digits P
Pi = PICONST(); Signum = 1
if X < 0 then do; Signum = -1; X = -X; end
Pim2 = Pi * 2; X = X // Pim2; Pid2 = Pi / 2
if X > Pi
  then do; X = X - Pi; Signum = -Signum; end
if X > Pid2 then X = Pi - X
Term = X; Xsup2 = X * X; Sum = X; F = 1
do J = 3 by 2
  Term = -Term * Xsup2 / (J * (J - 1))
  NewSum = Sum + Term
  if NewSum = Sum then return Signum * Sum
  Sum = NewSum
end

 

 


COS: procedure
parse arg X, P
if P = "" then P = 9; numeric digits P
Pi = PICONST(); Signum = 1; X = ABS(X)
Pim2 = Pi * 2; X = X // Pim2; Pid2 = Pi / 2
if X > Pi
  then do; X = X - Pi; Signum = -Signum; end
if X > Pid2
  then do; X = Pi - X; Signum = -Signum; end
Term = 1; Xsup2 = X * X; Sum = 1; F = 1
do J = 2 by 2
  Term = -Term * Xsup2 / (J * (J - 1))
  NewSum = Sum + Term
  if NewSum = Sum then return Signum * Sum
  Sum = NewSum
end

 

CONNECTIONS

Literature
Wirth N., Systematisches Programmieren
- 2nd ed. B.G Teubner, Stuttgart, 1975


Cover Contents Index Main page Rexx page   Mail

last modified 1st August 2001
Copyright © 2000-2001 Vladimir Zabrodsky
Czech Republic
 

1