Returns the least integer value greater than or equal to a number.
1. Description
The ceiling operator pops a number from the operand stack and pushes the smallest integer that is greater than or equal to that number. The type of the result is the same as the type of the operand - if the input is an integer, it returns an integer; if the input is a real, it returns a real with no fractional part.
This is a Level 1 operator, available in all PostScript implementations.
4. Return Values
num2-
The ceiling of num1 (type matches num1):
-
For integers: returns the same value
-
For reals: returns real with no fractional part, rounded up
-
5. Examples
5.1. Basic Usage
% Positive reals round up
3.2 ceiling % → 4.0
3.9 ceiling % → 4.0
4.0 ceiling % → 4.0 (already integer value)
% Negative reals round toward zero (less negative)
-4.8 ceiling % → -4.0 (rounds toward zero)
-4.1 ceiling % → -4.0
-4.0 ceiling % → -4.0
% Integers unchanged
99 ceiling % → 99
-50 ceiling % → -50
5.2. Rounding Behavior
% ceiling always rounds up (toward positive infinity)
0.1 ceiling % → 1.0
0.9 ceiling % → 1.0
1.0 ceiling % → 1.0
% For negative numbers, "up" means toward zero
-0.1 ceiling % → -0.0 (or 0.0)
-0.9 ceiling % → -0.0 (or 0.0)
-1.0 ceiling % → -1.0
-1.1 ceiling % → -1.0 (rounded toward zero/up)
6. Advanced Examples
6.1. Round Up to Multiple
% Round up to nearest multiple
/roundUpToMultiple { % value multiple -> rounded
div ceiling
1 index mul
exch pop
} def
23 5.0 roundUpToMultiple % → 25.0 (next multiple of 5)
100 64.0 roundUpToMultiple % → 128.0 (next multiple of 64)
6.2. Buffer Size Calculation
% Calculate buffer size needed
/bufferSize { % dataSize blockSize -> numBlocks
exch dup 0 eq {
pop pop 0
} {
exch div ceiling cvi
} ifelse
} def
1000 256 bufferSize % → 4 (need 4 blocks of 256)
7. Edge Cases and Common Pitfalls
| Type is preserved - real input gives real output. |
7.1. Type Preservation
% Type matches input type
3.5 ceiling % → 4.0 (real)
3 ceiling % → 3 (integer)
% To get integer result from real
3.5 ceiling cvi % → 4 (converted to integer)
7.2. Already an Integer Value
% Integer values unchanged
5.0 ceiling % → 5.0 (no change, but still real)
-3.0 ceiling % → -3.0
% Actual integers
5 ceiling % → 5
7.3. Negative Number Behavior
% ceiling rounds toward positive infinity
% For negative numbers, this means toward zero
-3.2 ceiling % → -3.0 (toward zero, less negative)
-3.9 ceiling % → -3.0 (not -4.0!)
% Compare with floor
-3.2 floor % → -4.0 (away from zero, more negative)
-3.2 ceiling % → -3.0 (toward zero, less negative)
8. Type Requirements
The operand must be numeric (integer or real). Other types will cause a typecheck error:
% BAD: Non-numeric operands
(hello) ceiling % ERROR: typecheck
[1 2] ceiling % ERROR: typecheck
true ceiling % 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 is empty.
ceiling % ERROR: stackunderflow (need 1 operand) typecheck-
The operand is not a number.
(text) ceiling % ERROR: typecheck
12. Performance Considerations
The ceiling operator is fast:
-
Simple comparison and rounding operation
-
O(1) constant time complexity
-
Efficient for allocation calculations
13. Best Practices
-
Use for allocation and capacity calculations
-
Convert with
cviif integer needed -ceilingpreserves type -
Understand negative behavior - rounds toward zero
-
Use for "round up" logic - next page, next block, etc.
13.1. Common Patterns
% Round up for resources
/resourcesNeeded { % demand capacity -> units
div ceiling cvi
} def
% Ensure positive ceiling
/posCeiling { % num -> ceil
dup 0 ge {
ceiling
} {
truncate
} ifelse
} def
% Pages needed (rounds up)
/calculatePages { % items perPage -> pages
div dup
dup truncate eq {
% Exact division
truncate
} {
% Round up
ceiling
} ifelse
cvi
} def
14. See Also
-
Arithmetic and Math - All arithmetic operators