Button for 1977 Button for 1984 Button for 1990 Button for 1995 Button for MDC Button for notes Button for examples

$%COS

Draft MDC Standard

2 MATH Library
2.21 COS

Option 1, optimized for speed, not precision

COS(X) ;
	; This version of the function is
	; optimized for speed, not for precision.
	; The ‘precision’ parameter is not supported,
	; and the precision is at best 1 in 10**–9.
	; Note that this function does not accept its
	; parameter in degrees, minutes and seconds.
	;
	New A,N,PI,R,SIGN,XX
	;
	; This approximation only works for 0 <= x <= pi/2
	; so reduce angle to correct quadrant.
	;
	Set PI=$%PI^MATH(),X=X#(PI*2),SIGN=1
	Set:X>PI X=2*PI-X
	Set:X*2>PI X=PI-X,SIGN=–1
	;
	Set XX=X*X,A(1)=–0.4999999963,A(2)=0.0416666418
	Set A(3)=–0.0013888397,A(4)=0.0000247609,A(5)=–0.0000002605
	Set (X,R)=1 For N=1:1:5 Set X=X*XX,R=A(N)*X+R
	Quit R*SIGN

Option 2, optimized for precision, not speed.

COS(X,PREC) ;
	New L,LIM,K,SIGN,VALUE
	; The official description does not mention that
	; the function may also be called with the first
	; parameter in degrees, minutes and seconds.
	Set:X[":" X=$%DMSDEC^MATH(X)
	;
	Set PREC=$Get(PREC,11)
	Set X=X#(2*$%PI^MATH())
	Set (VALUE,L)=1,SIGN=–1
	Set LIM=$Select((PREC+3)'>11:PREC+3,1:11),@("LIM=1E-"_LIM)
	For K=2:2 Do  Quit:($Translate(L,"-")<LIM)  Set SIGN=SIGN*–1
	. Set L=L*X*X/(K–1*K),VALUE=VALUE+(SIGN*L)
	. Quit
	Quit VALUE
Button for 1977 Button for 1984 Button for 1990 Button for 1995 Button for MDC Button for notes Button for examples

Copyright © Standard Documents; 1977-2024 MUMPS Development Committee;
Copyright © Examples: 1995-2024 Ed de Moel;
Copyright © Annotations: 2003-2008 Jacquard Systems Research
Copyright © Annotations: 2008-2024 Ed de Moel.

Some specifications are "approved for inclusion in a future standard". Note that the MUMPS Development Committee cannot guarantee that such future standards will indeed be published.

This page most recently updated on 16-Nov-2023, 16:49:04.

For comments, contact Ed de Moel (demoel@jacquardsystems.com)