# How to use dup which is one of PostScript RPN keywords?

According to the pstricks-add manual on near the last page, dup is used to duplicate the value of the previous stack. Duplicating can simplify an RPN expression.

For the sake of simplicity, let's consider the following example. A line of length 3 unit at a slope of 30 degrees is first drawn. The end point of this line (which is not on the origin) will be marked with a dot. I wrote 4 possible RPN notations for the dot, as follows:

\documentclass{article}
\usepackage{enumerate}

\def\theta{30}

\parskip=40pt
\parindent=0pt

\begin{document}

\begin{enumerate}[a.]
\begin{pspicture}[showgrid](3,2)
\end{pspicture}\\[20pt]

\item\verb|\radius\space dup \theta\space cos mul \theta\space sin mul|\\[20pt]
\begin{pspicture}[showgrid](3,2)
\psdot[linecolor=green](!\radius\space dup \theta\space cos mul \theta\space sin mul)
\end{pspicture}\\[20pt]

\begin{pspicture}[showgrid](3,2)
\end{pspicture}\\[20pt]

\begin{pspicture}[showgrid](3,2)
\end{pspicture}\\[20pt]
\end{enumerate}
\end{document}


In the first two examples, I got wrong dots. But the last 2 examples, the dots are drawn correctly. How to use the keyword dup ?

-

Every PostScript operator pops the required operands from the top of the operand stack and pushes the result back. In case of your first expression we get (the right side of => shows the operand stack from bottom to top):

THETA dup   => THETA THETA
cos         => THETA cos(THETA)


This is obviously not the result you're looking for, since the sin operator takes the result of the cosoperation previously applied. In order to move the value THETA (located at the bottom of the stack) to the top, insert exch after the first multiplication. It swaps the two topmost stack elements:

THETA dup   => THETA THETA
cos         => THETA cos(THETA)


Thus, your first variant should look like this to get the desired result:

\psdot[linecolor=red](!\theta\space dup cos \radius\space mul exch sin \radius\space mul)


The second expression evaluates like so:

RADIUS dup  => RADIUS RADIUS


In order to fix it, it's necessary to move the value RADIUS from the bottom to the top before applying the second multiplication. This can be achieved with the roll operator that circularly moves a given number of stack elements. 3 2 roll circularly shifts the three topmost elements "upwards" two times:

RADIUS dup  => RADIUS RADIUS

\psdot(!\radius\space dup \theta\space cos mul \theta\space sin 3 2 roll mul)