ILE C/C++ Programmer's Guide

Using Operators with the _DecimalT Class Template

You can use the following operators with the _DecimalT class template:

Notes:

  1. Logical operators are not implemented for _DecimalT class templates.

  2. For information on run-time exceptions during assignments, see C++ Packed Decimal Data Conversions.

Using Arithmetic Operators with the _DecimalT Class Template

Figure 290 shows how to:

Figure 290. Example: Arithmetic Operators for the _DecimalT Class Template



#include <bcd.h> // bcd Class Header File
#include <iostream.h>

int main()
{
_DecimalT<10,2> op_1 = __D("12");
_DecimalT<5,5> op_2 = __D("-.12345");
_DecimalT<24,12> op_3 = __D("12.34");
_DecimalT<20,5> op_4 = __D("11.01");

_DecimalT<14,5> res_add;
_DecimalT<25,2> res_sub;
_DecimalT<15,7> res_mul;
_DecimalT<31,14> res_div;

res_add = op_1 + op_2;
res_sub = op_3 - op_1;
res_mul = op_2 * op_1;
res_div = op_3 / op_4;

cout <<"res_add =" <<res_add <<endl;
cout <<"res_sub =" <<res_sub <<endl;
cout <<"res_mul =" <<res_mul <<endl;
cout <<"res_div =" <<res_div <<endl;

}

The output is:

+--------------------------------------------------------------------------------+
|	res_add =11.87655                                                              |
|	res_sub =0.34                                                                  |
|	res_mul =-1.4814000                                                            |
|	res_dev =1.12079927338782                                                      |
+--------------------------------------------------------------------------------+

Using Relational Operators with the _DecimalT Class Template

When you use relational operators with the _DecimalT class template, consider the following:

Figure 291 shows how to use relational operators with the _DecimalT class template.

Figure 291. Example: Relational Operators and the _DecimalT Class Template



#include <bcd.h>
#include <iostream.h>

_DecimalT<10,3> pdval = __D("0000023.423"); // bcd declaration
int ival = 1233.1; // Integer declaration
float fval = 1234.34f; // Float declaration
double dval = 251.5832; // Double declaration
long double lval = 37486.234; // Long double declaration

main( )
{
_DecimalT<15,6> value = __D("000485860.085999");
// Perform relational operation between other data types and
// bcd class

if (pdval < ival) cout <<"pdval is the smallest !"<<endl;
if (pdval < fval) cout <<"pdval is the smallest !"<<endl;
if (pdval < dval) cout <<"pdval is the smallest !"<<endl;
if (pdval < lval) cout <<"pdval is the smallest !"<<endl;
if (pdval < value) cout <<"pdval is the smallest !"<<endl;
}

The output is:

+--------------------------------------------------------------------------------+
|	pdval is the smallest !                                                        |
|	pdval is the smallest !                                                        |
|	pdval is the smallest !                                                        |
|	pdval is the smallest !                                                        |
|	pdval is the smallest !                                                        |
|                                                                                |
+--------------------------------------------------------------------------------+

Using Conditional Expressions with the _DecimalT Class Template

Figure 292 shows how to:

Figure 292. Example: Conditional Operators and the _DecimalT Class Template



#include <bcd.h>
#include <iostream.h>
int main ()
{ _DecimalT<10,2> x, y, z; x = __D("1.20"); y = __D("01.2"); z = (x==y)? __D("9.9"):__D("2.45"); if (z== __D("9.9")) { cout <<"x equals y" <<endl; }
}

Using Equality Operators with the _DecimalT Class Template

Figure 293 shows how to:

Figure 293. Example: Equality Operators and the _DecimalT Class Template



#include <bcd.h>
#include <iostream.h>

_DecimalT<1, 0> op_1 = __D("+0"); // Declare and initialize
_DecimalT<1, 0> op_2 = __D("-0"); // valid BCD
_DecimalT<9,4> op_3 = __D("00012.3400");
_DecimalT<4,2> op_4 = __D("12.34");

int main(void) // These statements
{ // perform equality <==> test
// on the above variable
if (op_1 == op_2) // declarations
{
cout <<"op_1 equals op_2"<<endl;
}

if (op_3 != op_4)
{
cout <<"op_3 not equals op_4"<<endl;
}
else
{
cout <<"op_3 equals op_4"<<endl;
}
}

The output is:

+--------------------------------------------------------------------------------+
|	                                                                               |

|op_1 equals op_2 | |
op_3 equals op_4 | +--------------------------------------------------------------------------------+

Using Unary Operators with the _DecimalT Class Template

A unary expression contains one operand and a unary operator. All unary operators have the same precedence and have right- to-left associativity. For information about overloading unary operators, see the WebSphere Development Studio: ILE C/C++ Language Reference.

Figure 294 shows how to:

Figure 294. Example: Unary Operators and the _DecimalT Class Template



#include <iostream.h>
#include <bcd.h>
int main()
{
_DecimalT<10,2> op_1 = __D("12");
_DecimalT<5,5> op_2 = __D("-.12345");
_DecimalT<24,12> op_3 = __D("12.34");
_DecimalT<20,5> op_4 = __D("11.01");
cout << "op_1++ => " << op_1++ << endl;
cout << "op_1 after increment => " << op_1 << endl;
cout << "-op_2 => " << -op_2 << endl;
cout << "--op_3 => " << --op_3 << endl;
cout << "+op_4 => " << +op_4 << endl;
}

The resulting output is:

+--------------------------------------------------------------------------------+

|op_1++ => 12.00 | |
op_1 after increment => 13.00 | |
-op_2 => 0.12345 | |
--op_3 => 11.340000000000 | |
+op_4 => 11.01000 | +--------------------------------------------------------------------------------+


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