Multiplies two numbers and returns their product.

1. Description

The mul operator pops two numbers from the operand stack, multiplies them together, and pushes the result back onto the stack. If both operands are integers and the result fits within the integer range, the result is an integer. Otherwise, the result is a real number.

This is a Level 1 operator, available in all PostScript implementations.

2. Syntax

num1 num2 mul product

2.1. Stack Effect

Table 1. Before Execution
Position Content

Top

num2 (integer or real) - Second factor

Top-1

num1 (integer or real) - First factor

Table 2. After Execution
Position Content

Top

product (integer or real) - Result of num1 × num2

3. Parameters

num1

The first factor (integer or real)

num2

The second factor (integer or real)

4. Return Values

product

The product of num1 × num2. Type depends on operands:

  • Integer if both operands are integers and result fits in integer range

  • Real otherwise

5. Examples

5.1. Basic Multiplication

% Integer multiplication
3 4 mul        % → 12 (integer result)

% Real multiplication
2.5 4.0 mul    % → 10.0 (real result)

% Mixed types
5 2.5 mul      % → 12.5 (real result)

% Negative numbers
-3 4 mul       % → -12 (integer result)
-2.5 -4 mul    % → 10.0 (real result)

5.2. Scaling Operations

% Scale a coordinate by a factor
100 1.5 mul    % → 150.0 (scale by 1.5)

% Scale percentage to decimal
15 0.01 mul    % → 0.15 (15% as decimal)

% Convert inches to points (1 inch = 72 points)
8.5 72 mul     % → 612.0 (8.5 inches in points)

5.3. Area Calculation

% Calculate rectangle area
/rectArea {  % width height -> area
    mul
} def

10 20 rectArea  % → 200

5.4. Compound Interest

% Simple interest calculation
/simpleInterest {  % principal rate years -> interest
    mul mul
} def

1000 0.05 3 simpleInterest  % → 150.0 (5% for 3 years)

6. Advanced Examples

6.1. Vector Scaling

/scaleVector {  % [x y] scalar -> [x' y']
    % Scale a 2D vector
    exch aload pop    % scalar x y
    2 index mul       % scalar x (y*scalar)
    3 1 roll          % (y*scalar) scalar x
    mul exch          % (x*scalar) (y*scalar)
    2 array astore    % [x' y']
} def

[10 20] 2.5 scaleVector  % → [25.0 50.0]

6.2. Matrix Scalar Multiplication

/scaleMatrix {  % [a b c d tx ty] scalar -> [a' b' c' d' tx' ty']
    % Multiply all matrix elements by scalar
    [
        7 -1 roll aload pop  % scalar a b c d tx ty
        8 -1 roll            % a b c d tx ty scalar
        dup 8 1 roll         % scalar a b c d tx ty scalar
        mul                  % scalar a b c d tx (ty*scalar)
        7 1 roll mul         % scalar a b c d (tx*scalar) (ty*scalar)
        6 1 roll mul         % scalar a b c (d*scalar) (tx*scalar) (ty*scalar)
        5 1 roll mul         % scalar a b (c*scalar) (d*scalar) (tx*scalar) (ty*scalar)
        4 1 roll mul         % scalar a (b*scalar) (c*scalar) (d*scalar) (tx*scalar) (ty*scalar)
        3 1 roll mul         % (a*scalar) (b*scalar) (c*scalar) (d*scalar) (tx*scalar) (ty*scalar)
    ]
} def

6.3. Product of Array

% Calculate product of all array elements
/productArray {  % [num1 num2 ... numn] -> product
    1 exch   % Start with 1 as multiplicative identity
    {
        mul  % Multiply each element
    } forall
} def

[2 3 4 5] productArray  % → 120

6.4. Factorial

/factorial {  % n -> n!
    dup 1 le {
        pop 1  % 0! = 1, 1! = 1
    } {
        dup 1 sub factorial mul
    } ifelse
} def

5 factorial  % → 120 (5! = 5×4×3×2×1)

7. Edge Cases and Common Pitfalls

Integer overflow results in a real number, not an error.

7.1. Integer Overflow

% Large integers overflow to real
1000000 1000000 mul  % → 1.0e12 (real, exceeds integer range)

% Maximum integer multiplication
46340 46341 mul      % → 2147488340 (still integer)
46341 46341 mul      % → 2147534481.0 (real, overflow)

7.2. Multiplying by Zero

% Zero multiplication
100 0 mul       % → 0 (integer)
100.5 0 mul     % → 0.0 (real)

% Sign is preserved in reals
-5.0 0 mul      % → -0.0 or 0.0 (implementation dependent)

7.3. Precision Loss with Large Numbers

% Very large real numbers lose precision
1.0e20 1.0e20 mul    % → 1.0e40 (may lose precision)

7.4. undefinedresult Error

Extremely large multiplications can overflow:

% Beyond real number range
1.0e308 10.0 mul     % May cause undefinedresult

8. Type Requirements

Both operands must be numeric (integer or real). Other types will cause a typecheck error:

% BAD: Non-numeric operands
(hello) 5 mul        % ERROR: typecheck
10 [1 2] mul         % ERROR: typecheck
  • add - Add two numbers

  • sub - Subtract two numbers

  • div - Divide two numbers (real result)

  • idiv - Integer division

  • mod - Modulo (remainder)

  • exp - Exponentiation

  • sqrt - Square root

10. PostScript Level

Available in: PostScript Level 1 and higher

This is a fundamental arithmetic operator available in all PostScript implementations.

11. Error Conditions

stackunderflow

The operand stack contains fewer than two elements.

5 mul          % ERROR: stackunderflow (need 2 operands)
typecheck

One or both operands are not numbers.

5 (text) mul   % ERROR: typecheck
undefinedresult

The result is outside the representable range for real numbers.

1.0e308 1.0e308 mul  % ERROR: undefinedresult

12. Performance Considerations

Multiplication is a fast primitive operation:

  • Integer multiplication is faster than real multiplication

  • For repeated multiplication by the same factor, consider pre-calculating

  • Use bit-shifting (bitshift) for multiplication by powers of 2 when working with integers

% Multiply by powers of 2 using bitshift (integers only)
10 1 bitshift   % → 20 (same as 10 2 mul)
10 2 bitshift   % → 40 (same as 10 4 mul)

13. Best Practices

  1. Use integer arithmetic when possible for better precision

  2. Be aware of type promotion - one real operand makes real result

  3. Check for overflow in applications with large numbers

  4. Use appropriate precision for the application domain

13.1. Multiplication Order

% Multiplication is commutative, but consider readability
% These are equivalent:
5 10 mul       % → 50
10 5 mul       % → 50

% But may have different meanings:
quantity price mul       % "quantity times price" is clearer
price quantity mul       % Less intuitive

14. See Also


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.