#include <tntdb/decimal.h>
Low level arithmetic methods | |
| template<typename T> | |
| static bool | overflowDetectedInMultiplyByTen (T &n) |
| template<typename ManType> | |
| static void | divideByPowerOfTen (const ManType dividend, ManType "ient, ManType &remainder, ManType divisorPowerOfTenDigits) throw (std::overflow_error) |
| void | init (MantissaType m, ExponentType e, FlagsType f=positive, PrintFlagsType pf=infinityShort) |
| static void | printFraction (std::ostream &out, ExponentType fracDigits, MantissaType fractional) |
| Print fraction. | |
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) | |
| Decimal (int64_t man, ExponentType exp) | |
| Decimal (MantissaType man, ExponentType exp, FlagsType f, PrintFlagsType pf=infinityShort) | |
| MantissaType | getMantissa () const |
| ExponentType | getExponent () const |
| bool | isPositive () const |
| bool | isInfinity () const |
| bool | isInfinity (bool positiveInfinity) const |
| bool | isNaN () const |
| bool | isZero () const |
| template<typename ManType> | |
| void | getIntegralFractionalExponent (ManType &integral, ManType &fractional, ExponentType &ex, ExponentType optionalUserSpecifiedExponentOffset=0) const throw (std::overflow_error) |
| 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) |
| template<typename FloatingPointType> | |
| FloatingPointType | getFloatingPoint () const |
| int | getInt () const throw (std::overflow_error) |
| int32_t | getInt32 () const throw (std::overflow_error) |
| unsigned | getUnsigned () const throw (std::overflow_error) |
| uint32_t | getUnsigned32 () const throw (std::overflow_error) |
| int64_t | getInt64 () const throw (std::overflow_error) |
| uint64_t | getUnsigned64 () const throw (std::overflow_error) |
| float | getFloat () const |
| double | getDouble () const |
| template<typename IntegerType> | |
| void | setInteger (IntegerType num) |
| template<typename FloatingPointType> | |
| void | setFloatingPoint (FloatingPointType num) |
| template<typename IntegerType> | |
| void | setDecimalInteger (IntegerType num, int32_t exponent) |
| void | setInt (int num) |
| void | setInt32 (int32_t num) |
| void | setUnsigned (unsigned num) |
| void | setUnsigned32 (uint32_t num) |
| void | setInt64 (int64_t num) |
| void | setUnsigned64 (uint64_t num) |
| void | setDecimalInt64 (int64_t num, int32_t exp) |
| void | setDecimalUnsigned64 (uint64_t num, int32_t exp) |
| void | setFloat (float num) |
| void | setDouble (double num) |
| std::string | toString () const |
| std::ostream & | print (std::ostream &out) const |
| std::ostream & | print (std::ostream &out, PrintFlagsType printFlags) const |
| std::istream & | read (std::istream &in, bool ignoreOverflowReadingFraction=false) |
| enum FlagsTypeEnum |
| enum InfinityOutputType |
| Decimal | ( | double | value | ) | [explicit] |
Initializes the Decimal-object with the given double value.
| value | double to initialize it with. |
| Decimal | ( | int64_t | man, | |
| ExponentType | exp | |||
| ) |
| 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 |
| MantissaType getMantissa | ( | ) | const |
Return the decimal mantissa.
| ExponentType getExponent | ( | ) | const |
Return the base 10 exponent.
| bool isPositive | ( | ) | const |
Is this Decimal number positive?
References Decimal::positive.
| bool isInfinity | ( | ) | const |
| bool isInfinity | ( | bool | positiveInfinity | ) | const |
Is this Decimal number positive or negative infinity?
| positiveInfinity |
| bool isNaN | ( | ) | const |
| bool isZero | ( | ) | const |
Is this Decimal number zero?
| 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. 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 |
References Decimal::divideByPowerOfTen(), Decimal::overflowDetectedInMultiplyByTen(), and Decimal::positive.
| IntegerType 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 |
References Decimal::bankersRound, Decimal::positive, and Decimal::round.
| FloatingPointType getFloatingPoint | ( | ) | const |
Return this number as a C++ floating point type.
References Decimal::positive.
| int getInt | ( | ) | const throw (std::overflow_error) |
Return this decimal number rounded as a C++ int.
| std::overflow_error | if the result will not fit |
| int32_t getInt32 | ( | ) | const throw (std::overflow_error) |
Return this decimal number rounded as a C++ int32_t.
| std::overflow_error | if the result will not fit |
| unsigned getUnsigned | ( | ) | const throw (std::overflow_error) |
Return this decimal number rounded as a C++ unsigned.
| std::overflow_error | if the result will not fit |
| uint32_t getUnsigned32 | ( | ) | const throw (std::overflow_error) |
Return this decimal number rounded as a C++ uint32_t.
| std::overflow_error | if the result will not fit |
| int64_t getInt64 | ( | ) | const throw (std::overflow_error) |
Return this decimal number rounded as a C++ int64_t.
| std::overflow_error | if the result will not fit |
| uint64_t getUnsigned64 | ( | ) | const throw (std::overflow_error) |
Return this decimal number rounded as a C++ uint64_t.
| std::overflow_error | if the result will not fit |
| float getFloat | ( | ) | const |
Convert to a C++ float.
| double getDouble | ( | ) | const |
Convert to a C++ double.
| void 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. |
References Decimal::infinity, Decimal::NaN, and Decimal::positive.
| void 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. |
References Decimal::read().
| 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.
| num | integer value to set this Decimal number to. |
| void setInt32 | ( | int32_t | num | ) |
Set this this tntdb::Decimal object to the value of the given int32_t.
| num | integer value to set this Decimal number to. |
| void setUnsigned | ( | unsigned | num | ) |
Set this this tntdb::Decimal object to the value of the given unsigned.
| num | integer value to set this Decimal number to. |
| void setUnsigned32 | ( | uint32_t | num | ) |
Set this this tntdb::Decimal object to the value of the given uint32_t.
| num | integer value to set this Decimal number to. |
| void setInt64 | ( | int64_t | num | ) |
Set this this tntdb::Decimal object to the value of the given int64_t.
| num | integer value to set this Decimal number to. |
| void setUnsigned64 | ( | uint64_t | num | ) |
Set this this tntdb::Decimal object to the value of the given uint64_t.
| num | integer value to set this Decimal number to. |
| 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.
| num | decimal mantissa integer value to set this Decimal number to. | |
| exp | 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.
| num | decimal mantissa integer value to set this Decimal number to. | |
| exp | base 10 exponent. |
| void setFloat | ( | float | num | ) |
Set this this tntdb::Decimal object to the value of the given float.
| num | float value to set this Decimal number to. |
| void setDouble | ( | double | num | ) |
Set this this tntdb::Decimal object to the value of the given double.
| num | double value to set this Decimal number to. |
| std::string toString | ( | ) | const |
Return this Decimal number as a string.
| std::ostream& 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 |
| std::ostream& 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::istream& 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); |
Referenced by Decimal::setFloatingPoint().
| bool 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. |
Referenced by Decimal::divideByPowerOfTen(), Decimal::getIntegralFractionalExponent(), and Decimal::numberOfDigits().
| void 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 |
References Decimal::overflowDetectedInMultiplyByTen().
Referenced by Decimal::getIntegralFractionalExponent().
| void 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 |