ILE C/C++ Programmer's Guide

_DecimalT Class Template Run-Time Exceptions

In ILE C, a packed decimal is implemented as a native data type. This approach allows an error, such as a decimal format that is not valid, to be detected at compile time. In ILE C++, detection of a similar error is deferred until run time.

Figure 307. _DecimalT Class Template Run-Time Exceptions



#include <bcd.h>
void main()
{
_DecimalT<10,20> b = __D("ABC"); // Run-time exception is raised

}



#include <bcd.h>
void main()
{
_DecimalT<33,2> a; // Max. dig. allow is 31. Again,
// run-time exception is raised
}

When Run-Time Exceptions Occur

Run-time exceptions may occur during the following operations:

Note:
Overflow situations that occur during compilation are deferred until runtime; loss of digits may occur.

Run-Time Exceptions Issued by the Compiler for _DecimalT Class Templates

The run-time exceptions issued by the compiler for _DecimalT class templates are shown in the following figure.

Figure 308. Run-Time Exceptions Issued by the Compiler for _DecimalT Class Templates


#include <bcd.h>
 
static _DecimalT<5,2>   s1 =   __D("12345678.0");
 
static _DecimalT<10,2>  s2 =   __D("1234567891234567891234567.12345")
                                               + __D("12345.1234567891");
 
                                           // s2 = (31,5) + (15,10)
 
static _DecimalT<10,2>  s3 =  __D("1234567891234562345")
                                               * __D("1234567891234.12");
 
                                           // s3 = (19,0) * (15,2)
 
static _DecimalT<10,2>  s4 =  __D("12345678912345678912") /
                                            __D("12345.123456789123456");
 
                                           // s4 = (20,0) / (20,15)
 
int main(void)
 
{
    _DecimalT<5,2> a1 = __D("12345678.0");
 
    _DecimalT<10,2> a2, a3, a4;
 
    _DecimalT<5,2> a5 = (_DecimalT<5,2>) __D("123456.78");
 
 
 
 
    a2 = __D("1234567891234567891234567.12345") + __D("12345.1234567891");
 
                                           // a2 = (31,5) + (15,10)
 
    a3 = __D("123456789123456.12345") * __D("1234567891234.12");
 
                                           // a3 = (20,5) * (15,2)
                                           // expression.
                                           // Note: Need (35,7) but
                                           // use (31,2), for example,
                                           // keep the integral part.
 
    a4 = __D("12345678912345678912") / __D("12345.123456789123456");
 
                                           // a4 = (20,0) / (20,15)
                                           // Note: Need 35 digits to
                                           // calculate integral part
                                           // and the result becomes
                                           // (31,0).
}
 

Notes:

  1. For assignments to a target field too small to hold the _DecimalT class template object, there is a run-time exception issued for static, external, or automatic initialization.

  2. For expressions requiring decimal-point alignment, namely addition, subtraction, or comparison, there is a run-time exception issued for static, external, and automatic initialization if, during alignment, the maximum number of allowed digits is exceeded.

  3. For multiplication, if the evaluation of the expression results in a value larger than the maximum number of allowed digits (DEC_DIG):

  4. For division, a run-time exception is generated when ((n[1]- p[1]) + p[2]) >31.


[ Top of Page | Previous Page | Next Page | Table of Contents ]