# Decimal division

The rules for a specific decimal division depend on whether the DEC31 option is in effect for the operation, whether p is greater than 15, and whether p' is greater than 15

The following table shows how the precision and scale of the result depend on these factors. In that table, the occurrence of "N/A" in a row implies that the indicated factor is not relevant to the case represented by the row.

Table 1. Precision (p) and scale (s) of the result of a decimal division
DEC31 p p' P S
Not in effect ≤15 ≤15 15 15-(p-s+s')
In effect ≤15 ≤15 31 N-(p-s+s'), where
• N is 30-p' if p' is odd.
• N is 29-p' if p' is even.
N/A >15 ≤15 31 N-(p-s+s'), where
• N is 30-p' if p' is odd.
• N is 29-p' if p' is even.
N/A N/A >15 31
15-(p-s+x), where
x is MAX(0,s'-(p'-15))
(See the following note)
Notes on decimal division: If p' is greater than 15, the division is performed using a temporary copy of the divisor. If more than 15 significant digits are needed for the integral part of the divisor, the statement's execution is ended, and an error occurs. Otherwise, the copy is converted to a number with precision 15, by truncating the copy on the right. The truncated copy has a scale of MAX(0,s'-(p'-15)), which is the formula for x. If, in the process of truncation, one or more nonzero digits are removed, SQLWARN7 in SQLCA is set to W, indicating loss of precision. Minimum divide result scale
If the calculated value of 's' is negative, an error occurs. If a minimum divide result scale is specified, this error does not occur.

The minimum scale is determined according to the following precedence:

Static SQL
1. The precompiler DEC option, if it is set with a non-zero scale.
2. The DECIMAL ARITHMETIC field (DECARTH) on installation panel DSNTIP4, if it is set with a non-zero scale.
3. The MINIMUM DIVIDE SCALE opaque subsystem parameter (MINDVSCL), if it is set to a value other than NONE.
4. The MINIMUM DIVIDE SCALE field (DECDIV3) on installation panel DSNTIP4, if it is set to YES.
Dynamic SQL
1. The CURRENT PRECISION special register, if it is set with a non-zero scale.
2. Either of the following cases:
• For a package that was bound with DYNAMICRULES RUN or if the USE FOR DYNAMICRULES field (DYNRULS) on installation panel DSNTIP4 is set to YES: The DECIMAL ARITHMETIC field (DECARTH) on installation panel DSNTIP4, if it is set with a non-zero scale.
• For all other cases: The precompiler DEC option, if it is set with a non-zero scale.
3. The MINIMUM DIVIDE SCALE opaque subsystem parameter (MINDVSCL), if it is set to value other than NONE.
4. The MINIMUM DIVIDE SCALE field (DECDIV3) on installation panel DSNTIP4, if it is set to YES
SQL statements that are executed using SPUFI
The value in DECIMAL ARITHMETIC (DECARTH).

The default value for both the precompiler DEC option and the CURRENT PRECISION special register is DECIMAL ARITHMETIC.

A minimum divide result scale of 3 can be specified using the MINIMUM DIVIDE SCALE field on the installation panel DSNTIP4. A minimum divide scale result between 1 and 9 can be specified using the DECIMAL ARITHMETIC OPTION of the form 'Dpp.s' where 'pp' is 15 or 31 and represents the precision and 's' represents the minimum divide scale, as a number between 1 and 9. Such a specification overrides the MINIMUM DIVIDE SCALE. When a minimum divide result scale is specified, the formula MAX(s,s'), where s represents the scale derived from the above table and s' represents the value specified by the minimum divide result scale, is applied and a new scale is derived. The newly derived scale is the scale of the result and overrides any scale derived using the table above. 