General Arithmetic Operation Rules for DECFLOAT
Certain general rules apply to all arithmetic operations on the DECFLOAT data type.
The following general rules apply to all arithmetic operations on the DECFLOAT data type:
- Every operation on finite numbers is carried out as though an
exact mathematical result is computed, using integer arithmetic on
the coefficient where possible.
If the coefficient of the theoretical exact result has no more than the number of digits that reflect its precision (16 or 34), it is used for the result without change (unless there is an underflow or overflow condition). If the coefficient has more than the number of digits that reflect its precision, it is rounded to exactly the number of digits that reflect its precision (16 or 34), and the exponent is increased by the number of digits that are removed.
For static SQL statements other than CREATE VIEW, the ROUNDING bind option or the native SQL procedure option determines the rounding mode.
For dynamic SQL statements (and static CREATE VIEW statements), the special register CURRENT DECFLOAT ROUNDING MODE determines the rounding mode.
If the value of the adjusted exponent of the result is less than Emin, an exception condition is returned. In this case, the calculated coefficient and exponent form the result, unless the value of the exponent is less than Etiny, in which case the exponent is set to Etiny, the coefficient is rounded (possibly to zero) to match the adjustment of the exponent, and the sign is unchanged. If this rounding gives an inexact result, an underflow exception condition is returned.
If the value of the adjusted exponent of the result is larger than Emax, an overflow exception condition is returned. In this case, the result is as defined as an overflow exception condition and might be infinite. It will have the same sign as the theoretical result.
- Arithmetic that uses the special value infinity follows the usual
rules, where negative infinity is less than every finite number and
positive infinity is greater than every finite number.
Under these rules, an infinite result is always exact. Certain uses of infinity return an invalid operation condition. The following list is a list of operations that can cause an invalid operation condition and the result of the operation is NaN when one of the operands is infinity but the other operand is not NaN nor sNaN.
- Add +infinity to -infinity during an addition or subtraction operation
- Multiply 0 by +infinity or -infinity
- Divide either +infinity or -infinity by either +infinity or -infinity
The first argument to the MOD function is either +infinity or -infinity
- Either argument of the QUANTIZE function is +infinity or -infinity
- The second argument of the POWER® function is +infinity or -infinity
A NaN when used as an operand to an arithmetic operation
The following arithmetic rules apply to arithmetic operations and the NaN value:
The result of any arithmetic operation that has an operand that is a NaN (a quiet NaN or signaling NaN) is NaN. The sign of the result is copied from the first operand that is a signaling NaN, or if neither operand is signaling, the sign is copied from the first operand that is a NaN. Whenever a result is a NaN, the sign of the result depends only on the copied operand.
- The sign of the result of a multiplication or division will be negative only if the operands have different signs and neither is a NaN.
- The sign of the result of an addition or subtraction will be negative
only if the result is less than zero and neither operand is a NaN,
except for the following cases where the result is a negative 0:
- A result is rounded to zero, and the value, before rounding, had a negative sign
- Subtract 0 from -0
- Addition of operands with opposite signs (or subtraction of operands with the same sign), the result has a coefficient of 0, and the rounding mode is ROUND_FLOOR
- Multiplication or division and the result has a coefficient of 0 and the signs of the operands are different
- The first argument of the POWER function is -0, and the second argument is a positive odd number
- The argument of the CEIL, FLOOR, or SQRT function is -0
- The first argument of the ROUND or TRUNCATE function is -0

INFINITY + 1 = INFINITY
INFINITY + INFINITY = INFINITY
INFINITY + -INFINITY = NAN -- exception
NAN + 1 = NAN
NAN + INFINITY = NAN
1 - INFINITY = -INFINITY
INFINITY - INFINITY = NAN -- exception
-INFINITY - -INFINITY = NAN -- exception
-0.0 - 0.0E1 = -0.0
-1.0 * 0.0E1 = -0.0
1.0E1 / 0 = INFINITY
-1.0E5 / 0.0 = -INFINITY
1.0E5 / -0 = -INFINITY
INFINITY / -INFINITY = NAN -- exception
INFINITY / 0 = INFINITY
-INFINITY / 0 = -INFINITY
-INFINITY / -0 = INFINITY