Currency Module User’s Guide : Chapter 6 Decimal Classes : Precision
Precision
NOTE >> Discussions of precision in most of this section assume that you are working on a 32-bit operating system. “Precision” describes the precision provided by Currency Module classes on a 64-bit system.
In the Currency Module 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. The Currency Module provides class RWMultiPrecisionInt<n> to define an integer type. In addition, the Currency Module provides three RWMultiPrecisionInt<n> typedefs: RWMP1Int, RWMP2Int, and RWMP3Int.
Class RWMultiPrecisionInt<n>
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 RWMultiPrecisionInt<3> are equivalent to RWMP1Int, RWMP2Int and RWMP3Int, described below.
The speed of computation changes depend 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> >.
RWMP1Int, RWMP2Int, and RWMP3Int Integer Types
For ease of use, the Currency Module provides the three integer types shown in Table 4. These types provide 52-bit, 64-bit, and 96-bit precision.
Table 4 – Int types provided in Currency Module 
<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 5 describes the size of numbers that can be represented using a given number of digits.
Table 5 – Decimal digits and magnitude (United States convention) 
Decimal digits
Magnitude
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.
Fixed and Floating Decimal Classes
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.