Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

RWFixedDecimal<T>


RWFixedDecimal RWDecimal RWDecimalBase

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <rw/money/fixeddec.h> /* for RWFixedDecimal<T>   */
#include <rw/money/mp2int.h>   /* For RWMP2Int            */

RWFixedDecimal<RWMP2Int> dollarAccount(0,2) = "0.01"; 
    // initialize to zero then set to 2 decimal places

The following header files are available for backward compatibility:

#include <rw/fixdec52.h> /* RWFixedDecimal52 */
#include <rw/fixdec64.h> /* RWFixedDecimal64 */
#include <rw/fixdec96.h> /* RWFixedDecimal96 */

Description

RWFixedDecimal<T> classes are exact representations of decimal fractions with a fixed number of digits after the decimal point. In most ways, they behave exactly like the corresponding RWDecimal<T> classes. The exception is that rounding automatically occurs to insure the correct number of decimal places.

RWFixedDecimal<T> is templatized. Three short type names are provided: RWFixedDecimal<RWMP1Int>, RWFixedDecimal<RWMP2Int>, and RWFixedDecimal<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 RWFixedDecimal<T> class. Throughout this section, when we refer to the RWFixedDecimal<T> class, you can assume that it applies to any of the three provided classes, or to one you have defined.

Example

Limits

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

Note that the precision returned by maxDigits() does not usually represent the number of digits in the largest possible RWFixedDecimal<T> object. Rather, it indicates the maximum number of digits supported by that object without returning an overflow error. For example the number shown in, Table 12 as the maximum value for RWFixedDecimal<RWMP2Int> has 19 digits. Notice, however, that any number larger than the 19-digit number shown will cause an overflow error because it exceeds the maximum value. There fore, RWFixedDecimal<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 6 indicates the minimum and maximum values for RWFixedDecimal<T> when T is replaced by one of the provided multi-precision integer types:

Table 6 -- Minimum and maximum values for RWFixedDecimal<T>

ClassMinimum valueMax 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 RWFixedDecimal<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 that is to be subjected to a statistical analysis. Money.h++ supports three sorts of non-numeric values: null, missing, and NaN (Not a Number).

The result of performing arithmetic with a missing or an NaN is itself a missing or an NaN. An arithmetic operation in which one operand is a null returns either a valid number or an NaN (details are given below). Thus, missings and NaNs propagate while nulls do not.

The special static variables RWFixedDecimal::missing, RWFixedDecimal::null, and RWFixedDecimal::NaN are the prototype missing and null values; to set up a non-numeric RWFixedDecimal<T> use these static variables along with either the copy constructor or the 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 RWFixedDecimal<T> has a numeric value.

Arithmetic

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

Public Constructors

RWFixedDecimal();
RWFixedDecimal(const RWFixedDecimal&);
RWFixedDecimal(const RWDecimal&);
RWFixedDecimal(const char *s);
RWFixedDecimal(long int x);
RWFixedDecimal(int x);
RWFixedDecimal(long int x, int e);
RWFixedDecimal(int x, int e);

Public Member Functions

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

Public Member Operators

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

Global Operators

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

Global Functions

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

Static Member Functions

static RWFixedDecimal<T> 
from(double);
static RWFixedDecimal<T> 
from(long double);
static int
maxDigits();
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 Variables

RWFixedDecimal<T> null;
RWFixedDecimal<T> missing;
RWFixedDecimal<T> NaN;


Previous fileTop of DocumentContentsIndexNext file

©Copyright 1999, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.