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.

2. Syntax

num1 neg num2

2.1. Stack Effect

Table 1. Before Execution
Position Content

Top

num1 (integer or real) - Number to negate

Table 2. After Execution
Position Content

Top

num2 (integer or real) - Negation of num1

3. Parameters

num1

Any number (integer or real), positive, negative, or zero

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

5.2. Double Negation

% Negating twice returns original
5 neg neg      % → 5
-3.5 neg neg   % → -3.5

5.3. Sign Reversal in Calculations

% Reverse direction of a vector
/reverseVector {  % [x y] -> [-x -y]
    aload pop      % x y
    neg exch neg exch
    2 array astore
} def

[10 20] reverseVector  % → [-10 -20]

5.4. Subtraction Alternative

% These are equivalent:
10 5 sub       % → 5

5 neg 10 add   % → 5 (add negative is same as subtract)

% Sometimes clearer to add negative:
/total 100 def
/deduction 25 def
total deduction neg add  % "Add negative deduction"

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

6.3. Debt Representation

% Convert between credit/debit representation
/creditToDebit {  % amount -> debit
    neg
} def

/debitToCredit {  % debit -> credit
    neg
} def

100 creditToDebit  % → -100 (represent as debt)
-100 debitToCredit % → 100 (back to credit)

6.4. Flip Sign Conditionally

% Make negative if condition is true
/negIf {  % num bool -> num
    {
        neg
    } if
} def

5 true negIf   % → -5
5 false negIf  % → 5
-3 true negIf  % → 3 (double negation)

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)

7.3. Type Preservation

% Type is preserved
-5 neg         % → 5 (integer)
-5.0 neg       % → 5.0 (real)

% Unless overflow
-2147483648 neg % → 2147483648.0 (real)

7.4. Not the Same as Subtraction from Zero

% These are equivalent for most values
5 neg          % → -5
0 5 sub        % → -5

% But subtraction requires two operands
% neg is more efficient and clearer

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
  • abs - Absolute value

  • sub - Subtract two numbers

  • add - Add two numbers

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

  1. Use neg instead of subtracting from zero - clearer and more efficient

  2. Be aware of type preservation - maintains integer/real type

  3. Remember special case for most negative integer

  4. 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

13.2. Combine with Conditions

% Make value negative if condition
/negativeIf {  % num bool -> num
    {
        dup 0 gt { neg } if
    } if
} def

% Ensure value is negative
/ensureNegative {  % num -> negNum
    dup 0 gt { neg } if
} def

10 ensureNegative   % → -10
-5 ensureNegative   % → -5

14. See Also


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.