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.
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 under 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.
The % (integer divide) operator divides two numbers and returns the integer part of the result. The result is calculated by repeatedly subtracting the divisor from the dividend as long as the dividend is larger than the divisor. During this subtraction, the absolute values of both the dividend and the divisor are used: the sign of the final result is the same as that which would result from regular division.
If the result cannot be expressed as a whole number, the operation is in error and fails--that is, the result must not have more digits than the current setting of NUMERIC DIGITS. For example, 10000000000%3 requires 10 digits for the result (3333333333) and would, therefore, fail if NUMERIC DIGITS 9 were in effect.
The // (remainder) operator returns the remainder from an integer division and is defined to be the residue of the dividend after integer division. The sign of the remainder, if nonzero, is the same as that of the original dividend.
This operation fails under the same conditions as integer division, that is, if integer division on the same two terms fails, the remainder cannot be calculated.
/* With: NUMERIC DIGITS 5 */ 12+7.00 -> 19.00 1.3-1.07 -> 0.23 1.3-2.07 -> -0.77 1.20*3 -> 3.60 7*3 -> 21 0.9*0.8 -> 0.72 1/3 -> 0.33333 2/3 -> 0.66667 5/2 -> 2.5 1/10 -> 0.1 12/12 -> 1 8.0/2 -> 4 2**3 -> 8 2**-3 -> 0.125 1.7**8 -> 69.758 2%3 -> 0 2.1//3 -> 2.1 10%3 -> 3 10//3 -> 1 -10//3 -> -1 10.2//1 -> 0.2 10//0.3 -> 0.1 3.6//1.3 -> 1.0