What is the difference between x RadToDeg cos x div and COSC?
The definition of COSC
states dup COS exch div
. Looking at the PostScript Stack Commands Reference (http://www.ugrad.math.ubc.ca/Flat/stack-ref.html) this means:
add a duplicate copy of the top object of the stack to the stack
COS
exchange the position of the top two elements on the stack
divide
In pst-math
the `COS function is defined as cosine on radians. From the manual on page 4:
pst-math introduces natural trigonometric PostScript operators COS, SIN and TAN defined by
cos: ℝ → [−1,1], x → cos(x)
So x COSC
becomes:
x dup COS exch div
→ x x COS exch div
→ x COS x div
→ x radian_cos x div
,
i.e., x RadToDeg cos x div
.
3 4 div
is valid, but 3 0 div
throws an error. With
3 0 DIV
it is valid but returns simply 3
. That is the difference between div
and DIV
In order to understand what DIV
and COSC
do behind the scene, it will be better if you can recreate them only with pst-plot
as follows.
\documentclass[border=10pt,pstricks]{standalone}
\usepackage{pst-plot}
\pstVerb{
/DIV {dup 0 eq {pop 1}{} ifelse div} bind def
/COSC {dup RadtoDeg cos exch DIV} bind def
}
\psset{xunit=.5,yunit=2}
\begin{document}
\begin{pspicture}(-15,-5)(15,5)
\psplot[plotpoints=500]{-15}{15}{x COSC}
\end{pspicture}
\end{document}
Notes:
DIV
is just an operator that takes two operandsx
andy
and returnsx/y
ify
is not0
. Otherwise, it returnsx/1=x
.Let's trace it step by step for non zero
y
.x y DIV x y dup 0 eq {pop 1}{} ifelse div x y y 0 eq {pop 1}{} ifelse div
As
y 0 eq
return false then the continuation jumps to{}
which is empty.x y div
Let's trace it step by step for
y=0
.x 0 DIV x 0 dup 0 eq {pop 1}{} ifelse div x 0 0 0 eq {pop 1}{} ifelse div
As
0 0 eq
return true then the continuation jumps to{pop 1}
.x 0 pop 1 div
pop
removes the top operand which is0
.x 1 div x
COSC
is also just an operator that takes one operandx
(in radian) and returncos(x)/x
for non zerox
and returnscos(x)
forx=0
.x COSC x dup RadtoDeg cos exch DIV x x RadtoDeg cos exch DIV x x_deg cos exch DIV x cos(x_deg) exch DIV cos(x_deg) x DIV
Final result is
cos(x_deg)/x
for non zerox
butcos(x_deg)
forx=0
.