Adds two numbers and returns their sum.
1. Description
The add operator pops two numbers from the operand stack, adds 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.
3. Parameters
num1-
The first number to add (integer or real)
num2-
The second number to add (integer or real)
4. Return Values
sum-
The sum of num1 and num2. Type depends on operands:
-
Integer if both operands are integers and result fits in integer range
-
Real otherwise
-
5. Examples
5.1. Basic Addition
% Integer addition
3 4 add % → 7 (integer result)
% Real addition
9.9 1.1 add % → 11.0 (real result)
% Mixed types
5 2.5 add % → 7.5 (real result)
% Negative numbers
-10 3 add % → -7 (integer result)
5.2. Type Coercion
% Both integers → integer result
100 200 add % → 300 (integer)
% Integer and real → real result
100 200.0 add % → 300.0 (real)
% Both reals → real result
1.5 2.5 add % → 4.0 (real)
6. Advanced Examples
6.2. Vector Addition
/addVectors { % [x1 y1] [x2 y2] -> [x3 y3]
% Add two 2D vectors
aload pop % x2 y2
3 -1 roll aload pop % y2 x2 x1 y1
3 -1 roll add % y2 x2 y1+x1
3 1 roll add % y1+x1 y2+x2
2 array astore % [x3 y3]
} def
[10 20] [5 15] addVectors % → [15 35]
6.3. Cumulative Sum
% Create array of cumulative sums
/cumulativeSum { % [n1 n2 ... nk] -> [n1 n1+n2 n1+n2+n3 ...]
dup length array % Result array
0 % Accumulator
3 1 roll % acc result input
0 1 2 index length 1 sub {
% i acc result input
2 index 1 index get % Get input[i]
3 index add % Add to accumulator
2 index 3 index 3 -1 roll put % Store in result
3 1 roll % Reorder for next iteration
} for
pop exch pop % Clean up, leave result
} def
[1 2 3 4 5] cumulativeSum % → [1 3 6 10 15]
7. Edge Cases and Common Pitfalls
| Integer overflow results in a real number, not an error. |
7.1. Integer Overflow
% Maximum 32-bit signed integer
2147483647 1 add % → 2147483648.0 (real)
When integers overflow, PostScript automatically promotes the result to a real number.
8. Type Requirements
Both operands must be numeric (integer or real). Other types will cause a typecheck error:
% BAD: Non-numeric operands
(hello) 5 add % ERROR: typecheck
[1 2 3] 10 add % 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 add % ERROR: stackunderflow (need 2 operands) typecheck-
One or both operands are not numbers.
(text) 5 add % ERROR: typecheck undefinedresult-
The result is outside the representable range for real numbers (extremely rare).
12. Performance Considerations
The add operator is a primitive operation that executes in constant time O(1). Performance is excellent even for very large numbers of additions.
For adding many values, consider:
-
Using built-in operators when possible
-
Minimizing type conversions between integer and real
-
Using integer arithmetic when precision allows
13. Best Practices
-
Use integer arithmetic when possible for better precision and performance
-
Be aware of type coercion - mixing integers and reals produces reals
-
Document expected types in procedures for clarity
-
Handle overflow gracefully in critical applications
14. See Also
-
Arithmetic and Math - All arithmetic operators