NOTE: Discussions of precision in most of this section assume that you are working on a 32-bit operating system. Section 6.3.3 describes the precision provided by Money.h++ classes on a 64-bit system.
In Money.h++ the precision of a number is the number of digits that number can contain. When using RWDecimal<T> or RWFixedDecimal<T> you set the precision of the numbers that the classes will represent by replacing the <T> with an integer type. Money.h++ provides class RWMultiPrecisionInt<n> to define an integer type. In addition, Money.h++ provides three RWMultiPrecisionInt<n> typedefs: RWMP1Int, RWMP2Int and RWMP3Int.
The private class RWMultiPrecisionInt<n> provides a very large number of bits of precision for representing decimals. By replacing the <n> in RWMultiPrecisionInt<n> with an integer from 1 to 16, you can specify up to 16 *32, or 512 bits of precision. For example, the following declaration creates a number that will have 5 *32 or 160 bits of precision:
RWDecimal< RWMultiPrecisionInt<5> > myDecimal
RWMultiPrecisionInt<n> has been specialized for n =1, since this case would use only one integer to represent a value, and you would be better off simply using an int. When n= 1, a type double stores the integer value.
NOTE: RWMultiPrecisionInt<1>, RWMultiPrecisionInt<2>, and RWMultiPrecision<3> are equivalent to RWMP1Int, RWMP2Int and RWMP3Int, described below.
The speed of computation changes depending upon the number of bits used for precision. This means that a computation that uses class RWDecimal<RWMultiPrecisionInt<1>> is faster than the same computation using RWDecimal<RWMultiPrecisionInt<16>>.
For ease of use, Money.h++ provides the three integer types shown in Table 7. These types provide 52-bit, 64-bit and 96-bit precision.
<T> | Decimal digits |
RWMP1Int |
15 |
RWMP2Int |
18 |
RWMP3Int |
28 |
Any number that can be written using 18 digits, a decimal point and a sign can be represented exactly using an RWDecimal<RWMP2Int>. Table 8 describes the size of numbers that can be represented using a given number of digits.
Decimal digits | Magnitude (American conventions) |
3 |
Thousands |
6 |
Millions |
9 |
Billions |
12 |
Trillions |
14 |
Trillions with two decimal places |
15 |
Thousands of trillions |
18 |
Trillions with 6 decimal places |
21 |
Billions of trillions |
24 |
Trillions of trillions |
27 |
Trillions of trillions with 3 decimal places |
28 |
Millions of trillions with ten decimal places |
Note that the speed of computation falls as the number of bits increases. The RWDecimal<RWMP1Int> and RWFixedDecimal<RWMP1Int> classes provide the fastest computations and can deal with numbers that are large enough for the vast majority of financial applications. Classes RWDecimal<RWMP2Int> and RWFixedDecimal<RWMP2Int> are slightly slower, but can handle the larger numbers required by some institutions. The RWDecimal<RWMP3Int> and RWFixedDecimal<RWMP3Int> classes are slightly slower still, but can handle the overwhelming majority of numbers required for financial applications.
Some computer systems use 64 bits to represent a long type, while others use 32 bits. Table 9 describes how this affects the number of bits available when you templatize a decimal class with either one of the provided decimal types or class RWMultiPrecisionInt<n>.
Type or Class | Bits of precision | |
32-bit long |
64-bit long | |
RWMP1Int |
52 |
52 |
RWMP2Int |
64 |
128 |
RWMP3Int |
96 |
192 |
RWMultiPrecisionInt<1> |
52 |
52 |
RWMultiPrecisionInt<2> |
64 |
128 |
RWMultiPrecisionInt<3> |
96 |
192 |
RWMultiPrecisionInt<4> |
128 |
256 |
RWMultiPrecisionInt<5> |
160 |
320 |
RWMultiPrecisionInt<6> |
192 |
384 |
RWMultiPrecisionInt<7> |
224 |
448 |
RWMultiPrecisionInt<8> |
256 |
512 |
RWMultiPrecisionInt<9> |
288 |
576 |
RWMultiPrecisionInt<10> |
320 |
640 |
RWMultiPrecisionInt<11> |
352 |
704 |
RWMultiPrecisionInt<12> |
384 |
768 |
RWMultiPrecisionInt<13> |
416 |
832 |
RWMultiPrecisionInt<14> |
448 |
896 |
RWMultiPrecisionInt<15> |
480 |
960 |
RWMultiPrecisionInt<16> |
512 |
1024 |
For a floating decimal point class, RWDecimal<T>, the decimal point is changed as necessary when operations are performed. For a fixed decimal point class, RWFixedDecimal<T>, the number of digits after the decimal point is fixed. Fixed decimal classes are useful for representing, for example, bank account balances where there must always be exactly two decimal places. The following two code fragments illustrate the difference between floating and fixed classes:
RWDecimal<RWMP2Int> a = "12.34", b="0.123"; a += b; // a = 12.463 RWFixedDecimal<RWMP2Int> c = "12.34", d="0.123"; c += d; // c = 12.46
Once set, the number of decimal places for a fixed decimal point class remains constant. Any value later assigned to it will be rounded to that number of decimal places. An important exception to this rule occurs when an RWFixedDecimal<T> has the value null. When a fixed decimal class has a value of null, it will take on the number of decimal places of an assigned value. Since the default constructor produces a null value, this feature can be particularly useful. For example:
RWFixedDecimal<RWMP2Int> a("1.234"); RWFixedDecimal<RWMP2Int> b; b = a;
is equivalent to
RWFixedDecimal<RWMP2Int> a("1.234"); RWFixedDecimal<RWMP2Int> b = a;
Elements of an array are initialized to a null value. This means that the number of decimal places for an element is not determined until a value is assigned to that element.
©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.