{TRIG Function library for Pascal; (c) 1981 by Ficomp, Inc. Fairfax, Va.}

MODULE TRIGLIB;
{$M RAD}
{$M DEG}
{$M PI}
{$M TAN}
{$M COTAN}
{$M ASIN}
{$M ACOS}
{$M ATAN2}
{$M *}

CONST
  DRCON  = 0.0174532925;
  HALF_PI = 1.570796327;
  FULL_PI = 3.141592654;
  MAXREAL = 10.0E+17;
VAR
  A:REAL;
EXTERNAL PROCEDURE @ERR(AN_ERROR:BOOLEAN; ERRNUM:INTEGER);

FUNCTION RAD(X:REAL): REAL;
{CONVERTS DEGREES TO RADIANS}
BEGIN
  RAD := X*DRCON
END;

FUNCTION DEG(X:REAL): REAL;
{CONVERTS RADIANS TO DEGREES}
BEGIN
  DEG := X/DRCON
END;

FUNCTION PI(X:REAL): REAL;
{Returns PI*X}
BEGIN
  PI := FULL_PI*X
END;

FUNCTION TAN(X:REAL): REAL;
{TANGENT FUNCTION}
BEGIN
  A := COS(X);
  IF A=0.0 THEN
    TAN := MAXREAL
  ELSE
    TAN := SIN(X)/A
END;

FUNCTION COTAN(X:REAL): REAL;
{COTANGENT FUNCTION}
BEGIN
  A := SIN(X);
  IF A = 0.0 THEN
    COTAN := MAXREAL
  ELSE
    COTAN := COS(X)/A
END;

FUNCTION ASIN(X:REAL): REAL;
{ARCSIN FUNCTION}
BEGIN
  IF X=0.0 THEN
    ASIN:=0.0
  ELSE
    IF X=1.0 THEN
      ASIN:=HALF_PI
  ELSE
    IF X=-1.0 THEN
      ASIN:=-HALF_PI
  ELSE
    IF (X<-1.0) OR (X>1.0) THEN 
      BEGIN
	@ERR(TRUE,4);
	WRITELN('ASIN(',X,') ');
	ASIN :=0.0
      END
  ELSE
    ASIN := ARCTAN(X/SQRT(-X*X+1.0))
END;

FUNCTION ACOS(X:REAL): REAL;
{ARCOSINE FUNCTION}
BEGIN
  IF X=0.0 THEN
    ACOS:=HALF_PI
  ELSE
    IF X=1.0 THEN
      ACOS:=0.0 
  ELSE
    IF X=-1.0 THEN
      ACOS:=FULL_PI
  ELSE
    IF (X<-1.0) OR (X>1.0) THEN
      BEGIN
	@ERR(TRUE,4);
	WRITELN('ACOS(',X,') ');
	ACOS :=0.0
      END
  ELSE
    ACOS := -ARCTAN(X/SQRT(-X*X+1.0))+HALF_PI
END;

FUNCTION ATAN2(X,Y:REAL):REAL;
{arctangent function with quadrant recognition}
BEGIN
  IF X=0.0 THEN
    IF Y=0.0 THEN
      ATAN2:=0.0
    ELSE
      IF Y>0.0 THEN
	ATAN2:=HALF_PI
      ELSE
	ATAN2:=3*HALF_PI
  ELSE
    IF Y=0.0 THEN
      IF X>0.0 THEN
	ATAN2:=0.0
      ELSE
	ATAN2:=FULL_PI
  ELSE
    BEGIN
      A:=ARCTAN(Y/X);
      IF X>0.0 THEN
	ATAN2:=A
      ELSE
	ATAN2:=A+FULL_PI
    END
END;

MODEND.
SE
    BEGIN
      A:=ARCTAN(Y/X);
      IF X>0.0 THEN
