Results of arithmetic operations

After any necessary conversion of the operands in an expression has been carried out, the arithmetic operation is performed and a result is obtained. This result can be the value of the expression, or it can be an intermediate result upon which further operations are to be performed, or a condition can be raised.

Table 1 and Table 2 show the attributes and precisions that result from various arithmetic operations.

Table 1 shows the attributes of the result for the special cases of exponentiation noted in the right-hand columns of Table 1 and Table 2.

In an add or subtract of FIXED(p1,q1) and FIXED(p2,q2), ABS(q1-q2) must be less than or equal to N for DECIMAL and M for BINARY.

In a multiply or divide of FIXED(p1,q1) and FIXED(p2,q2), the resulting scale factor must be between 0 and 127.

If both operands in an addition, multiplication or division are UNSIGNED FIXED BIN, then the result also has the UNSIGNED attribute.

On the z/OS platform, the choice of which set of instructions is used for a float calculation is determined by two compiler options:

  • Under FLOAT(DFP)
    • All computations that would yield a FLOAT DEC result are done using the IEEE decimal floating-point instructions.
    • All computations that would yield a FLOAT BIN result are done using the floating-point instructions for the format specified by the HEXADEC and IEEE suboptions of the DEFAULT compiler option.
  • Under FLOAT(NODFP)
    • All computations that would yield a FLOAT result are done using the floating-point instructions for the format specified by the HEXADEC and IEEE suboptions of the DEFAULT compiler option.

So, under the FLOAT(NODFP) and DEFAULT(HEXADEC) options, all computations are done using the hexadecimal floating-point instructions, and variables declared IEEE will be converted to HEXADEC. But, under the FLOAT(NODFP) and DEFAULT(IEEE) options, all computations are done using the IEEE binary floating-point instructions, and variables declared HEXADEC will be converted to IEEE as necessary.

On all other platforms, float calculations are done using the IEEE binary floating-point instructions native to that platform.

Under the compiler option RULES(ANS), if one operand is scaled FIXED DECIMAL and the other is FIXED BINARY, the FIXED BINARY value is converted to FIXED DECIMAL. Table 3 shows the attributes and precisions that result for this case under compiler option RULES(ANS). For more information about the RULES compiler option, see the Programming Guide.

Table 1. Results of arithmetic operations for one or more FLOAT operands
1st Operand
(p1,q1)
2nd Operand
(p2,q2)
Attributes of
the Result
for Addition,
Subtraction,
Multiplication,
or Division
Addition
or
Subtraction
Precision
Multiplication
Precision
Division
Precision
Attributes
of the
Result for
Exponentiation
FLOAT
DECIMAL
(p1)
FLOAT
DECIMAL
(p2)
FLOAT
DECIMAL
(p)
p = MAX(p1,p2)
FLOAT
DECIMAL (p)
(unless special case
C applies)
p = MAX(p1,p2)
FLOAT
DECIMAL
(p1)
FIXED
DECIMAL
(p2,q2)
FIXED
DECIMAL
(p1,q1)
FLOAT
DECIMAL
(p2)
FLOAT
BINARY
(p1)
FLOAT
BINARY
(p2)
FLOAT
BINARY
(p)
FLOAT
BINARY (p)
(unless special case
C applies)
p = MAX(p1,p2)
FLOAT
BINARY
(p1)
FIXED
BINARY
(p2,q2)
FIXED
BINARY
(p1,q1)
FLOAT
BINARY
(p2)
FIXED
DECIMAL
(p1,q1)
FLOAT
BINARY
(p2)
FLOAT
BINARY
(p)
p = MAX(
CEIL(p1*3.32),p2)
FLOAT
BINARY (p)
(unless special case
A or C applies)
p = MAX(
CEIL(p1*3.32),p2)
FLOAT
DECIMAL
(p1)
FIXED
BIN
(p2,q2)
FLOAT
DECIMAL
(p1)
FLOAT
BINARY
(p2)
FIXED
BINARY
(p1,q1)
FLOAT
DECIMAL
(p2)
FLOAT
BINARY
(p)
p = MAX(p1,CEIL(p2*3.32))
FLOAT
BINARY (p)
(unless special case
B or C applies)
p = MAX(
p1,CEIL(p2*3.32))
FLOAT
BINARY
(p1)
FIXED
DECIMAL
(p2,q2)
FLOAT
BINARY
(p1)
FLOAT
DECIMAL
(p2)
Notes:
  1. Special cases of exponentiation are described in Table 1.
  2. For a table of CEIL(N*3.32) values, see Table 1.
