# PostScript operators

Some basic PostScript operators.

## Interaction

These operators can be used interactively in the Ghostscript interpreter (`gs`

).

Use `=`

or `==`

to pop the top item from the stack and display it.

```
1 2 3 4 5 =
% Prints: 5
% Contents of stack: 1 2 3 4
```

Use `stack`

or `pstack`

to display the stack contents, one per line.

```
1 2 3 stack
% Prints:
% 3
% 2
% 1
```

## Stack

`clear`

removes all items from the stack.

```
1 2 3 clear
% stack is now empty
```

`pop`

removes the top item from the stack.

```
1 2 3 pop
% stack: 1 2
```

`dup`

duplicates the top item on the stack.

```
1 2 3 dup
% stack: 1 2 3 3
```

`n j roll`

rotates the stack. This takes the top *n* items and rotates them *j* times. The sign of *j* indicates in which direction to rotate.

```
6 7 8 9 3 1 roll
% stack: 6 9 7 8
6 7 8 9 3 -1 roll
% stack: 6 8 9 7
```

`exch`

exchanges the top two items on the stack. This is useful to let procedures take arguments.

```
1 2 3 exch
% stack: 1 3 2
```

## Arithmetic

Addition, subtraction, and multiplication:

```
1 2 add % 1 + 2 => stack: 3
3 4 sub % 3 - 4 => stack: -1
5 6 mul % 5 * 6 => stack: 30
```

Floating point division, integer division, and modulo:

```
5 3 div % 5 / 3 => stack: 1.66667
5 3 idiv % 5 / 3 => stack: 1
5 3 mod % 5 % 3 => stack: 2
```

Reverse sign:

```
1 neg % stack: -1
```

## Variables and procedures

`def`

assigns a value to a key.

```
/ppi 72 def
% 72 is assigned to "ppi"
% stack is empty
5 ppi % stack: 5 72
mul % stack: 360
```