tntdb 1.1
|
This class holds a decimal floating point number. More...
#include <decimal.h>
Public Types | |
enum | { Base = 10 } |
enum | FlagsTypeEnum { positive = 0x01, infinity = 0x02, negativeInfinity = infinity, positiveInfinity = infinity | positive, NaN = 0x04 } |
Flags used for denoting positive or negative, infinity and not a number. More... | |
enum | InfinityOutputType { infinityShort, infinityLong, infinityTilde } |
How infinity is printed on output. More... | |
enum | RoundingAlgorithmType { truncate, round, bankersRound } |
Rounding algorithm. More... | |
typedef uint64_t | MantissaType |
typedef int32_t | ExponentType |
typedef int8_t | FlagsType |
typedef int8_t | PrintFlagsType |
Public Member Functions | |
Decimal () | |
Initializes the Decimal-object with empty values. | |
Decimal (double value) | |
Initializes the Decimal-object with the given double value. | |
Decimal (int64_t man, ExponentType exp) | |
Initialize this Decimal-object with the given decimal mantissa and exponent. | |
Decimal (MantissaType man, ExponentType exp, FlagsType f, PrintFlagsType pf=infinityShort) | |
Initializes the Decimal-object with the given MantissaType mantissa and ExponentType exponent. | |
MantissaType | getMantissa () const |
Return the decimal mantissa. | |
ExponentType | getExponent () const |
Return the base 10 exponent. | |
bool | isPositive () const |
Is this Decimal number positive? | |
bool | isInfinity () const |
Is this Decimal number positive or negative infinity? | |
bool | isInfinity (bool positiveInfinity) const |
Is this Decimal number positive or negative infinity? | |
bool | isNaN () const |
Is this Decimal not a number? | |
bool | isZero () const |
Is this Decimal number zero? | |
template<typename ManType > | |
void | getIntegralFractionalExponent (ManType &integral, ManType &fractional, ExponentType &ex, ExponentType optionalUserSpecifiedExponentOffset=0) const throw (std::overflow_error) |
Split this decimal number into integral part, fractional and exponent parts. | |
template<typename IntegerType > | |
IntegerType | numberOfDigits (IntegerType n) const |
Return the number of decimal digits in n. | |
template<typename IntegerType > | |
IntegerType | getInteger (RoundingAlgorithmType roundingAlgorithm=round) const throw (std::overflow_error) |
Return this number as a C++ integer type. | |
template<typename FloatingPointType > | |
FloatingPointType | getFloatingPoint () const |
Return this number as a C++ floating point type. | |
int | getInt () const throw (std::overflow_error) |
Return this decimal number rounded as a C++ int. | |
long | getLong () const throw (std::overflow_error) |
Return this decimal number rounded as a C++ long. | |
int32_t | getInt32 () const throw (std::overflow_error) |
Return this decimal number rounded as a C++ int32_t. | |
unsigned | getUnsigned () const throw (std::overflow_error) |
Return this decimal number rounded as a C++ unsigned. | |
unsigned long | getUnsignedLong () const throw (std::overflow_error) |
Return this decimal number rounded as a C++ unsigned long. | |
uint32_t | getUnsigned32 () const throw (std::overflow_error) |
Return this decimal number rounded as a C++ uint32_t. | |
int64_t | getInt64 () const throw (std::overflow_error) |
Return this decimal number rounded as a C++ int64_t. | |
uint64_t | getUnsigned64 () const throw (std::overflow_error) |
Return this decimal number rounded as a C++ uint64_t. | |
float | getFloat () const |
Convert to a C++ float. | |
double | getDouble () const |
Convert to a C++ double. | |
template<typename IntegerType > | |
void | setInteger (IntegerType num) |
Set this this tntdb::Decimal object to the value of the given integer type. | |
template<typename FloatingPointType > | |
void | setFloatingPoint (FloatingPointType num) |
Set this this tntdb::Decimal object to the value of the given floating point type. | |
template<typename IntegerType > | |
void | setDecimalInteger (IntegerType num, int32_t exponent) |
Set this this tntdb::Decimal object to the value of the given integer type mantissa and base 10 exponent. | |
void | setInt (int num) |
Set this this tntdb::Decimal object to the value of the given int. | |
void | setLong (long num) |
Set this this tntdb::Decimal object to the value of the given long. | |
void | setInt32 (int32_t num) |
Set this this tntdb::Decimal object to the value of the given int32_t. | |
void | setUnsigned (unsigned num) |
Set this this tntdb::Decimal object to the value of the given unsigned. | |
void | setUnsignedLong (unsigned long num) |
Set this this tntdb::Decimal object to the value of the given unsigned. | |
void | setUnsigned32 (uint32_t num) |
Set this this tntdb::Decimal object to the value of the given uint32_t. | |
void | setInt64 (int64_t num) |
Set this this tntdb::Decimal object to the value of the given int64_t. | |
void | setUnsigned64 (uint64_t num) |
Set this this tntdb::Decimal object to the value of the given uint64_t. | |
void | setDecimalInt64 (int64_t num, int32_t exp) |
Set this this tntdb::Decimal object to the value of the given int64_t decimal mantissa and base 10 exponent. | |
void | setDecimalUnsigned64 (uint64_t num, int32_t exp) |
Set this this tntdb::Decimal object to the value of the given uint64_t decimal mantissa and base 10 exponent. | |
void | setFloat (float num) |
Set this this tntdb::Decimal object to the value of the given float. | |
void | setDouble (double num) |
Set this this tntdb::Decimal object to the value of the given double. | |
std::string | toString () const |
Return this Decimal number as a string. | |
std::ostream & | print (std::ostream &out) const |
Print this Decimal number. | |
std::ostream & | print (std::ostream &out, PrintFlagsType printFlags) const |
Print this Decimal number. | |
std::istream & | read (std::istream &in, bool ignoreOverflowReadingFraction=false) |
Read a Decimal number. | |
Low level arithmetic methods | |
void | normalize () |
Multiply an integer type by 10, checking for overflow. | |
bool | operator== (const tntdb::Decimal &other) const |
Multiply an integer type by 10, checking for overflow. | |
bool | operator!= (const tntdb::Decimal &other) const |
Multiply an integer type by 10, checking for overflow. | |
bool | operator< (const tntdb::Decimal &other) const |
Multiply an integer type by 10, checking for overflow. | |
bool | operator> (const tntdb::Decimal &other) const |
Multiply an integer type by 10, checking for overflow. | |
bool | operator<= (const tntdb::Decimal &other) const |
Multiply an integer type by 10, checking for overflow. | |
bool | operator>= (const tntdb::Decimal &other) const |
Multiply an integer type by 10, checking for overflow. | |
template<typename T > | |
static bool | overflowDetectedInMultiplyByTen (T &n) |
Multiply an integer type by 10, checking for overflow. | |
template<typename ManType > | |
static void | divideByPowerOfTen (const ManType dividend, ManType "ient, ManType &remainder, ManType divisorPowerOfTenDigits) throw (std::overflow_error) |
Divide an integer type by a power of 10 divisor, checking for overflow. | |
void | init (MantissaType m, ExponentType e, FlagsType f=positive, PrintFlagsType pf=infinityShort) |
Initialize this Decimal number, called by the constructors. | |
static void | printFraction (std::ostream &out, ExponentType fracDigits, MantissaType fractional) |
Print fraction. |
This class holds a decimal floating point number.
It is necessary to convert it to some other type to perform arithmetic and other operations. Decimal is implemented with a 64 bit unsigned integer mantissa, and a 32 bit exponent. For conversions from integer types, strings, and reading from the database, Decimal tries to maintain accuracy if the result will fit in the 64 bit unsigned mantissa and exponent. When converting to integer types, Decimal will throw std::overflow exception if the result will not fit. For converting to binary floating point numbers, binary floating point is used in the conversion, and the result is an approximation.
Flags used for denoting positive or negative, infinity and not a number.
tntdb::Decimal::Decimal | ( | ) |
Initializes the Decimal-object with empty values.
tntdb::Decimal::Decimal | ( | double | value | ) | [explicit] |
Initializes the Decimal-object with the given double value.
value | double to initialize it with. |
tntdb::Decimal::Decimal | ( | int64_t | man, |
ExponentType | exp | ||
) |
tntdb::Decimal::Decimal | ( | MantissaType | man, |
ExponentType | exp, | ||
FlagsType | f, | ||
PrintFlagsType | pf = infinityShort |
||
) |
Initializes the Decimal-object with the given MantissaType mantissa and ExponentType exponent.
man | integer decimal mantissa value to set this Decimal number to. |
exp | integer base 10 exponent to set this Decimal number to. |
f | the flags, need to specify positive or negative. Decimal::FlagsTypeEnum |
pf | the print flags for infinity and not a number. Decimal::InfinityOutputType |
void tntdb::Decimal::divideByPowerOfTen | ( | const ManType | dividend, |
ManType & | quotient, | ||
ManType & | remainder, | ||
ManType | divisorPowerOfTenDigits | ||
) | throw (std::overflow_error) [static] |
Divide an integer type by a power of 10 divisor, checking for overflow.
dividend | the integer dividend. |
quotient | the result of the division by the power of 10 divisor. |
remainder | the remainder result of the division by the power of 10 divisor. |
divisorPowerOfTenDigits | divide by 10^divisorPowerOfTenDigits. |
std::overflow_error | if the result will not fit |
double tntdb::Decimal::getDouble | ( | ) | const [inline] |
Convert to a C++ double.
ExponentType tntdb::Decimal::getExponent | ( | ) | const |
Return the base 10 exponent.
float tntdb::Decimal::getFloat | ( | ) | const [inline] |
Convert to a C++ float.
FloatingPointType tntdb::Decimal::getFloatingPoint | ( | ) | const |
Return this number as a C++ floating point type.
int tntdb::Decimal::getInt | ( | ) | const throw (std::overflow_error) [inline] |
Return this decimal number rounded as a C++ int.
std::overflow_error | if the result will not fit |
int32_t tntdb::Decimal::getInt32 | ( | ) | const throw (std::overflow_error) [inline] |
Return this decimal number rounded as a C++ int32_t.
std::overflow_error | if the result will not fit |
int64_t tntdb::Decimal::getInt64 | ( | ) | const throw (std::overflow_error) [inline] |
Return this decimal number rounded as a C++ int64_t.
std::overflow_error | if the result will not fit |
IntegerType tntdb::Decimal::getInteger | ( | RoundingAlgorithmType | roundingAlgorithm = round | ) | const throw (std::overflow_error) |
Return this number as a C++ integer type.
roundingAlgorithm | Decimal::RoundingAlgorithmType |
std::overflow_error | if the result will not fit |
void tntdb::Decimal::getIntegralFractionalExponent | ( | ManType & | integral, |
ManType & | fractional, | ||
ExponentType & | ex, | ||
ExponentType | optionalUserSpecifiedExponentOffset = 0 |
||
) | const throw (std::overflow_error) |
Split this decimal number into integral part, fractional and exponent parts.
An optional user specified exponent offset can be used to first scale the decimal number.
integral | the part of the decimal floating point number to the left of the decimal point. |
fractional | the part to the decimal floating point number to the right of the decimal point. |
ex | the exponent of the decimal floating point number |
optionalUserSpecifiedExponentOffset | optional user specified exponent offset can be used to first scale the decimal number. |
std::overflow_error | if the result will not fit |
long tntdb::Decimal::getLong | ( | ) | const throw (std::overflow_error) [inline] |
Return this decimal number rounded as a C++ long.
std::overflow_error | if the result will not fit |
MantissaType tntdb::Decimal::getMantissa | ( | ) | const |
Return the decimal mantissa.
unsigned tntdb::Decimal::getUnsigned | ( | ) | const throw (std::overflow_error) [inline] |
Return this decimal number rounded as a C++ unsigned.
std::overflow_error | if the result will not fit |
uint32_t tntdb::Decimal::getUnsigned32 | ( | ) | const throw (std::overflow_error) [inline] |
Return this decimal number rounded as a C++ uint32_t.
std::overflow_error | if the result will not fit |
uint64_t tntdb::Decimal::getUnsigned64 | ( | ) | const throw (std::overflow_error) [inline] |
Return this decimal number rounded as a C++ uint64_t.
std::overflow_error | if the result will not fit |
unsigned long tntdb::Decimal::getUnsignedLong | ( | ) | const throw (std::overflow_error) [inline] |
Return this decimal number rounded as a C++ unsigned long.
std::overflow_error | if the result will not fit |
void tntdb::Decimal::init | ( | MantissaType | m, |
ExponentType | e, | ||
FlagsType | f = positive , |
||
PrintFlagsType | pf = infinityShort |
||
) | [protected] |
Initialize this Decimal number, called by the constructors.
m | integer decimal mantissa value to set this Decimal number to. |
e | integer base 10 exponent to set this Decimal number to. |
f | the flags, need to specify positive or negative. Decimal::FlagsTypeEnum |
pf | the print flags for infinity and not a number. Decimal::InfinityOutputType |
bool tntdb::Decimal::isInfinity | ( | ) | const |
bool tntdb::Decimal::isInfinity | ( | bool | positiveInfinity | ) | const |
Is this Decimal number positive or negative infinity?
positiveInfinity |
bool tntdb::Decimal::isNaN | ( | ) | const |
bool tntdb::Decimal::isPositive | ( | ) | const [inline] |
bool tntdb::Decimal::isZero | ( | ) | const |
Is this Decimal number zero?
void tntdb::Decimal::normalize | ( | ) |
Multiply an integer type by 10, checking for overflow.
n | on input: the number to multply by 10, on output, the result of the multiplication by 10. |
IntegerType tntdb::Decimal::numberOfDigits | ( | IntegerType | n | ) | const |
Return the number of decimal digits in n.
bool tntdb::Decimal::operator!= | ( | const tntdb::Decimal & | other | ) | const [inline] |
Multiply an integer type by 10, checking for overflow.
n | on input: the number to multply by 10, on output, the result of the multiplication by 10. |
bool tntdb::Decimal::operator< | ( | const tntdb::Decimal & | other | ) | const |
Multiply an integer type by 10, checking for overflow.
n | on input: the number to multply by 10, on output, the result of the multiplication by 10. |
bool tntdb::Decimal::operator<= | ( | const tntdb::Decimal & | other | ) | const [inline] |
Multiply an integer type by 10, checking for overflow.
n | on input: the number to multply by 10, on output, the result of the multiplication by 10. |
bool tntdb::Decimal::operator== | ( | const tntdb::Decimal & | other | ) | const |
Multiply an integer type by 10, checking for overflow.
n | on input: the number to multply by 10, on output, the result of the multiplication by 10. |
bool tntdb::Decimal::operator> | ( | const tntdb::Decimal & | other | ) | const [inline] |
Multiply an integer type by 10, checking for overflow.
n | on input: the number to multply by 10, on output, the result of the multiplication by 10. |
bool tntdb::Decimal::operator>= | ( | const tntdb::Decimal & | other | ) | const [inline] |
Multiply an integer type by 10, checking for overflow.
n | on input: the number to multply by 10, on output, the result of the multiplication by 10. |
bool tntdb::Decimal::overflowDetectedInMultiplyByTen | ( | T & | n | ) | [static] |
Multiply an integer type by 10, checking for overflow.
n | on input: the number to multply by 10, on output, the result of the multiplication by 10. |
std::ostream& tntdb::Decimal::print | ( | std::ostream & | out, |
PrintFlagsType | printFlags | ||
) | const |
Print this Decimal number.
If out.precision() != 0, then this decimal number is printed with out.precision() significant digits.
out | output stream |
printFlags | the optional printFlags only affect how positive and negative infinity are printed. |
std::ostream& tntdb::Decimal::print | ( | std::ostream & | out | ) | const |
Print this Decimal number.
If out.precision() != 0, then this decimal number is printed with out.precision() significant digits.
out | output stream |
static void tntdb::Decimal::printFraction | ( | std::ostream & | out, |
ExponentType | fracDigits, | ||
MantissaType | fractional | ||
) | [static, protected] |
Print fraction.
std::istream& tntdb::Decimal::read | ( | std::istream & | in, |
bool | ignoreOverflowReadingFraction = false |
||
) |
Read a Decimal number.
in | input stream |
ignoreOverflowReadingFraction | if true, ignore overflow errors while reading the fractional part, and set the mantissa to the the value before the overflow. This is useful while converting from float or double values to Decimal. The full range of significant digits that the Decimal can hold can be read in the conversion like: std::ostringstream ostr; ostr.precision(24); ostr << num; read(ostr.str(), true); |
void tntdb::Decimal::setDecimalInt64 | ( | int64_t | num, |
int32_t | exp | ||
) | [inline] |
Set this this tntdb::Decimal object to the value of the given int64_t decimal mantissa and base 10 exponent.
num | decimal mantissa integer value to set this Decimal number to. |
exp | base 10 exponent. |
void tntdb::Decimal::setDecimalInteger | ( | IntegerType | num, |
int32_t | exponent | ||
) |
Set this this tntdb::Decimal object to the value of the given integer type mantissa and base 10 exponent.
void tntdb::Decimal::setDecimalUnsigned64 | ( | uint64_t | num, |
int32_t | exp | ||
) | [inline] |
Set this this tntdb::Decimal object to the value of the given uint64_t decimal mantissa and base 10 exponent.
num | decimal mantissa integer value to set this Decimal number to. |
exp | base 10 exponent. |
void tntdb::Decimal::setDouble | ( | double | num | ) | [inline] |
Set this this tntdb::Decimal object to the value of the given double.
num | double value to set this Decimal number to. |
void tntdb::Decimal::setFloat | ( | float | num | ) | [inline] |
Set this this tntdb::Decimal object to the value of the given float.
num | float value to set this Decimal number to. |
void tntdb::Decimal::setFloatingPoint | ( | FloatingPointType | num | ) |
Set this this tntdb::Decimal object to the value of the given floating point type.
num | floating point value to set this Decimal number to. |
void tntdb::Decimal::setInt | ( | int | num | ) | [inline] |
Set this this tntdb::Decimal object to the value of the given int.
num | integer value to set this Decimal number to. |
void tntdb::Decimal::setInt32 | ( | int32_t | num | ) | [inline] |
Set this this tntdb::Decimal object to the value of the given int32_t.
num | integer value to set this Decimal number to. |
void tntdb::Decimal::setInt64 | ( | int64_t | num | ) | [inline] |
Set this this tntdb::Decimal object to the value of the given int64_t.
num | integer value to set this Decimal number to. |
void tntdb::Decimal::setInteger | ( | IntegerType | num | ) |
Set this this tntdb::Decimal object to the value of the given integer type.
num | integer value to set this Decimal number to. |
void tntdb::Decimal::setLong | ( | long | num | ) | [inline] |
Set this this tntdb::Decimal object to the value of the given long.
num | integer value to set this Decimal number to. |
void tntdb::Decimal::setUnsigned | ( | unsigned | num | ) | [inline] |
Set this this tntdb::Decimal object to the value of the given unsigned.
num | integer value to set this Decimal number to. |
void tntdb::Decimal::setUnsigned32 | ( | uint32_t | num | ) | [inline] |
Set this this tntdb::Decimal object to the value of the given uint32_t.
num | integer value to set this Decimal number to. |
void tntdb::Decimal::setUnsigned64 | ( | uint64_t | num | ) | [inline] |
Set this this tntdb::Decimal object to the value of the given uint64_t.
num | integer value to set this Decimal number to. |
void tntdb::Decimal::setUnsignedLong | ( | unsigned long | num | ) | [inline] |
Set this this tntdb::Decimal object to the value of the given unsigned.
num | integer value to set this Decimal number to. |
std::string tntdb::Decimal::toString | ( | ) | const |
Return this Decimal number as a string.