Table 2. Results of arithmetic operations between two unscaled FIXED operands under RULES(ANS)
1st Operand
(p1,q1)
2nd Operand
(p2,q2)
Attributes of
the Result for
Addition,
Subtraction,
Multiplication,
or Division
Addition
or
Subtraction
Precision
Multiplication
Precision
Division
Precision
Attributes
of the
Result for
Exponentiation
FIXED
DECIMAL
(p1,0)
FIXED
DECIMAL
(p2,0)
FIXED
DECIMAL
(p,q)
p = 1
+MAX(p1,p2)
q = 0
p = 1
+p1+p2
q = 0
p = N
q = N-p1
FLOAT DECIMAL (p)
(unless special case
A applies)
p = MAX(p1,p2)
FIXED
BINARY
(p1,0)
FIXED
BINARY
(p2,0)
FIXED
BINARY
(p,0)
p = 1
+MAX(p1-q1,
p2-q2) +q
q = 0
p = 1+p1
    +p2
q = 0
p = M
q = 0
FLOAT BINARY (p)
(unless special case
B applies)
p = MAX(p1,p2)
FIXED
DECIMAL
(p1,0)
FIXED
BINARY
(p2,0)
FIXED
BINARY
(p,0)
p = 1
+MAX(r,p2)
q = 0
p = 1
+r+p2
q = 0
p = M
q = 0
FLOAT BINARY (p)
(unless special case
A applies)
p = MAX(CEIL
(p1*3.32 ),p2)
FIXED
BINARY
(p1,0)
FIXED
DECIMAL
(p2,0)
FIXED
BINARY
(p,0)
p = 1
+MAX(p1,t)
q = 0
p = 1
+p1+t
q = 0
p = M
q = 0
FLOAT BINARY (p)
(unless special case
B applies)
p = MAX(CEIL
(p1*3.32 ),p2)
M is the maximum precision for FIXED BINARY.
N is the maximum precision for FIXED DECIMAL.
r = 1 + CEIL(p1*3.32)
s = CEIL(ABS(q1*3.32)) * SIGN(q1)
t = 1 + CEIL(p2*3.32)
u = CEIL(ABS(q2*3.32)) * SIGN(q2)
v = CEIL(p2/3.32)
w = CEIL(p1/3.32)
Notes:

The scaling factor must be in the range 0 through +127.

  1. Special cases of exponentiation are described in Table 1.
  2. For a table of CEIL(N*3.32) values, see Table 1.
  3. Under RULES(ANS) a divide with unscaled FIXED operands can produce a scaled result only if both operands are FIXED DECIMAL.
Table 3. Results of arithmetic operations between two scaled FIXED operands under RULES(ANS)
1st Operand
(p1,q1)
2nd Operand
(p2,q2)
Attributes of
the Result for
Addition,
Subtraction,
Multiplication,
or Division
Addition
or
Subtraction
Precision
Multiplication
Precision
Division
Precision
Attributes
of the
Result for
Exponentiation
FIXED
DECIMAL
(p1,q1)
FIXED
DECIMAL
(p2,q2)
FIXED
DECIMAL
(p,q)
p = 1 +
MAX(p1-q1,
p2-q2) +q
q =
MAX(q1,q2)
p = 1
+p1+p2
q =
q1+q2
p = N
q =
N-p1+q1-q2
FLOAT DECIMAL (p)
(unless special case
A applies)
p = MAX(p1,p2)
FIXED
DECIMAL
(p1,q1)
FIXED
BINARY
(p2,0)
FIXED
DECIMAL
(p,q)
p = 1
+MAX(p1-
q1,v) +q
q = q1
p = 1
+p2+v
q = q1
p = N
q = N-p1+q1
FLOAT BINARY (p)
(unless special case
A applies)
p = MAX(CEIL
(p1*3.32 ),p2)
FIXED
BINARY
(p1,0)
FIXED
DECIMAL
(p2,q2)
FIXED
DECIMAL
(p,q)
p = 1
+MAX(p2-
q2,w) +q
q = q2
p = 1
+p2+w
q = q1
p = N
q = N-w-q2
FLOAT BINARY (p)
(unless special case
B applies)
p = MAX(CEIL
(p1*3.32 ),p2)
M is the maximum precision for FIXED BINARY.
N is the maximum precision for FIXED DECIMAL.
r = 1 + CEIL(p1*3.32)
s = CEIL(ABS(q1*3.32)) * SIGN(q1)
t = 1 + CEIL(p2*3.32)
u = CEIL(ABS(q2*3.32)) * SIGN(q2)
v = CEIL(p2/3.32)
w = CEIL(p1/3.32)
Notes:

