Runtime Exceptions Issued by the Compiler for _DecimalT Class Templates
The runtime exceptions issued by the compiler for _DecimalT class
templates are shown in the following figure.
#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).
}
Note:
- For assignments to a target field too small to hold the
_DecimalTclass template object, there is a runtime exception issued for static, external, or automatic initialization. - For expressions requiring decimal-point alignment, namely addition, subtraction, or comparison, there is a runtime exception issued for static, external, and automatic initialization if, during alignment, the maximum number of allowed digits is exceeded.
- For multiplication, if the evaluation of the expression results
in a value larger than the maximum number of allowed digits
(DEC_DIG):- A runtime exception is issued either for static or external initialization, or if the expression is within a function.
- Truncation occurs on the fractional part, preserving as much of the integral part as possible.
- For division, a runtime exception is generated when
((n[1]- p[1]) + p[2]) >31.