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:

  1. add a duplicate copy of the top object of the stack to the stack

  2. COS

  3. exchange the position of the top two elements on the stack

  4. 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 operands x and y and returns x/y if y is not 0. Otherwise, it returns x/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 is 0.

    x 1 div
    x
    
  • COSC is also just an operator that takes one operand x (in radian) and return cos(x)/x for non zero x and returns cos(x) for x=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 zero x but cos(x_deg) for x=0.

Tags:

Pstricks