; FUNCTION COSINE(X:REAL):REAL;
; {+++   COSINE ROUTINE, ANGLE "X" MUST BE IN RADIANS   +++}
; CONST	PI = 3.1415926536;
; 	J = 2;
; VAR	M,N : INTEGER;
; 	R,S,T,U,V : REAL;


	NAME COSINE
	ENTRY COSINE


; 	FUNCTION FIX(Y,P: REAL): REAL;
; 	VAR	SGN : -1..1;
; 	BEGIN
L151
	ENTR	D,3,2
; 	  IF Y>0.0 THEN
	LXI	H,-4
	DADD	S
	SPHL
	XCHG
	PUSH	IX
	POP	H
	LXI	B,12
	DADD	B
	LXI	B,4
	LDIR
	MOV	H,A
	MOV	L,A
	MOV	D,A
	MOV	E,A
	PUSH	H
	PUSH	D
	GRET	D,-4
; 	    SGN := 1
	JNC	L165
; 	  ELSE IF Y=0.0 THEN
	MOV	0(IX),A
	MVI	-1(IX),1
	JMP	L178
L165
	LXI	H,-4
	DADD	S
	SPHL
	XCHG
	PUSH	IX
	POP	H
	LXI	B,12
	DADD	B
	LXI	B,4
	LDIR
	MOV	H,A
	MOV	L,A
	MOV	D,A
	MOV	E,A
	PUSH	H
	PUSH	D
	EQUL	D,-4
; 	    SGN := 0
	JNC	L180
; 	  ELSE
	MOV	0(IX),A
	MOV	-1(IX),A
; 	    SGN := -1;
	JMP	L193
L180
	MVI	0(IX),255
	MVI	-1(IX),255
L193
L178
; 	  FIX := SGN * TRUNC(ABS(Y)/P);
	MOV	L,-1(IX)
	MOV	H,0(IX)
	PUSH	H
	LXI	H,-4
	DADD	S
	SPHL
	XCHG
	PUSH	IX
	POP	H
	LXI	B,12
	DADD	B
	LXI	B,4
	LDIR
	CALL	L116
	LXI	H,-4
	DADD	S
	SPHL
	XCHG
	PUSH	IX
	POP	H
	LXI	B,8
	DADD	B
	LXI	B,4
	LDIR
	FDVD	D,-4
	CALL	L129
	POP	H
	MULT	D,0
	CVTF	H
	LXI	H,3
	DADD	S
	XCHG
	PUSH	IX
	POP	H
	LXI	B,19
	DADD	B
	XCHG
	LXI	B,4
	LDDR
	POP	H
	POP	H
; 	END;
	EXIT	D,8
;
;
;
; BEGIN
L138
cosine: ENTR	D,2,24
;   R := 0.0;
	MOV	H,A
	MOV	L,A
	MOV	D,A
	MOV	E,A
	PUSH	H
	PUSH	D
	LXI	H,3
	DADD	S
	XCHG
	PUSH	IX
	POP	H
	LXI	B,-4
	DADD	B
	XCHG
	LXI	B,4
	LDDR
	POP	H
	POP	H
;   IF ABS(X)>PI THEN R := FIX(X,PI);
	LXI	H,-4
	DADD	S
	SPHL
	XCHG
	PUSH	IX
	POP	H
	LXI	B,8
	DADD	B
	LXI	B,4
	LDIR
	CALL	L116
	LXI	H,612
	LXI	D,-30739
	PUSH	H
	PUSH	D
	GRET	D,-4
	JNC	L227
	PUSH	B
	PUSH	B
	LXI	H,-4
	DADD	S
	SPHL
	XCHG
	PUSH	IX
	POP	H
	LXI	B,8
	DADD	B
	LXI	B,4
	LDIR
	LXI	H,612
	LXI	D,-30739
	PUSH	H
	PUSH	D
	CALL	L151	; FIX(...)
	LXI	H,3
	DADD	S
	XCHG
	PUSH	IX
	POP	H
	LXI	B,-4
	DADD	B
	XCHG
	LXI	B,4
	LDDR
	POP	H
	POP	H
L227
;   X := X - R * PI;
	LXI	H,-4
	DADD	S
	SPHL
	XCHG
	PUSH	IX
	POP	H
	LXI	B,8
	DADD	B
	LXI	B,4
	LDIR
	LXI	H,-4
	DADD	S
	SPHL
	XCHG
	PUSH	IX
	POP	H
	LXI	B,-7
	DADD	B
	LXI	B,4
	LDIR
	LXI	H,612
	LXI	D,-30739
	PUSH	H
	PUSH	D
	MULT	D,-4
	DSUB	D,-4
	LXI	H,3
	DADD	S
	XCHG
	PUSH	IX
	POP	H
	LXI	B,11
	DADD	B
	XCHG
	LXI	B,4
	LDDR
	POP	H
	POP	H
;   M := -1;
	MVI	0(IX),255
	MVI	-1(IX),255
;   N := 0;
	MOV	-2(IX),A
	MOV	-3(IX),A
;   S := 1.0;
	LXI	H,320
	MOV	D,A
	MOV	E,A
	PUSH	H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