Product SiteDocumentation Site

10.2. Arithmetic Operators

Rexx arithmetic is performed by the operators +, -, *, /, %, //, and ** (add, subtract, multiply, divide, integer divide, remainder, and power).
Before every arithmetic operation, the terms operated upon have leading zeros removed (noting the position of any decimal point, and leaving only one zero if all the digits in the number are zeros). They are then truncated, if necessary, to DIGITS + 1 significant digits before being used in the computation. The extra digit improves accuracy because it is inspected at the end of an operation, when a number is rounded to the required precision. When a number is truncated, the LOSTDIGITS condition is raised if a SIGNAL ON LOSTDIGITS condition trap is active. The operation is then carried out under up to double that precision. When the operation is completed, the result is rounded, if necessary, to the precision specified by the NUMERIC DIGITS instruction.
The values are rounded as follows: 5 through 9 are rounded up, and 0 through 4 are rounded down.

10.2.1. Power

The ** (power) operator raises a number to a power, which can be positive, negative, or 0. The power must be a whole number. The second term in the operation must be a whole number and is rounded to DIGITS digits, if necessary, as described in Section 10.5, “Limits and Errors when Rexx Uses Numbers Directly”. If negative, the absolute value of the power is used, and the result is inverted (that is, the number 1 is divided by the result). For calculating the power, the number is multiplied by itself for the number of times expressed by the power. Trailing zeros are then removed as though the result were divided by 1.