The scaling factor must be in the range 0 through +127. In addition and subtraction operations, the absolute value of the difference in the scale factors must not be greater than M for FIXED BIN operations and N for FIXED DEC operations.

  1. Special cases of exponentiation are described in Table 1.
  2. For a table of CEIL(N*3.32) values, see Table 1.
  3. Under RULES(ANS), scaled FIXED BINARY is not allowed.
Table 4. Results of arithmetic operations between two FIXED operands under RULES(IBM)
1st Operand
(p1,q1)
2nd Operand
(p2,q2)
Attributes of
the Result for
Addition,
Subtraction,
Multiplication,
or Division
Addition
or
Subtraction
Precision
Multiplication
Precision
Division
Precision
Attributes
of the
Result for
Exponentiation
FIXED
DECIMAL
(p1,q1)
FIXED
DECIMAL
(p2,q2)
FIXED
DECIMAL
(p,q)
p = 1
+MAX(p1-q1,
p2-q2) +q
q =
MAX(q1,q2)
p = 1
+p1+p2
q =
q1+q2
p = N
q =
N-p1+q1-q2
FLOAT DECIMAL (p)
(unless special case
A applies)
p = MAX(p1,p2)
FIXED
BINARY
(p1,q1)
FIXED
BINARY
(p2,q2)
FIXED
BINARY
(p,q)
p = 1
+MAX(p1-q1,
p2-q2) +q
q =
MAX(q1,q2)
p = 1
+p1+p2
q = q1+q2
p = M
q = M-p1
+q1-q2
FLOAT BINARY (p)
(unless special case
B applies)
p = MAX(p1,p2)
FIXED
DECIMAL
(p1,q1)
FIXED
BINARY
(p2,q2)
FIXED
BINARY
(p,q)
p = 1
+MAX(r-s,
p2-q2)+q
q =
MAX(s,q2)
p = 1+r
+p2
q = s+q2
p = M
q = M-r
+s-q2
FLOAT BINARY (p)
(unless special case
A applies)
p =MAX(
CEIL((p1*3.32 ),p2)
FIXED
BINARY
(p1,q1)
FIXED
DECIMAL
(p2,q2)
FIXED
BINARY
(p,q)
p = 1
+MAX(p1-
q1,t-u) +q
q =
MAX(s,q1,u)
p = 1
+p1+t
q = q1+u
p = M
q = M-p1
+q1-u
FLOAT BINARY (p)
(unless special case
B applies)
p = MAX(p1,
CEIL(p2*3.32))
M is the maximum precision for FIXED BINARY.
N is the maximum precision for FIXED DECIMAL.
r = 1 + CEIL(p1*3.32)
s = CEIL(ABS(q1*3.32)) * SIGN(q1)
t = 1 + CEIL(p2*3.32)
u = CEIL(ABS(q2*3.32)) * SIGN(q2)
v = CEIL(p2/3.32)
w = CEIL(p1/3.32)
Notes:

The scaling factor must be in the range 0 through +127. In addition and subtraction operations, the absolute value of the difference in the scale factors must not be greater than M for FIXED BIN operations and N for FIXED DEC operations.

  1. Special cases of exponentiation are described in Table 1.
  2. For a table of CEIL(N*3.32) values, see Table 1.
  3. The bounds of the precision p for FIXED BINARY are determined by the FIXEDBIN suboption and for FIXED DECIMAL by the FIXEDDEC suboption of the LIMITS compiler option. The LIMITS option has no effect on the bounds of the scaling factor q.

Consider the following expression:

  A * B + C

The operation A * B is performed first, to give an intermediate result. Then the value of the expression is obtained by performing the operation (intermediate result) + C.

PL/I gives the intermediate result attributes the same way it gives attributes to any variable. The attributes of the result are derived from the attributes of the two operands (or the single operand in the case of a prefix operation) and the operator involved. The way the attributes of the result are derived is further explained under Targets.

The ADD, SUBTRACT, MULTIPLY, and DIVIDE built-in functions allow you to override the implementation precision rules for addition, subtraction, multiplication, and division operations.