SourcePro C++ 12.0 |
SourcePro® C++ API Reference Guide |
SourcePro C++ Documentation Home |
Exactly represents a decimal fraction. More...
#include <rw/currency/decimal.h>
Public Member Functions | |
RWDecimal () | |
RWDecimal (const RWDecimal< M > &d) | |
RWDecimal (const RWDecimalPortable &) | |
operator RWDecimalPortable () const | |
RWDecimal (int i) | |
RWDecimal (long int i) | |
RWDecimal (int m, int e) | |
RWDecimal (long int m, int e) | |
RWDecimal (const char *str) | |
RWDecimal< M > | operator- () const |
RWDecimal< M > | operator+ () const |
RWDecimal< M > & | operator= (const RWDecimal< M > &a) |
RWDecimal< M > & | operator+= (const RWDecimal< M > &a) |
RWDecimal< M > & | operator-= (const RWDecimal< M > &a) |
RWDecimal< M > & | operator*= (const RWDecimal< M > &a) |
RWDecimal< M > & | operator/= (const RWDecimal< M > &a) |
void | saveOn (RWvostream &) const |
void | restoreFrom (RWvistream &) |
void | saveOn (RWFile &) const |
void | restoreFrom (RWFile &) |
int | isNumber () const |
int | decimalPlaces () const |
Static Public Member Functions | |
static void | setInexactHandler (void(*eh)(const RWDecimalInexactErr< M > &)) |
static void | setOverflowHandler (void(*eh)(const RWDecimalOverflowErr< M > &)) |
static int | maxDigits () |
static RWDecimal< M > | maxValue () |
static RWDecimal< M > | minValue () |
static RWDecimal< M > | from (double) |
static RWDecimal< M > | from (long double) |
Static Public Attributes | |
static const RWDecimal< M > | null |
static const RWDecimal< M > | missing |
static const RWDecimal< M > | NaN |
static const RWDecimal< M > | SNaN |
static const RWDecimal< M > | infinity |
Related Functions | |
(Note that these are not member functions.) | |
template<class M > | |
RWDecimal< M > | operator+ (const RWDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator+ (const RWDecimal< M > &a, int b) |
template<class M > | |
RWDecimal< M > | operator+ (const RWDecimal< M > &a, long b) |
template<class M > | |
RWDecimal< M > | operator+ (int a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator+ (long a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator- (const RWDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator- (const RWDecimal< M > &a, int b) |
template<class M > | |
RWDecimal< M > | operator- (const RWDecimal< M > &a, long b) |
template<class M > | |
RWDecimal< M > | operator- (int a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator- (long a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator* (const RWDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator* (double a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator* (const RWDecimal< M > &a, double b) |
template<class M > | |
RWDecimal< M > | operator* (const RWDecimal< M > &a, int b) |
template<class M > | |
RWDecimal< M > | operator* (const RWDecimal< M > &a, long b) |
template<class M > | |
RWDecimal< M > | operator* (int a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator* (long a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator/ (const RWDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator/ (const RWDecimal< M > &a, int b) |
template<class M > | |
RWDecimal< M > | operator/ (const RWDecimal< M > &a, long b) |
template<class M > | |
RWDecimal< M > | operator/ (int a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator/ (long a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator/ (const RWDecimal< M > &a, const RWDecimalPortable &b) |
template<class M > | |
RWDecimal< M > | operator/ (const RWDecimalPortable &a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator/ (const RWDecimal< M > &a, const char *b) |
template<class M > | |
RWDecimal< M > | operator/ (const char *a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator/ (double a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator/ (const RWDecimal< M > &a, double b) |
template<class M > | |
bool | operator== (const RWDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
bool | operator< (const RWDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
bool | operator!= (const RWDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
bool | operator> (const RWDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
bool | operator>= (const RWDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
bool | operator<= (const RWDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | pow (const RWDecimal< M > &d, int e) |
template<class M > | |
RWDecimal< M > | abs (const RWDecimal< M > &d) |
template<class M > | |
RWDecimal< M > | round (const RWDecimal< M > &d, int digits, RWDecimalBase::RoundingMethod m) |
template<class M > | |
long int | toInt (const RWDecimal< M > &d, RWDecimalBase::RoundingMethod m) |
template<class M > | |
RWCString | toString (const RWDecimal< M > &d) |
template<class M > | |
long double | toDouble (const RWDecimal< M > &d) |
template<class M > | |
std::istream & | operator>> (std::istream &strm, RWDecimal< M > &d) |
template<class M > | |
RWvostream & | operator<< (RWvostream &strm, const RWDecimal< M > &n) |
RWDecimal<T> instances 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.
#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;
#include <iostream> #include <rw/currency/decimal.h> #include <rw/currency/mp2int.h> int main() { RWDecimal<RWMP2Int> penny = "0.01"; RWDecimal<RWMP2Int> bank = 0; for(int i=100; i--;) bank+=penny; // deposit 100 pennies bank -= 1; // withdraw a buck std::cout << (bank==0 ? "broke!" : "whew! still solvent") << std::endl; return 0; }
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:
int RWDecimal<T>::maxDigits(); // indicates precision RWDecimal<T> RWDecimal<T>::maxValue(); // indicates maximum value RWDecimal<T> RWDecimal<T>::minValue(); // indicates minimum value
The following code snippets demonstrate when an overflow condition caused by exceeding a maximum value will occur:
// Set max to maximum value: RWDecimal<RWMP1Int>max = RWDecimal<RWMP1Int>::maxValue(); // Add 1 to max to generate an overflow error: RWDecimal<RWMP1Int>tooBig = max + RWDecimal<RWMP1Int>(1); // Set min to minimum value: RWDecimal<RWMP1Int>min = RWDecimal<RWMP1Int>::minValue(); // Subtract 1 from min to generate an overflow error: RWDecimal<RWMP1Int>tooSmall = min - RWDecimal<RWMP1Int>(1);
Table 1 indicates the minimum and maximum values for RWDecimal<T> when T
is replaced by one of the provided multi-precision integer types:
Class | Minimum value | Max Digits |
---|---|---|
Maximum value | ||
RWDecimal<RWMP3Int> | -39614081257132168796771975167 | 28 |
39614081257132168796771975167 | ||
RWDecimal<RWMP2Int> | -9223372036854775807 | 18 |
9223372036854775807 | ||
RWDecimal<RWMP1Int> | -9007199254740991 | 15 |
9007199254740991 |
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 five types of non-numeric values: null, missing, NaN (not a number), SNaN (signaling NaN), and infinity. The SNaN value behaves as a NaN value for all operations except toDouble(). 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. Operations performed on infinity follow specific rules detailed in RWDecimalBase::State, and may return infinity, NaN, or zero. Details are given below.
The special static constants RWDecimal::missing, RWDecimal::null, RWDecimal::NaN, RWDecimal::SNaN, and RWDecimal::infinity are the prototype missing, null, NaN, SNaN and infinity 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.
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:
Constructs an RWDecimal<T> with a value of null.
Copy constructor. Constructs an RWDecimal<T> that is a copy of the argument.
RWDecimal< M >::RWDecimal | ( | const RWDecimalPortable & | ) |
Constructs an RWDecimal<T> representation of the argument. This constructor is most often used implicitly to provide type conversion from one RWDecimal<T> type to another using an RWDecimalPortable object as an intermediate representation.
Constructs an RWDecimal<T> with value i. The explicit integer constructors prevent initializations from 0 from being ambiguous. Without the int
constructor, the compiler would not know whether to convert 0 to a const
char*
or a long
int
.
Constructs an RWDecimal<T> with value i. The explicit integer constructors prevent initializations from 0 from being ambiguous. Without the int
constructor, the compiler would not know whether to convert 0 to a const
char*
or a long
int
.
Constructs an RWDecimal<T> with value m * 10-e
, so that e represents the number of digits after the decimal point.
Constructs an RWDecimal<T> with value m * 10-e
, so that e represents the number of digits after the decimal point.
Constructs an RWDecimal<T> from the null-terminated character string str. Since we write numbers using base 10, and the RWDecimal<T> class stores numbers using base 10, the number constructed is an exact representation of the string. If the string cannot be successfully parsed as a number, the RWDecimal<T> is initialized to null. If the number in the string cannot be exactly represented (for example, it has too many significant digits) then the appropriate error handler (either the inexact or overflow handler) is called. The string may contain embedded commas to separate groups of digits and may have a leading dollar sign. Negatives can be indicated with a negative sign or by using parentheses. A technical description of the exact input grammar allowed is given in the section "Technical Notes" of the Currency Module User's Guide.
int RWDecimal< M >::decimalPlaces | ( | ) | const [inline] |
Returns the number of digits to the right of the decimal point.
Reimplemented in RWFixedDecimal< M >.
Converts a floating point value to an RWDecimal. Results may be inexact.
Reimplemented in RWFixedDecimal< M >.
Converts a floating point value to an RWDecimal. Results may be inexact.
Reimplemented in RWFixedDecimal< M >.
int RWDecimal< M >::isNumber | ( | ) | const [inline] |
Returns true
if self represents a decimal fraction; returns false
if self is not a representation of a number; for example, if self is a null or missing value.
Reimplemented in RWFixedDecimal< M >.
static int RWDecimal< M >::maxDigits | ( | ) | [inline, static] |
Returns the maximum number of digits that an RWDecimal<T> of the indicated type is guaranteed to represent without overflow. In most cases, an RWDecimal<T> can represent some numbers of length actualDigits
, where actualDigits=
maxDigits() + 1
. For example, the maxDigits() value for class RWDecimal<T> is 18, even though the number of digits in the maxValue() for the class is 19.
Reimplemented in RWFixedDecimal< M >.
Returns the maximum value that can be represented by this class. The maximum value +1 will always generate an overflow error.
Reimplemented in RWFixedDecimal< M >.
Returns the minimum value that can be represented by this class. The minimum value -1 will always generate an overflow error.
Reimplemented in RWFixedDecimal< M >.
RWDecimal< M >::operator RWDecimalPortable | ( | ) | const |
Conversion to a portable decimal object. This is most often used implicitly to interface with a facility such as I/O or type conversion.
Reimplemented in RWFixedDecimal< M >.
Performs the operation between self and the argument, and then stores the results in self. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
Returns the result of applying the unary operator.
Reimplemented in RWFixedDecimal< M >.
Performs the operation between self and the argument, and then stores the results in self. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
Returns the result of applying the unary operator.
Reimplemented in RWFixedDecimal< M >.
Performs the operation between self and the argument, and then stores the results in self. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
Performs the operation between self and the argument, and then stores the results in self. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
Sets the value of self equal to the value of the argument.
Restores value of self from an RWFile. The RWFile class is provided with Rogue Wave's Essential Tools Module class library. This function requires that you link in the Essential Tools Module library.
Reimplemented in RWFixedDecimal< M >.
void RWDecimal< M >::restoreFrom | ( | RWvistream & | ) |
Restores value of self from a virtual stream. The virtual stream class is provided with Rogue Wave's Essential Tools Module class library. This function requires that you link in the Essential Tools Module library.
Reimplemented in RWFixedDecimal< M >.
Stores value of self to an RWFile in binary format. The RWFile class is provided with Rogue Wave's Essential Tools Module class library. This function requires that you link in the Essential Tools Module library.
Reimplemented in RWFixedDecimal< M >.
void RWDecimal< M >::saveOn | ( | RWvostream & | ) | const |
Stores value of self to a virtual stream. The virtual stream class is provided with Rogue Wave's Essential Tools Module class library. This function requires that you link in the Essential Tools Module library.
Reimplemented in RWFixedDecimal< M >.
static void RWDecimal< M >::setInexactHandler | ( | void(*)(const RWDecimalInexactErr< M > &) | eh | ) | [inline, static] |
Sets the function that is called when an "inexact" error occurs. This type of error most often indicates the use of an arithmetic operation that will cause loss of precision in the result. defaultInexactHandler
, the default error handler, prints an error message, but does not throw an exception.
Reimplemented in RWFixedDecimal< M >.
static void RWDecimal< M >::setOverflowHandler | ( | void(*)(const RWDecimalOverflowErr< M > &) | eh | ) | [inline, static] |
Sets the function that is called when an "overflow" error occurs. This type of error most often indicates the use of an arithmetic operation that will cause a result larger than can be stored. The default error handler, defaultOverflowHandler
, simply throws the error object as a C++ exception.
Reimplemented in RWFixedDecimal< M >.
Returns the absolute value of d.
The not equal operator returns true
if the two operands are not exactly the same, and false
otherwise.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
RWDecimal< M > operator* | ( | const RWDecimal< M > & | a, | |
const RWDecimal< M > & | b | |||
) | [related] |
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
RWDecimal< M > operator+ | ( | const RWDecimal< M > & | a, | |
const RWDecimal< M > & | b | |||
) | [related] |
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
RWDecimal< M > operator- | ( | const RWDecimal< M > & | a, | |
const RWDecimal< M > & | b | |||
) | [related] |
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
RWDecimal< M > operator/ | ( | const RWDecimalPortable & | a, | |
const RWDecimal< M > & | b | |||
) | [related] |
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
RWDecimal< M > operator/ | ( | const RWDecimal< M > & | a, | |
const RWDecimalPortable & | b | |||
) | [related] |
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
RWDecimal< M > operator/ | ( | const RWDecimal< M > & | a, | |
const RWDecimal< M > & | b | |||
) | [related] |
These arithmetic operators have their conventional meanings. If the resulting operation causes loss of precision or overflow in the result, the appropriate error handler will be called before the function returns.
RWvostream & operator<< | ( | RWvostream & | strm, | |
const RWDecimal< M > & | n | |||
) | [related] |
Writes an RWDecimal<T> to an output stream. Output is written in the form -xxx.yyy
where the xxx
and yyy
are integers and only the necessary parts are written. For more elaborate formatting, use an RWDecimalFormat object.
The equality operator returns true
if the two operands are exactly the same, and false
otherwise.
std::istream & operator>> | ( | std::istream & | strm, | |
RWDecimal< M > & | d | |||
) | [related] |
Reads an RWDecimal<T> from an input stream. Since we write numbers using base 10 and the RWDecimal<T> class stores numbers using base 10, the number constructed is an exact representation of the input. The number may contain embedded commas to separate groups of digits and may have a leading dollar sign. Negatives can be indicated with a negative sign or by using parentheses. A technical description of the exact input grammar allowed is given in the section "Technical Notes" of the Currency Module User's Guide.
Returns d raised to the exponent e. This computation is likely to cause a loss of precision (and a corresponding call to the precision error handler) if e is at all large, and d has any decimal places.
RWDecimal< M > round | ( | const RWDecimal< M > & | d, | |
int | digits, | |||
RWDecimalBase::RoundingMethod | m | |||
) | [related] |
Returns d rounded to digits decimal places. The method of rounding is controlled by the optional last parameter. These parameters are enumerated in RWDecimalBase, and their effects are shown in the table below. The last three columns indicate the result of rounding three example numbers to one decimal place. Rounding a negative number returns exactly the same result as rounding the corresponding positive number and making the result negative.
Method | Description | 1.25 | 1.35 | 1.251 |
PLAIN | Rounds towards zero on a tie | 1.2 | 1.3 | 1.2 |
UP | Always rounds away from zero | 1.3 | 1.4 | 1.3 |
DOWN | Always rounds toward zero | 1.2 | 1.3 | 1.2 |
TRUNCATE | Same as DOWN | 1.2 | 1.3 | 1.2 |
BANKERS | On a tie, round so last digit is even | 1.2 | 1.4 | 1.3 |
long double toDouble | ( | const RWDecimal< M > & | d | ) | [related] |
long int toInt | ( | const RWDecimal< M > & | d, | |
RWDecimalBase::RoundingMethod | m | |||
) | [related] |
Converts an RWDecimal<T> to a string. The string produced by this function has the form -xxx.yyy
where the xxx
and yyy
are integers to the left and right of the decimal. Digits only appear if necessary. For more elaborate formatting, use an RWDecimalFormat object.
These are the prototype non-numeric values. Use these to set a RWDecimal<T> to a non-numeric value or to test for a specific non-numeric value.
Reimplemented in RWFixedDecimal< M >.
These are the prototype non-numeric values. Use these to set a RWDecimal<T> to a non-numeric value or to test for a specific non-numeric value.
Reimplemented in RWFixedDecimal< M >.
These are the prototype non-numeric values. Use these to set a RWDecimal<T> to a non-numeric value or to test for a specific non-numeric value.
Reimplemented in RWFixedDecimal< M >.
These are the prototype non-numeric values. Use these to set a RWDecimal<T> to a non-numeric value or to test for a specific non-numeric value.
Reimplemented in RWFixedDecimal< M >.
These are the prototype non-numeric values. Use these to set a RWDecimal<T> to a non-numeric value or to test for a specific non-numeric value.
Reimplemented in RWFixedDecimal< M >.
© 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.