1. cvi
Converts a number or string to an integer.
1.2. Stack Effects
| Level | Object |
|---|---|
0 |
|
| Level | Object |
|---|---|
0 |
|
1.3. Description
cvi (convert to integer) converts an integer, real, or string to an integer result:
-
Integer operand: Returns it unchanged
-
Real operand: Truncates fractional part (rounds toward 0) and converts to integer
-
String operand: Parses as PostScript number syntax, then converts to integer if needed
A [rangecheck] error occurs if a real is too large to convert to an integer.
1.5. Examples
(3.3E1) cvi % Result: 33
(123) cvi % Result: 123
(-456) cvi % Result: -456
-47.8 cvi % Result: -47
520.9 cvi % Result: 520
3.14159 cvi % Result: 3
42 cvi % Result: 42 (unchanged)
1.7. Common Pitfalls
Truncation, Not Rounding - cvi truncates toward zero, not to nearest integer.
|
3.9 cvi % Result: 3 (not 4)
-3.9 cvi % Result: -3 (not -4)
| Range Limits - Very large reals cannot be converted. |
1.0e100 cvi % Error: rangecheck
| String Syntax - String must contain valid PostScript number syntax. |
(abc) cvi % Error: syntaxerror
(12x) cvi % Error: syntaxerror
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
String has no-access attribute |
[ |
Real too large for integer conversion |
[ |
No operand on stack |
[ |
String not valid number syntax |
[ |
Operand not number or string |
[ |
String parses to invalid number |
1.9. Implementation Notes
-
Truncation performed before conversion
-
Integer range typically ±2³¹-1 (32-bit implementations)
-
String parsing follows full PostScript syntax (including radix notation)
-
Same conversion as implicit coercion in many operators