1. Arithmetic and Math Operators

1.1. Overview

PostScript provides a comprehensive set of arithmetic and mathematical operators for numerical computation. These operators work with both integer and real (floating-point) numbers, with automatic type promotion where appropriate.

1.2. Categories

1.2.1. Basic Arithmetic

The fundamental arithmetic operations:

  • add - Add two numbers

  • sub - Subtract two numbers

  • mul - Multiply two numbers

  • div - Divide (real result)

  • idiv - Integer division

  • mod - Modulo (remainder)

1.2.2. Unary Operations

Operations on a single number:

  • abs - Absolute value

  • neg - Negate

  • ceiling - Round up to integer

  • floor - Round down to integer

  • round - Round to nearest integer

  • truncate - Truncate to integer

1.2.3. Exponential and Logarithmic

Power and logarithm functions:

  • sqrt - Square root

  • exp - Exponential (base^exponent)

  • ln - Natural logarithm (base e)

  • log - Common logarithm (base 10)

1.2.4. Trigonometric

Trigonometric functions (angles in degrees):

1.2.5. Bitwise Operations

Operations on integer bit patterns:

  • bitshift - Bitwise shift left or right

1.2.6. Random Numbers

Pseudo-random number generation:

  • rand - Generate random integer

  • srand - Seed random generator

  • rrand - Get/set random seed

1.3. Type Coercion Rules

1.3.1. Integer vs. Real Results

  • If both operands are integers and the result fits in integer range, the result is an integer

  • Otherwise, the result is a real number

  • The div operator always returns a real, even with integer operands

  • The idiv operator always returns an integer

1.3.2. Examples

3 4 add        % → 7 (integer)
3 4.0 add      % → 7.0 (real)
4 2 div        % → 2.0 (real, always)
4 2 idiv       % → 2 (integer, always)

1.4. Common Pitfalls

Division by Zero

Dividing by zero results in an undefinedresult error:

5 0 div    % Error: undefinedresult

Domain Restrictions

Some operations have restricted domains:

  • sqrt requires non-negative arguments

  • ln and log require positive arguments

  • exp with fractional exponent requires non-negative base

-4 sqrt    % Error: rangecheck
0 ln       % Error: rangecheck
-2 0.5 exp % Error: undefinedresult

Trigonometric Functions Use Degrees

PostScript trigonometric functions work in degrees, not radians:

90 sin     % → 1.0 (90 degrees)
0 cos      % → 1.0 (0 degrees)
1 0 atan   % → 90.0 (returns degrees)

Integer Overflow

When integer operations exceed the maximum integer value, the result becomes a real:

2147483647 1 add  % → 2147483648.0 (real)

1.5. Best Practices

1.5.1. Use Appropriate Division Operator

  • Use div for mathematical division requiring precise real results

  • Use idiv when you need integer quotients (truncated toward zero)

  • Use mod to get the remainder

1.5.2. Check Domain Before Operations

For operations with restricted domains, validate input:

/safesqrt {  % num -> real or error
  dup 0 lt {
    pop
    (sqrt: negative argument) print
    0
  } {
    sqrt
  } ifelse
} def

1.5.3. Be Aware of Precision

Real numbers have finite precision. Avoid comparing reals for exact equality:

% Bad:
0.1 0.2 add 0.3 eq  % May be false due to rounding

% Better:
/nearlyequal {  % num1 num2 tolerance -> bool
  3 1 roll sub abs exch lt
} def

0.1 0.2 add 0.3 0.0001 nearlyequal  % true