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

RWDecimal<T>

Module:  Currency   Group:  Decimal


RWDecimal<T> RWDecimalBase

Local Index

Members

Non-Members

Synopsis

#include <rw/currency/decimal.h>    /* For RWDecimal<T>   */ 
#include <rw/currency/mp2int.h>     /* For RWMP2Int       */

RWDecimal<RWMP2Int> x = "0.01";
cout << "one dollar: " << 100*x << endl;

The following header files are available for backward compatibility:

#include <rw/dec52.h> /* RWDecimal52 */
#include <rw/dec64.h> /* RWDecimal64 */
#include <rw/dec96.h> /* RWDecimal96 */

Description

RWDecimals are exact representations of decimal fractions. They behave very similarly to the built-in floating point types, float and double. However, because the built-in types use base 2, they cannot store decimal fractions exactly, resulting in rounding errors and a loss of precision. Since the RWDecimal<T> classes use base 10, they can do decimal math exactly.

RWDecimal<T> is templatized. Three short type names are provided: RWDecimal<RWMP1Int>, RWDecimal<RWMP2Int>, and RWDecimal<RWMP3Int>. Each type provides a different amount of precision, as described below in the "Limits" section. The trade-off is simple: the more precision, the slower the class.

You may also write your own RWDecimal<T> class. Throughout this section, when we refer to the RWDecimal<T> class, you can assume that it applies to any of the three provided classes, or to one you have defined.

Example

Limits

Class RWDecimal<T> provides three static member functions that can be used to define the limits on an RWDecimal<T> object. These functions return the precision, maximum value, and minimum value of a number:

Note that the precision returned by maxDigits() does not usually represent the number of digits in the maximum value of an RWDecimal<T> object. Rather, it indicates the largest number of digits that will always be supported by that object without returning an overflow error. For example, Table 1 indicates that the maximum value for RWDecimal<RWMP2Int> has 19 digits. Notice, however, that larger 19-digit numbers will cause an overflow error because they exceed the maximum value. Therefore, RWDecimal<RWMP2Int>::maxDigits() returns 18, because that is the number of digits that will always be supported without an overflow error.

The following code snippets demonstrate when an overflow condition caused by exceeding a maximum value will occur:

Table 1 indicates the minimum and maximum values for RWDecimal<T> when T is replaced by one of the provided multi-precision integer types:

Table 1: Max/Min Value and Digits for RWDecimal

Class Minimum value Max Digits
Maximum value

RWDecimal<RWMP3Int>

-39614081257132168796771975167

28

39614081257132168796771975167

RWDecimal<RWMP2Int>

-9223372036854775807

18

9223372036854775807

RWDecimal<RWMP1Int>

-9007199254740991

15

9007199254740991

Non-Numeric Values

As well as representing a decimal fraction, an RWDecimal<T> can also represent one of several non-numeric values. This concept has several uses, including, for example, representing a null entry from a database or indicating a missing value in data, which is to be subjected to a statistical analysis. Currency Module supports three types of non-numeric values: null, missing, and NaN (not a number). The missing and NaN values propagate while null values do not. This means that arithmetic operations performed using a missing or an NaN result in a missing or an NaN value, whereas arithmetic operations performed with a null operand return either a valid number or an NaN. Details are given below.

The special static constants RWDecimal::missing, RWDecimal::null, and RWDecimal::NaN are the prototype missing and null values. To set up a non-numeric RWDecimal<T>, use these static variables along with either the copy constructor or assignment operator. To test for a non-numeric value, use these values along with an equality operator. You can use the member function isNumber() to test if an RWDecimal<T> has a numeric value.

Arithmetic

For the most part, arithmetic between RWDecimal<T> objects is defined very simply: you get back an exact representation of the result of the operation. However, there are several special cases:

Public Constructors

RWDecimal();
RWDecimal(const RWDecimal<T>&);
RWDecimal(const char *s);
RWDecimal(long int x);
RWDecimal(int x);
RWDecimal(long int x, int e);
RWDecimal(int x, int e);
RWDecimal(const RWDecimalPortable&);

Public Member Functions

int
decimalPlaces() const;
bool
isNumber()const;
void
restoreFrom (RWvistream&);
void
restoreFrom(RWFile&);
void
saveOn (RWvostream&) const;
void
saveOn(RWFile&) const;

Public Member Operators

RWDecimal<T>&
operator=(const RWDecimal<T>&);
RWDecimal<T>&
operator+=(const RWDecimal<T>&);
RWDecimal<T>&
operator-=(const RWDecimal<T>&);
RWDecimal<T>&
operator*=(const RWDecimal<T>&);
RWDecimal<T>&
operator/=(const RWDecimal<T>&);
RWDecimalPortable
operator RWDecimalPortable() const;

Global Operators

RWDecimal<T>
operator+(const RWDecimal<T>&);
RWDecimal<T>
operator-(const RWDecimal<T>&);
RWDecimal<T>
operator+(const RWDecimal<T>&, const RWDecimal<T>&);
RWDecimal<T>
operator-(const RWDecimal<T>&, const RWDecimal<T>&);
RWDecimal<T>
operator*(const RWDecimal<T>&, const RWDecimal<T>&);
RWDecimal<T>
operator/(const RWDecimal<T>&, const RWDecimal<T>&);
bool
operator<(const RWDecimal<T>&, const RWDecimal<T>&);
bool
operator>(const RWDecimal<T>&, const RWDecimal<T>&);
bool
operator<=(const RWDecimal<T>&, const RWDecimal<T>&);
bool
operator>=(const RWDecimal<T>&, const RWDecimal<T>&);
bool
operator==(const RWDecimal<T>&, const RWDecimal<T>&);
bool
operator!=(const RWDecimal<T>&, const RWDecimal<T>&);
istream&
operator>>(istream&, RWDecimal<T>&);
ostream&
operator<<(ostream&, const RWDecimal<T>&);

Global Functions

RWDecimal<T>
abs (const RWDecimal<T>& x);
RWDecimal<T>
pow(const RWDecimal<T>& x, int n);
RWDecimal<T>
round (const RWDecimal<T>& x, int n,
      RWDecimalBase::RoundingMethod=PLAIN); 
long double
toDouble (const RWDecimal<T>& x);
long int
toInt(const RWDecimal<T>& x, 
      RWDecimalBase::RoundingMethod=PLAIN);
RWCString
toString(const RWDecimal<T>&);

Static Member Functions

static RWDecimal<T> 
from(double);
static 
RWDecimal<T> 
from(long double);
static int
maxDigits();
static RWDecimal<T>
maxValue();
static RWDecimal<T>
minValue();
static void
setInexactHandler (void (*)(const RWDecimalInexactErr<T>&));
static void
setOverflowHandler(void (*) (const RWDecimalOverflowErr<T>&));

Static Constants

RWDecimal<T> missing; 
RWDecimal<T> NaN;
RWDecimal<T> null;


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.