Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Currency Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

6.3 Precision


NOTE -- Discussions of precision in most of this section assume that you are working on a 32-bit operating system. Section 6.3, "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.

6.3.1 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:

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 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> >.

6.3.2 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.

6.3.3 32-bit long Versus 64-bit long Types

Some computer systems use 64 bits to represent a long type, while others use 32 bits. Table 6 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>.

Table 6: Bits of precision for 32-bit long types and 64-bit long types 

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

6.3.4 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:

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:

is equivalent to

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.



Previous fileTop of DocumentContentsIndex pageNext file

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.