Returns the negative (opposite sign) of a number.
1. Description
The neg operator pops a number from the operand stack and pushes its negation back onto the stack. The type of the result is the same as the type of the operand, unless the operand is the most negative integer (-2147483648), in which case the result is a real.
This is a Level 1 operator, available in all PostScript implementations.
4. Return Values
num2-
The negation of num1 (changes sign):
-
Same type as num1 (integer or real)
-
Exception: Most negative integer returns real
-
5. Examples
5.1. Basic Negation
% Negate positive
5 neg % → -5
4.5 neg % → -4.5
% Negate negative (makes positive)
-3 neg % → 3
-7.2 neg % → 7.2
% Zero unchanged (but may preserve sign for reals)
0 neg % → 0
-0.0 neg % → 0.0 or -0.0
6. Advanced Examples
6.1. Opposite Direction
% Calculate opposite angle
/oppositeAngle { % angle -> oppositeAngle
180 add % Add 180 degrees
360 mod % Normalize to 0-360
} def
45 oppositeAngle % → 225
6.2. Mirror Coordinate
% Mirror point across axis
/mirrorX { % x y -> x -y
neg
} def
/mirrorY { % x y -> -x y
exch neg exch
} def
10 20 mirrorX % → 10 -20
10 20 mirrorY % → -10 20
7. Edge Cases and Common Pitfalls
| Most negative integer (-2147483648) returns a real when negated. |
7.1. Most Negative Integer
% Special case: most negative 32-bit integer
-2147483648 neg % → 2147483648.0 (real!)
% Positive equivalent exceeds integer range
% All other integers preserve type
-2147483647 neg % → 2147483647 (still integer)
2147483647 neg % → -2147483647 (still integer)
7.2. Sign of Zero
% Integer zero has no sign
0 neg % → 0 (same as 0)
% Real zero may preserve signed zero (IEEE 754)
0.0 neg % → -0.0 (implementation dependent)
-0.0 neg % → 0.0 (implementation dependent)
8. Type Requirements
The operand must be numeric (integer or real). Other types will cause a typecheck error:
% BAD: Non-numeric operands
(hello) neg % ERROR: typecheck
[1 2] neg % ERROR: typecheck
true neg % 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.
neg % ERROR: stackunderflow (need 1 operand) typecheck-
The operand is not a number.
(text) neg % ERROR: typecheck
12. Performance Considerations
The neg operator is extremely fast:
-
Simple sign bit flip or subtraction from zero
-
O(1) constant time complexity
-
No overhead compared to manual subtraction
13. Best Practices
-
Use
neginstead of subtracting from zero - clearer and more efficient -
Be aware of type preservation - maintains integer/real type
-
Remember special case for most negative integer
-
Useful for sign reversal in geometric operations
13.1. Idiomatic Usage
% GOOD: Clear intent
velocity neg % Reverse direction
% LESS CLEAR: Equivalent but obscure
0 velocity sub % Same result, less clear intent
% Sign changes
/x 10 def
x neg /x exch def % x is now -10
% Double negation for positive
value neg neg % Same as abs for negative, but keeps positive positive
14. See Also
-
Arithmetic and Math - All arithmetic operators