A class for decimal arithmetics. More...
#include <bpsdecimal.h>
Public Member Functions | |
BpsDecimal | abs () const |
BpsDecimal () | |
The default constructor initializes the value to NaN. | |
BpsDecimal (const BpsDecimal &aOther) | |
The copy constructor. More... | |
BpsDecimal (const char *aString) | |
Construct from a decimal string representation. More... | |
BpsDecimal (const QByteArray &aString) | |
Construct from a decimal string representation. More... | |
BpsDecimal (const QString &aString) | |
Construct from a decimal string representation. More... | |
BpsDecimal (const QVariant &aVariant) | |
Construct from a QVariant. More... | |
BpsDecimal (double aValue) | |
Construct from a double value. More... | |
BpsDecimal (qint32 aValue) | |
Construct from a signed 32 bit integer value. More... | |
BpsDecimal (qlonglong aValue) | |
Construct from a 64 bit signed integer value. More... | |
BpsDecimal (quint32 aValue) | |
Construct from a unsigned 32 bit integer value. More... | |
BpsDecimal (qulonglong aValue) | |
Construct from a 64 bit unsigned integer value. More... | |
BpsDecimal | ceil () const |
If the value is not integer, round up to next integer (towards +Infinity). More... | |
BpsDecimal | div (const BpsDecimal &aOther) const |
Divides this value by the other value and returns the integer part of the result. More... | |
BpsDecimal | down () const |
If the value is not integer, round down to next integer (towards 0). More... | |
BpsDecimal | exp () const |
Calculate e to the power of this value and return the result. More... | |
BpsDecimal | floor () const |
If the value is not integer, round down to next integer (towards -Infinity). More... | |
bool | isFinite () const |
Check if value is neither infinite nor a NaN. More... | |
bool | isInfinite () const |
Check if value is infinite. More... | |
bool | isInteger () const |
Check if value is finite and has exponent=0. More... | |
bool | isNaN () const |
Check if value is not a number. More... | |
bool | isNegative () const |
Check if value is negative. More... | |
BpsDecimal | ln () const |
Calculate the natural logarithm (to base e) of this value and return the result. More... | |
BpsDecimal | log10 () const |
Calculate the logarithm to base ten of this value and return the result. More... | |
BpsDecimal | max (const BpsDecimal &aOther) const |
Compare this value with the other and return the larger (nearer towards +Infinity). More... | |
BpsDecimal | min (const BpsDecimal &aOther) const |
Compare this value with the other and return the smaller (nearer towards -Infinity). More... | |
operator QVariant () const | |
Automatic conversion to QVariant. More... | |
bool | operator!= (const BpsDecimal &aOther) const |
Compare with other value for un-equality. More... | |
BpsDecimal | operator% (const BpsDecimal &aOther) const |
Integer divide by other value and return remainder as result. More... | |
BpsDecimal & | operator%= (const BpsDecimal &aOther) |
Integer divide by other value and set remainder as value. More... | |
BpsDecimal | operator* (const BpsDecimal &aOther) const |
Multiply with other value and return result. More... | |
BpsDecimal & | operator*= (const BpsDecimal &aOther) |
Multiply with other value. More... | |
BpsDecimal | operator+ (const BpsDecimal &aOther) const |
Add two values and return result. More... | |
BpsDecimal & | operator++ () |
Prefix increment the value by one. More... | |
BpsDecimal | operator++ (int) |
Postfix increment the value by one. More... | |
BpsDecimal & | operator+= (const BpsDecimal &aOther) |
Add other value. More... | |
BpsDecimal | operator- (const BpsDecimal &aOther) const |
Subtract second value and return result. More... | |
BpsDecimal & | operator-- () |
Prefix decrement the value by one. More... | |
BpsDecimal | operator-- (int) |
Postfix decrement the value by one. More... | |
BpsDecimal & | operator-= (const BpsDecimal &aOther) |
Subtract second value. More... | |
BpsDecimal | operator/ (const BpsDecimal &aOther) const |
Divide by other value and return result. More... | |
BpsDecimal & | operator/= (const BpsDecimal &aOther) |
Divide by other value. More... | |
bool | operator< (const BpsDecimal &aOther) const |
Compare if less than other value. More... | |
bool | operator<= (const BpsDecimal &aOther) const |
Compare if less than or equal to other value. More... | |
BpsDecimal & | operator= (const BpsDecimal &aOther) |
Assign value from an other decimal. More... | |
BpsDecimal & | operator= (const char *aString) |
Assign value from a string. More... | |
BpsDecimal & | operator= (const QByteArray &aString) |
Assign value from a string. More... | |
BpsDecimal & | operator= (const QString &aString) |
Assign value from a string. More... | |
BpsDecimal & | operator= (const QVariant &aVariant) |
Assign value from a QVariant. More... | |
BpsDecimal & | operator= (double aOther) |
Assign value from a double. More... | |
BpsDecimal & | operator= (qint32 aOther) |
Assign value from a 32 bit signed integer. More... | |
BpsDecimal & | operator= (qlonglong aOther) |
Assign value from a 64 bit signed integer. More... | |
BpsDecimal & | operator= (quint32 aOther) |
Assign value from a 32 bit unsigned integer. More... | |
BpsDecimal & | operator= (qulonglong aOther) |
Assign value from a 64 bit unsigned integer. More... | |
bool | operator== (const BpsDecimal &aOther) const |
Compare with other value for equality. More... | |
bool | operator> (const BpsDecimal &aOther) const |
Compare if greater than other value. More... | |
bool | operator>= (const BpsDecimal &aOther) const |
Compare if greater than or equal to other value. More... | |
BpsDecimal | power (const BpsDecimal &aOther) const |
Raises this value to the power of the other value and returns the result. More... | |
BpsDecimal | rounded (int aDecPlaces) const |
Round to an number of decimal places. More... | |
BpsDecimal | sqrt () const |
Calculate the square root of this value and return the result. More... | |
double | toDouble () const |
Get the value as double. More... | |
qint32 | toInt () const |
Get the value as signed 32 bit integer. More... | |
qlonglong | toLongLong () const |
Get the value as 64 bit signed integer. More... | |
QString | toString () const |
Converts a decimal value to a string. More... | |
QString | toString (int aDecPlaces, int aMaxSize=36) const |
Converts a decimal value to a string of format [-]9.9. More... | |
quint32 | toUInt () const |
Get the value as unsigned 32 bit integer. More... | |
qulonglong | toULongLong () const |
Get the value as 64 bit unsigned integer. More... | |
QVariant | toVariant () const |
Explicit conversion to QVariant. More... | |
BpsDecimal | up () const |
If the value is not integer, round up to next integer (away from 0). More... | |
Static Public Member Functions | |
static BpsDecimal | fromString (const char *aString, bool *aOk=0) |
Convert a string to a decimal value. More... | |
static BpsDecimal | fromString (const QByteArray &aString, bool *aOk=0) |
Convert a string to a decimal value. More... | |
static BpsDecimal | fromString (const QString &aString, bool *aOk=0) |
Convert a string to a decimal value. More... | |
static int | metaType () |
static BpsDecimal | Pi () |
Related Functions | |
(Note that these are not member functions.) | |
BPSCORE_EXPORT BpsDecimal | operator- (const BpsDecimal &aValue) |
Unary prefix minus operator. More... | |
BPSCORE_EXPORT QDataStream & | operator<< (QDataStream &aStream, const BpsDecimal &aDecimal) |
Write the binary code of the decimal value to a data stream. More... | |
BPSCORE_EXPORT QDebug | operator<< (QDebug aDebug, const BpsDecimal &aDecimal) |
Write the string representation of a decimal value to a debug stream. More... | |
BPSCORE_EXPORT QTextStream & | operator<< (QTextStream &aStream, const BpsDecimal &aDecimal) |
Write the string representation of the decimal value to a text stream. More... | |
BPSCORE_EXPORT std::ostream & | operator<< (std::ostream &aStream, const BpsDecimal &aDecimal) |
Write the string representation of the decimal value to a std::ostream. More... | |
BPSCORE_EXPORT QDataStream & | operator>> (QDataStream &aStream, BpsDecimal &aDecimal) |
Read the binary code of the decimal value from a data stream. More... | |
BPSCORE_EXPORT uint | qHash (const BpsDecimal &aValue, uint aSeed) |
Returns a hash value, as required by QHash or QSet. More... | |
A class for decimal arithmetics.
The arithmetic of the standard floating point values is mathematically correct, however for many applications the results are inconvenient. It becomes then an issue when real (non-integer) numbers are involved in calculations:
Despite the result your computer is not broken, nor does it calculate inexact. The reason for this surprising output is that standard floating point works binary and with a finite number of digits, and there is a difference when the last digit must be rounded in calculations. Often you won't notice a delta like in the example above, but because it happens and gets multiplied with the number of operations, it's a serious problem even if the final result is rounded to few decimal places and so seems correct. But one day there could be a case where it is not.
And now the same calculation with the decimal class:
USAGE HINTS
In calculations where a BpsDecimal is present, the other operand will automatically be converted to a BpsDecimal, and so the term and finally the overall result is a BpsDecimal:
While decimal arithmetic is best suited for commercial or financial purposes, binary arithmetic is faster because it is the native number representation for computers and is handled by hardware floating point processors. Decimal arithmetic would perform bad for heavy number crunching like in graphic libraries for example. (No rule without exception however: mainframes have decimal hardware processors, but you will most likely not use mainframes as client PC's).
QVARIANT INTEGRATION
BpsDecimal is integrated to QVariant to enable QDebug output, conversion to string and regular numerics, and to compare for equality:
Also, BpsDecimal can be streamed by QMetaType::load() and QMetaType::save(), which is used for example when QVariant is loaded or saved by QSettings.
IMPLEMENTATION AND CREDITS
BpsDecimal uses IBM's decNumber library to implement a numeric variable according to the General Decimal Arithmetic Specification in ANSI C. This specification defines a decimal arithmetic which meets the requirements of commercial, financial, and human-oriented applications. It also matches the decimal arithmetic in the IEEE 754 Standard for Floating Point Arithmetic.
BpsDecimal encapsulates decQuad and uses decimal128, a 128-bit decimal floating-point representation, to provide 34 decimal digits of precision in a compressed format. A BpsDecimal variable occupies 16 bytes.
Many thanks to IBM and IBM Fellow Mike Cowlishaw from the UK Laboratories, for making decNumber package widely available under the terms of ICU v1.8.1.
BpsDecimal::BpsDecimal | ( | const BpsDecimal & | aOther | ) |
The copy constructor.
aOther | The other number value to copy from. |
BpsDecimal::BpsDecimal | ( | qint32 | aValue | ) |
Construct from a signed 32 bit integer value.
aValue | The integer value to copy from. |
BpsDecimal::BpsDecimal | ( | quint32 | aValue | ) |
Construct from a unsigned 32 bit integer value.
aValue | The integer value to copy from. |
BpsDecimal::BpsDecimal | ( | qlonglong | aValue | ) |
Construct from a 64 bit signed integer value.
aValue | The integer value to copy from. |
BpsDecimal::BpsDecimal | ( | qulonglong | aValue | ) |
Construct from a 64 bit unsigned integer value.
aValue | The integer value to copy from. |
BpsDecimal::BpsDecimal | ( | double | aValue | ) |
Construct from a double value.
aValue | The double value to copy from. |
BpsDecimal::BpsDecimal | ( | const QString & | aString | ) |
Construct from a decimal string representation.
The string must follow the syntax: [+|-]integer-digits[.fraction-digits][e|E[+|-]exponent-digits]
aString | The string to interpret as decimal number. |
BpsDecimal::BpsDecimal | ( | const QByteArray & | aString | ) |
Construct from a decimal string representation.
The string must follow the syntax: [+|-]integer-digits[.fraction-digits][e|E[+|-]exponent-digits]
aString | The byte array to interpret as decimal number. The text is expected as 0-terminated string in constData(). |
BpsDecimal::BpsDecimal | ( | const char * | aString | ) |
Construct from a decimal string representation.
The string must follow the syntax: [+|-]integer-digits[.fraction-digits][e|E[+|-]exponent-digits]
aString | The 0-terminated string. |
BpsDecimal::BpsDecimal | ( | const QVariant & | aVariant | ) |
Construct from a QVariant.
If the variant type is any numeric or string, the respective conversions are used. If the variant is invalid, null or not any numeric or string, the targets value is set to 0.
aVariant | The source variant. |
BpsDecimal BpsDecimal::abs | ( | ) | const |
BpsDecimal BpsDecimal::ceil | ( | ) | const |
If the value is not integer, round up to next integer (towards +Infinity).
BpsDecimal BpsDecimal::div | ( | const BpsDecimal & | aOther | ) | const |
Divides this value by the other value and returns the integer part of the result.
aOther | The other value to divide by. |
BpsDecimal BpsDecimal::down | ( | ) | const |
If the value is not integer, round down to next integer (towards 0).
BpsDecimal BpsDecimal::exp | ( | ) | const |
Calculate e to the power of this value and return the result.
BpsDecimal BpsDecimal::floor | ( | ) | const |
If the value is not integer, round down to next integer (towards -Infinity).
|
static |
Convert a string to a decimal value.
aString | The string to convert. |
aOk | If not 0, *aOk is set true when conversion succeeded and false when conversion failed. |
|
static |
Convert a string to a decimal value.
aString | The string to convert. |
aOk | If not 0, *aOk is set true when conversion succeeded and false when conversion failed. |
|
static |
Convert a string to a decimal value.
aString | The string to convert. |
aOk | If not 0, *aOk is set true when conversion succeeded and false when conversion failed. |
bool BpsDecimal::isFinite | ( | ) | const |
Check if value is neither infinite nor a NaN.
bool BpsDecimal::isInfinite | ( | ) | const |
Check if value is infinite.
bool BpsDecimal::isInteger | ( | ) | const |
Check if value is finite and has exponent=0.
bool BpsDecimal::isNaN | ( | ) | const |
Check if value is not a number.
bool BpsDecimal::isNegative | ( | ) | const |
Check if value is negative.
BpsDecimal BpsDecimal::ln | ( | ) | const |
Calculate the natural logarithm (to base e) of this value and return the result.
BpsDecimal BpsDecimal::log10 | ( | ) | const |
Calculate the logarithm to base ten of this value and return the result.
BpsDecimal BpsDecimal::max | ( | const BpsDecimal & | aOther | ) | const |
Compare this value with the other and return the larger (nearer towards +Infinity).
aOther | The other value to compare. |
|
static |
BpsDecimal BpsDecimal::min | ( | const BpsDecimal & | aOther | ) | const |
Compare this value with the other and return the smaller (nearer towards -Infinity).
aOther | The other value to compare. |
BpsDecimal::operator QVariant | ( | ) | const |
Automatic conversion to QVariant.
bool BpsDecimal::operator!= | ( | const BpsDecimal & | aOther | ) | const |
Compare with other value for un-equality.
aOther | The other value to compare to. |
BpsDecimal BpsDecimal::operator% | ( | const BpsDecimal & | aOther | ) | const |
Integer divide by other value and return remainder as result.
aOther | The other value to divide by. |
BpsDecimal& BpsDecimal::operator%= | ( | const BpsDecimal & | aOther | ) |
Integer divide by other value and set remainder as value.
aOther | The other value to divide by. |
BpsDecimal BpsDecimal::operator* | ( | const BpsDecimal & | aOther | ) | const |
Multiply with other value and return result.
aOther | The other value to multiply. |
BpsDecimal& BpsDecimal::operator*= | ( | const BpsDecimal & | aOther | ) |
Multiply with other value.
aOther | The other value. |
BpsDecimal BpsDecimal::operator+ | ( | const BpsDecimal & | aOther | ) | const |
Add two values and return result.
aOther | The other value to add. |
BpsDecimal& BpsDecimal::operator++ | ( | ) |
Prefix increment the value by one.
BpsDecimal BpsDecimal::operator++ | ( | int | ) |
Postfix increment the value by one.
BpsDecimal& BpsDecimal::operator+= | ( | const BpsDecimal & | aOther | ) |
Add other value.
aOther | The other value to add. |
BpsDecimal BpsDecimal::operator- | ( | const BpsDecimal & | aOther | ) | const |
Subtract second value and return result.
aOther | The other value to subtract. |
BpsDecimal& BpsDecimal::operator-- | ( | ) |
Prefix decrement the value by one.
BpsDecimal BpsDecimal::operator-- | ( | int | ) |
Postfix decrement the value by one.
BpsDecimal& BpsDecimal::operator-= | ( | const BpsDecimal & | aOther | ) |
Subtract second value.
aOther | The other value to subtract. |
BpsDecimal BpsDecimal::operator/ | ( | const BpsDecimal & | aOther | ) | const |
Divide by other value and return result.
aOther | The other value to divide by. |
BpsDecimal& BpsDecimal::operator/= | ( | const BpsDecimal & | aOther | ) |
Divide by other value.
aOther | The other value to divide by. |
bool BpsDecimal::operator< | ( | const BpsDecimal & | aOther | ) | const |
Compare if less than other value.
aOther | The other value to compare to. |
bool BpsDecimal::operator<= | ( | const BpsDecimal & | aOther | ) | const |
Compare if less than or equal to other value.
aOther | The other value to compare to. |
BpsDecimal& BpsDecimal::operator= | ( | const BpsDecimal & | aOther | ) |
Assign value from an other decimal.
aOther | The other value to copy. |
BpsDecimal& BpsDecimal::operator= | ( | const char * | aString | ) |
Assign value from a string.
Construct from a decimal string representation. The string must follow the syntax: [+|-]integer-digits[.fraction-digits][e|E[+|-]exponent-digits]
aString | The 0-terminated string. |
BpsDecimal& BpsDecimal::operator= | ( | const QByteArray & | aString | ) |
Assign value from a string.
Construct from a decimal string representation. The string must follow the syntax: [+|-]integer-digits[.fraction-digits][e|E[+|-]exponent-digits]
aString | The byte array to interpret as decimal number. The text is expected as 0-terminated string in constData(). |
BpsDecimal& BpsDecimal::operator= | ( | const QString & | aString | ) |
Assign value from a string.
Construct from a decimal string representation. The string must follow the syntax: [+|-]integer-digits[.fraction-digits][e|E[+|-]exponent-digits]
aString | The string to interpret as decimal number. |
BpsDecimal& BpsDecimal::operator= | ( | const QVariant & | aVariant | ) |
Assign value from a QVariant.
If the variant type is any numeric or string, the respective conversions are used. If the variant is invalid, null or not any numeric or string, the targets value is set to 0.
aVariant | The source variant. |
BpsDecimal& BpsDecimal::operator= | ( | double | aOther | ) |
Assign value from a double.
aOther | The other value to copy. |
BpsDecimal& BpsDecimal::operator= | ( | qint32 | aOther | ) |
Assign value from a 32 bit signed integer.
aOther | The other value to copy. |
BpsDecimal& BpsDecimal::operator= | ( | qlonglong | aOther | ) |
Assign value from a 64 bit signed integer.
aOther | The other value to copy. |
BpsDecimal& BpsDecimal::operator= | ( | quint32 | aOther | ) |
Assign value from a 32 bit unsigned integer.
aOther | The other value to copy. |
BpsDecimal& BpsDecimal::operator= | ( | qulonglong | aOther | ) |
Assign value from a 64 bit unsigned integer.
aOther | The other value to copy. |
bool BpsDecimal::operator== | ( | const BpsDecimal & | aOther | ) | const |
Compare with other value for equality.
aOther | The other value to compare to. |
bool BpsDecimal::operator> | ( | const BpsDecimal & | aOther | ) | const |
Compare if greater than other value.
aOther | The other value to compare to. |
bool BpsDecimal::operator>= | ( | const BpsDecimal & | aOther | ) | const |
Compare if greater than or equal to other value.
aOther | The other value to compare to. |
|
static |
BpsDecimal BpsDecimal::power | ( | const BpsDecimal & | aOther | ) | const |
Raises this value to the power of the other value and returns the result.
aOther | The other value. |
BpsDecimal BpsDecimal::rounded | ( | int | aDecPlaces | ) | const |
Round to an number of decimal places.
aDecPlaces | Number of decimal places to round to. |
BpsDecimal BpsDecimal::sqrt | ( | ) | const |
Calculate the square root of this value and return the result.
double BpsDecimal::toDouble | ( | ) | const |
Get the value as double.
qint32 BpsDecimal::toInt | ( | ) | const |
Get the value as signed 32 bit integer.
The value is rounded to an integer if necessary. If x is infinite, is a NaN, or after rounding is outside the range of the result, then StatusInvalidOperation is set. The StatusInexact flag is not set by this function, even if rounding occurred.
qlonglong BpsDecimal::toLongLong | ( | ) | const |
Get the value as 64 bit signed integer.
The value is rounded to an integral value.
QString BpsDecimal::toString | ( | ) | const |
Converts a decimal value to a string.
Finite numbers are converted to a string of format [-]9.9 if the final string is 20 characters or less, and to exponential notation otherwise.
Note that strings which are not regular numbers (one of "Infinity", "-Infinity", "NaN") are possible.
QString BpsDecimal::toString | ( | int | aDecPlaces, |
int | aMaxSize = 36 |
||
) | const |
Converts a decimal value to a string of format [-]9.9.
The printed value is rounded to the absolute number of decimal places.
aDecPlaces | Number of decimal places. |
aMaxSize | Maximum string size. |
quint32 BpsDecimal::toUInt | ( | ) | const |
Get the value as unsigned 32 bit integer.
The value is rounded to an integer if necessary. If x is infinite, is a NaN, or after rounding is outside the range of the result, then StatusInvalidOperation is set. The StatusInexact flag is not set by this function, even if rounding occurred.
qulonglong BpsDecimal::toULongLong | ( | ) | const |
Get the value as 64 bit unsigned integer.
The value is rounded to an integral value >= 0.
QVariant BpsDecimal::toVariant | ( | ) | const |
Explicit conversion to QVariant.
BpsDecimal BpsDecimal::up | ( | ) | const |
If the value is not integer, round up to next integer (away from 0).
|
related |
Unary prefix minus operator.
aValue | The decimal to get the negative value of. |
|
related |
Write the binary code of the decimal value to a data stream.
aStream | The stream to write to. |
aDecimal | The BpsDecimal value. |
|
related |
Write the string representation of a decimal value to a debug stream.
aDebug | The stream to write to. |
aDecimal | The BpsDecimal value. |
|
related |
Write the string representation of the decimal value to a text stream.
aStream | The stream to write to. |
aDecimal | The BpsDecimal value. |
|
related |
Write the string representation of the decimal value to a std::ostream.
aStream | The stream to write to. |
aDecimal | The BpsDecimal value. |
|
related |
Read the binary code of the decimal value from a data stream.
aStream | The stream to read from. |
aDecimal | The BpsDecimal value. |
|
related |