Divides the first number by the second and returns a real number result.
1. Description
The div operator pops two numbers from the operand stack, divides num1 by num2, and pushes the result back onto the stack. The result is always a real number, even if both operands are integers. For integer division with truncation, use idiv instead.
This is a Level 1 operator, available in all PostScript implementations.
3. Parameters
num1-
The dividend - the number to be divided (integer or real)
num2-
The divisor - the number to divide by (integer or real, must be non-zero)
4. Return Values
quotient-
The quotient num1 ÷ num2 as a real number (always real, even for integer operands)
5. Examples
5.1. Basic Division
% Integer operands still produce real result
3 2 div % → 1.5 (real, not 1!)
% Even exact divisions produce reals
4 2 div % → 2.0 (real result)
% Real division
7.5 2.5 div % → 3.0 (real result)
% Negative division
-10 4 div % → -2.5 (real result)
10 -4 div % → -2.5 (real result)
-10 -4 div % → 2.5 (real result)
5.2. Contrast with idiv
% div always returns real
7 2 div % → 3.5 (real)
% idiv returns integer (truncated)
7 2 idiv % → 3 (integer)
% Use div for mathematical precision
1 3 div % → 0.333333... (real)
% Use idiv for integer quotient
1 3 idiv % → 0 (integer)
6. Advanced Examples
6.1. Aspect Ratio
/aspectRatio { % width height -> ratio
div
} def
1920 1080 aspectRatio % → 1.777... (16:9 ratio)
6.2. Unit Conversion
% Convert between units
/inchesToCm { % inches -> cm
2.54 mul
} def
/cmToInches { % cm -> inches
2.54 div
} def
10 inchesToCm % → 25.4
25.4 cmToInches % → 10.0
7. Edge Cases and Common Pitfalls
Division by zero causes an undefinedresult error.
|
7.1. Division by Zero
% WRONG: Division by zero
10 0 div % ERROR: undefinedresult
% CORRECT: Check divisor first
/safeDivOrZero { % num1 num2 -> result
dup 0 eq {
pop pop 0
} {
div
} ifelse
} def
10 0 safeDivOrZero % → 0 (returns 0 instead of error)
7.2. Always Returns Real
% Even perfect integer divisions return real
100 10 div % → 10.0 (real, not 10)
% If you need an integer result, use idiv or convert
100 10 div cvi % → 10 (converted to integer)
100 10 idiv % → 10 (integer division)
8. Type Requirements
Both operands must be numeric (integer or real). The divisor (num2) must not be zero. Other types will cause a typecheck error:
% BAD: Non-numeric operands
(hello) 5 div % ERROR: typecheck
10 (world) div % ERROR: typecheck
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 div % ERROR: stackunderflow (need 2 operands) typecheck-
One or both operands are not numbers.
5 (text) div % ERROR: typecheck undefinedresult-
The divisor is zero, or the result is outside the representable range.
10 0 div % ERROR: undefinedresult (division by zero) 1.0e308 1.0e-308 div % ERROR: undefinedresult (overflow)
12. Performance Considerations
Division is slower than addition and multiplication but still fast:
-
Real division is performed in hardware on most systems
-
For division by constants, consider pre-calculating the reciprocal
-
For division by powers of 2,
bitshiftis faster (for integers)
% Slower: repeated division
100 { 1000 10 div pop } repeat
% Faster: multiply by reciprocal
/reciprocal 1 10 div def
100 { 1000 reciprocal mul pop } repeat
13. Best Practices
-
Use
divfor mathematical accuracy - it preserves fractional results -
Use
idivfor integer quotients - when you need truncated integers -
Always check for zero divisors in user input or calculations
-
Be aware that result is always real - convert with
cviif needed
14. See Also
-
Arithmetic and Math - All arithmetic operators