PostScript provides a comprehensive set of arithmetic and mathematical operators for numerical computation. These operators work with both integer and real (floating-point) numbers, with automatic type promotion where appropriate.
When integer operations exceed the maximum integer value, the result becomes a real:
21474836471add% → 2147483648.0 (real)
1.5. Best Practices
1.5.1. Use Appropriate Division Operator
Use div for mathematical division requiring precise real results
Use idiv when you need integer quotients (truncated toward zero)
Use mod to get the remainder
1.5.2. Check Domain Before Operations
For operations with restricted domains, validate input:
/safesqrt{% num -> real or errordup0lt{pop(sqrt: negative argument)print0}{sqrt}ifelse}def
1.5.3. Be Aware of Precision
Real numbers have finite precision. Avoid comparing reals for exact equality:
% Bad:0.10.2add0.3eq% May be false due to rounding% Better:/nearlyequal{% num1 num2 tolerance -> bool31rollsubabsexchlt}def0.10.2add0.30.0001nearlyequal% true