Previous Topic Next topic Print topic


Arithmetic to Arithmetic Conversion

Arithmetic to arithmetic conversion assigns any arithmetic data type to any other arithmetic data type. This type of conversion occurs most frequently as a result of using operands of differing arithmetic types in an arithmetic or relational operation. In all cases, the target base and scale are supplied by the conversion rules of the operator or built-in function, but the target precision is not supplied.

If a target precision is not supplied, it is determined from the data type of the source value (listed vertically) and the target base and scale (listed horizontally), as shown in the table below.

Target Base And Scale, Precision r[,s] Source Base And Scale, Precision p[,q]
Fixed Binary Fixed Decimal Float Binary Float Decimal
Fixed Binary r = p r = MIN(CEIL (p*3.32)+1,n) * *
Fixed Decimal r= MIN(CEIL(p/3.32)+1, n) r= p * *
Float Binary s=0

r =MIN(p,n)

s = q

r= MIN (CEIL(p*3.32),n)

r= p r=MIN(CEIL(p*3.32),n)
Float Decimal r=MIN (CEIL(p/3.32),n) r=MIN(p,n) r = MIN (CEIL(p/3.32),n) r= p
* These conversions arise only in cases where the target precision is explicitly known, as in the case of assignment to a fixed-point variable.
n The maximum number of digits allowed by Open PL/I for the target base and scale. For precision limits, see your Open PL/I User's Guide.

Conversion of a fixed-point decimal value containing a fraction to floating-point or vice versa produces an approximate value. The result is the number in the target scale and precision that is closest to the source value and is produced by rounding at the last binary or decimal digit position.

Conversion of a fixed-point decimal value containing a fraction to a fixed-point value with no fraction or fewer fractional digits results in truncation of excess digits without rounding. Rounding can be performed by using the ROUND, CEIL, FLOOR, or TRUNC built-in functions, as described in the chapter Open PL/I Built-ins.

If the target precision does not provide sufficient digits to hold all integral digits of the converted value, the program is in error. This error is not detected by Open PL/I and may or may not be detected by another implementation of PL/I. If detected, the ERROR condition is signaled. If undetected, the program produces unpredictable results. Programs that contain this undetected error and produce "correct" results may fail when moved to another implementation of PL/I.

When fixed binary (63) support is enabled, arithmetic operations with fixed binary operands (p <= 31) will result in a maximum intermediate precision of p=31. For operands (32 >= p <= 63) the result precision will be promoted according to result precision rules with maximum fixed binary precision p = 63. See the User Guide for the –fbmaxp 63 option to enable fixed binary (63) support.

The following shows the target precisions given, in the table above, for some common source values and target data types. Note that for some implementations, the target precision may be limited to a value smaller than what is shown here.

Source Target
Fixed Binary(15) Fixed Decimal(6,0)
Fixed Binary(31) Fixed Decimal(11,0)
Fixed Binary(63) Fixed Decimal(19,0)
Float Binary(21) Float Decimal(7)
Float Binary(52) Float Decimal(16)
Fixed Decimal(4) Fixed Binary(15)
Fixed Decimal(7) Fixed Binary(25)
Float Decimal(6) Float Binary(20)
Float Decimal(15)      Float Binary(50)
Previous Topic Next topic Print topic