all bits have now been inspected, the initial calculation is complete; otherwise the accumulator is squared
and the next bit is inspected for multiplication. When the initial calculation is complete, the temporary result
is divided into 1 if the power was negative.
The multiplications and division are done under the arithmetic operation rules, using a precision of DIGITS
+ L + 1 digits. L is the length in digits of the integer part of the whole number n (that is, excluding any
decimal part, as though the built-in function TRUNC(n) had been used). Finally, the result is rounded to
NUMERIC DIGITS digits, if necessary, and insignificant trailing zeros are removed.
Integer Division
The % (integer divide) operator divides two numbers and returns the integer part of the result. The result
returned is defined to be that which would result from repeatedly subtracting the divisor from the dividend
while 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.
The result returned has no fractional part (that is, no decimal point or zeros following it). If the result
cannot be expressed as a whole number, the operation is in error and will fail—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. Note that
this operator may not give the same result as truncating regular division (which could be affected by
rounding).
Remainder
The // (remainder) operator returns the remainder from integer division and is defined as being the
residue of the dividend after the operation of calculating integer division as previously described. 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 would fail, the remainder cannot be calculated).
Additional Operator Examples
Following are some examples using the power, integer divide, and remainder operators:
/* Again with: Numeric digits 5 */
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
Notes:
1. A particular algorithm for calculating powers is used, because it is efficient (though not optimal) and
considerably reduces the number of actual multiplications performed. It, therefore, gives better
performance than the simpler definition of repeated multiplication. Because results may differ from
those of repeated multiplication, the algorithm is defined here.
2. The integer divide and remainder operators are defined so that they can be calculated as a by-product
of the standard division operation. The division process is ended as soon as the integer result is
available; the residue of the dividend is the remainder.
Numbers and Arithmetic
Chapter 16. Numbers and Arithmetic 221