SourcePro® 2024.1 |
SourcePro® API Reference Guide |
Exact representation of a decimal fraction with a fixed number of digits after the decimal point, with automatic rounding to ensure correct number of decimal places. More...
#include <rw/currency/fixeddec.h>
Public Member Functions | |
RWFixedDecimal () | |
RWFixedDecimal (const char *s) | |
RWFixedDecimal (const RWDecimal< M > &d) | |
RWFixedDecimal (const RWFixedDecimal< M > &d) | |
RWFixedDecimal (int i) | |
RWFixedDecimal (int x, int e) | |
RWFixedDecimal (long int i) | |
RWFixedDecimal (long int x, int e) | |
RWFixedDecimal< M > & | operator*= (const RWFixedDecimal< M > &a) |
RWFixedDecimal< M > & | operator+= (const RWFixedDecimal< M > &a) |
RWFixedDecimal< M > & | operator-= (const RWFixedDecimal< M > &a) |
RWFixedDecimal< M > & | operator/= (const RWFixedDecimal< M > &a) |
RWFixedDecimal< M > & | operator= (const RWFixedDecimal< M > &x) |
Static Public Member Functions | |
static RWFixedDecimal< M > | infinityValue () |
static RWFixedDecimal< M > | missingValue () |
static RWFixedDecimal< M > | NaNValue () |
static RWFixedDecimal< M > | nullValue () |
static RWFixedDecimal< M > | pow (const RWFixedDecimal< M > &x, int e) |
static RWFixedDecimal< M > | round (const RWFixedDecimal< M > &x, int n, RWDecimalBase::RoundingMethod rm) |
static RWFixedDecimal< M > | SNaNValue () |
Static Public Attributes | |
static const RWFixedDecimal< M > | infinity |
static const RWFixedDecimal< M > | missing |
static const RWFixedDecimal< M > | NaN |
static const RWFixedDecimal< M > | null |
static const RWFixedDecimal< M > | SNaN |
Related Symbols | |
(Note that these are not member symbols.) | |
template<class M > | |
RWFixedDecimal< M > | abs (const RWFixedDecimal< M > &x) |
template<class M > | |
bool | operator!= (const RWFixedDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator* (const RWDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator* (const RWFixedDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator* (const RWFixedDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator* (const RWFixedDecimal< M > &a, double b) |
template<class M > | |
RWFixedDecimal< M > | operator* (const RWFixedDecimal< M > &a, int b) |
template<class M > | |
RWFixedDecimal< M > | operator* (const RWFixedDecimal< M > &a, long b) |
template<class M > | |
RWFixedDecimal< M > | operator* (double a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator* (int a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator* (long a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator+ (const RWDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator+ (const RWFixedDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator+ (const RWFixedDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator+ (const RWFixedDecimal< M > &a, int b) |
template<class M > | |
RWFixedDecimal< M > | operator+ (const RWFixedDecimal< M > &a, long b) |
template<class M > | |
RWFixedDecimal< M > | operator+ (int a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator+ (long a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator- (const RWDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator- (const RWFixedDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator- (const RWFixedDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator- (const RWFixedDecimal< M > &a, int b) |
template<class M > | |
RWFixedDecimal< M > | operator- (const RWFixedDecimal< M > &a, long b) |
template<class M > | |
RWFixedDecimal< M > | operator- (int a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator- (long a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (const char *a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (const RWDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (const RWDecimalPortable &a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (const RWFixedDecimal< M > &a, const char *b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (const RWFixedDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (const RWFixedDecimal< M > &a, const RWDecimalPortable &b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (const RWFixedDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (const RWFixedDecimal< M > &a, double b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (const RWFixedDecimal< M > &a, int b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (const RWFixedDecimal< M > &a, long b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (double a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (int a, const RWFixedDecimal< M > &b) |
template<class M > | |
RWFixedDecimal< M > | operator/ (long a, const RWFixedDecimal< M > &b) |
template<class M > | |
bool | operator< (const RWFixedDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
std::ostream & | operator<< (std::ostream &strm, const RWFixedDecimal< M > &x) |
template<class M > | |
bool | operator<= (const RWFixedDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
bool | operator== (const RWFixedDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
bool | operator> (const RWFixedDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
bool | operator>= (const RWFixedDecimal< M > &a, const RWFixedDecimal< M > &b) |
template<class M > | |
std::istream & | operator>> (std::istream &strm, RWFixedDecimal< M > &x) |
template<class M > | |
long double | toDouble (const RWFixedDecimal< M > &d) |
template<class M > | |
long int | toInt (const RWFixedDecimal< M > &d) |
template<class M > | |
long int | toInt (const RWFixedDecimal< M > &d, RWDecimalBase::RoundingMethod m) |
template<class M > | |
RWCString | toString (const RWFixedDecimal< M > &d) |
![]() | |
template<class M > | |
RWDecimal< M > | abs (const RWDecimal< M > &d) |
template<class M > | |
bool | operator!= (const RWDecimal< M > &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, 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* (double a, const RWDecimal< M > &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- (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 char *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 RWDecimal< M > &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 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/ (const RWDecimalPortable &a, const RWDecimal< M > &b) |
template<class M > | |
RWDecimal< M > | operator/ (double a, const RWDecimal< M > &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 > | |
bool | operator< (const RWDecimal< M > &a, const RWDecimal< M > &b) |
template<class M > | |
RWvostream & | operator<< (RWvostream &strm, const RWDecimal< M > &n) |
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 > | |
std::istream & | operator>> (std::istream &strm, RWDecimal< M > &d) |
template<class M > | |
RWDecimal< M > | pow (const RWDecimal< M > &d, int e) |
template<class M > | |
RWDecimal< M > | round (const RWDecimal< M > &d, int digits, RWDecimalBase::RoundingMethod m) |
template<class M > | |
long double | toDouble (const RWDecimal< M > &d) |
template<class M > | |
long int | toInt (const RWDecimal< M > &d, RWDecimalBase::RoundingMethod m) |
template<class M > | |
RWCString | toString (const RWDecimal< M > &d) |
Additional Inherited Members | |
![]() | |
enum | Op { assign , add , sub , mult , div , powop , pow10op , conversion } |
enum | RoundingMethod { PLAIN , UP , DOWN , BANKERS , TRUNCATE } |
enum | State { normal , nullstate , NaNstate , ctorError , missingstate , SNaNstate , infinitystate } |
![]() | |
RWDecimal () | |
RWDecimal (const char *str) | |
RWDecimal (const RWDecimal< M > &d) | |
RWDecimal (const RWDecimalPortable &) | |
RWDecimal (int i) | |
RWDecimal (int m, int e) | |
RWDecimal (long int i) | |
RWDecimal (long int m, int e) | |
int | decimalPlaces () const |
int | isNumber () const |
operator RWDecimalPortable () const | |
RWDecimal< M > & | operator*= (const RWDecimal< M > &a) |
RWDecimal< M > | operator+ () const |
RWDecimal< M > & | operator+= (const RWDecimal< M > &a) |
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) |
void | restoreFrom (RWFile &) |
void | restoreFrom (RWvistream &) |
void | saveOn (RWFile &) const |
void | saveOn (RWvostream &) const |
![]() | |
void | swap (RWDecimalBase &rhs) |
![]() | |
static RWDecimal< M > | from (double) |
static RWDecimal< M > | from (long double) |
static RWDecimal< M > | infinityValue () |
static int | maxDigits () |
static RWDecimal< M > | maxValue () |
static RWDecimal< M > | minValue () |
static RWDecimal< M > | missingValue () |
static RWDecimal< M > | NaNValue () |
static RWDecimal< M > | nullValue () |
static void | setInexactHandler (void(*eh)(const RWDecimalInexactErr< M > &)) |
static void | setOverflowHandler (void(*eh)(const RWDecimalOverflowErr< M > &)) |
static RWDecimal< M > | SNaNValue () |
![]() | |
static const RWDecimal< M > | infinity |
static const RWDecimal< M > | missing |
static const RWDecimal< M > | NaN |
static const RWDecimal< M > | null |
static const RWDecimal< M > | SNaN |
RWFixedDecimal 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 classes. The exception is that rounding automatically occurs to ensure the correct number of decimal places.
RWFixedDecimal is templatized. Three short type names are provided: RWFixedDecimal, RWFixedDecimal, and RWFixedDecimal. 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 class. Throughout this section, when we refer to the RWFixedDecimal class, you can assume that it applies to any of the three provided classes, or to one you have defined.
The following header files are available for backward compatibility:
Class RWFixedDecimal provides three static member functions that can be used to define the limits on an RWFixedDecimal 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 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 6 as the maximum value for RWFixedDecimal 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. Therefore, 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 when T
is replaced by one of the provided multiprecision integer types:
Class | Minimum value | Max Digits |
---|---|---|
Maximum value | ||
RWDecimal | -39614081257132168796771975167 | 28 |
39614081257132168796771975167 | ||
RWDecimal | -9223372036854775807 | 18 |
9223372036854775807 | ||
RWDecimal | -9007199254740991 | 15 |
9007199254740991 |
As well as representing a decimal fraction, an RWFixedDecimal 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. Currency Module 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, missing values and NaN values propagate while null values 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 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 has a numeric value.
For the most part, arithmetic between RWFixedDecimal objects is defined very simply: you get back an exact representation of the result of the operation. There are several special cases, however:
|
inline |
Constructs an RWFixedDecimal with a value of null.
|
inline |
Copy constructor. Constructs an RWFixedDecimal that is a copy of the argument.
|
inline |
Constructs an RWFixedDecimal that represents the same value as the parameter. The number of decimal places is fixed to be the same as the parameter.
|
inline |
Constructs an RWFixedDecimal 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
.
|
inline |
Constructs an RWFixedDecimal with value i.
|
inline |
Constructs an RWFixedDecimal with value x*10-e
where e
represents the number of digits after the decimal point.
|
inline |
Constructs an RWFixedDecimal with value x*10-e
where e
represents the number of digits after the decimal point.
|
inline |
Constructs an RWDecimal from the null terminated character string s. Since we write numbers using base 10 and the RWDecimal 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 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. For more information on exact input grammar allowed, see the discussion of Input Format Grammar in the Currency Module User's Guide.
|
static |
Specifies the non-numeric value infinity
. Use this function to test or to set its value on an RWFixedDecimal.
|
static |
Specifies the non-numeric value missing
. Use this function to test or to set its value on an RWFixedDecimal.
|
static |
Specifies the non-numeric value NaN
. Use this function to test or to set its value on an RWFixedDecimal.
|
static |
Specifies the non-numeric value null
. Use this function to test or to set its value on an RWFixedDecimal.
|
inline |
Performs a multiplication of self and a, and stores the result in self.
|
inline |
Performs an addition of self and a, and stores the result in self.
|
inline |
Performs a subtraction of a from self, and stores the result in self.
|
inline |
Performs a division of self by a, and stores the result in self.
RWFixedDecimal< M > & RWFixedDecimal< M >::operator= | ( | const RWFixedDecimal< M > & | x | ) |
Sets value of self equal to the value of the argument.
|
inlinestatic |
Returns x 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 x has any decimal places.
|
inlinestatic |
Returns x rounded to n decimal places. The method of rounding is controlled by the optional last parameter as follows:
Table 7: Methods of rounding
Method | Description | 1.25 | 1.35 | 1.251 |
PLAIN | Rounds away from zero on a tie | 1.3 | 1.4 | 1.3 |
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 |
The last three columns indicate the result of rounding three example numbers to one decimal place.
|
static |
Specifies the non-numeric value SNaN
. Use this function to test or to set its value on an RWFixedDecimal.
|
related |
Returns the absolute value of x.
|
related |
The inequality operator returns false
if the two operands are exactly the same, and true
otherwise.
|
related |
Multiplication operator.
|
related |
Multiplication operator.
|
related |
Multiplication operator.
|
related |
Multiplication operator.
|
related |
Multiplication operator.
|
related |
Multiplication operator.
|
related |
Multiplication operator.
|
related |
Multiplication operator.
|
related |
Multiplication operator.
|
related |
Addition operator
|
related |
Addition operator
|
related |
Addition operator
|
related |
Addition operator
|
related |
Addition operator
|
related |
Addition operator
|
related |
Addition operator
|
related |
Subtraction operator.
|
related |
Subtraction operator.
|
related |
Subtraction operator.
|
related |
Subtraction operator.
|
related |
Subtraction operator.
|
related |
Subtraction operator.
|
related |
Subtraction operator.
|
related |
Division Operator
|
related |
Division Operator
|
related |
Division Operator
|
related |
Division Operator
|
related |
Division Operator
|
related |
Division Operator
|
related |
Division Operator
|
related |
Division Operator
|
related |
Division Operator
|
related |
Division Operator
|
related |
Division Operator
|
related |
Division Operator
|
related |
Division Operator
|
related |
|
related |
Writes an RWFixedDecimal 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.
|
related |
|
related |
The equality operator returns true
if the two operands are exactly the same, and false
otherwise.
|
related |
|
related |
|
related |
Reads an RWFixedDecimal from an input stream. The format expected is ( -xxx.yyy
) where the xxx
and yyy
are integers (which may begin with zeros) and all the parts – parentheses, minus sign, xxx
, decimal point, and yyy
– are optional. Since we write numbers using base 10, and the RWFixedDecimal class stores numbers using base 10, the number constructed is an exact representation of the input.
|
related |
Converts the argument to a double precision value close to its true value.
|
related |
Converts the argument to an integer. The method of rounding used is RWDecimalBase::PLAIN, as described in detail in the description of the global function round().
|
related |
Converts the argument to an integer. The method of rounding is controlled by the last parameter, as described in detail in the description of the global function round().
|
related |
Converts an RWFixedDecimal to a string. The string has the form xxx.yyy
where the xxx
and yyy
are integers and only the parts necessary are written. For more elaborate formatting, use an RWDecimalFormat object.
|
static |
Specifies the non-numeric value infinity
. Use this static instance to test or to set its value on an RWFixedDecimal.
|
static |
Specifies the non-numeric value missing
. Use this static instance to test or to set its value on an RWFixedDecimal.
|
static |
Specifies the non-numeric value NaN
. Use this static instance to test or to set its value on an RWFixedDecimal.
|
static |
Specifies the non-numeric value null
. Use this static instance to test or to set its value on an RWFixedDecimal.
|
static |
Specifies the non-numeric value SNaN
. Use this static instance to test or to set its value on an RWFixedDecimal.
Copyright © 2024 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved. |