tntdb::Decimal Class Reference

This class holds a decimal floating point number. More...

#include <decimal.h>

List of all members.

Low level arithmetic methods

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 &quotient, 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.

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.
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.
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 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 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.


Detailed Description

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.


Member Enumeration Documentation

Flags used for denoting positive or negative, infinity and not a number.

Enumerator:
positive  Set if this Decimal is positive, else this Decimal is negative.
infinity  Set if this Decimal is positive or negative infinity.
negativeInfinity  Negative infinity.
positiveInfinity  Positive infinity.
NaN  Not a Number.

How infinity is printed on output.

Enumerator:
infinityShort  Affects output only, print Inf or -Inf.
infinityLong  Affects output only, print Infinity or -Infinity instead of Inf or -Inf.
infinityTilde  Affects output only, print ~ or -~ (as used in Oracle) instead of Inf or -Inf.

Rounding algorithm.

Enumerator:
truncate  1.1, 1.5, and 1.6 and all rounded down to 1.0.
round  1.1 is rounded down to 1.0, 1.5, and 1.6 are rounded up to 2.0.
bankersRound  1.1 and 1.5 are rounded down to 1.0, 1.6 is rounded up to 2.0.


Constructor & Destructor Documentation

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.

Parameters:
value double to initialize it with.

tntdb::Decimal::Decimal ( int64_t  man,
ExponentType  exp 
)

Initialize this Decimal-object with the given decimal mantissa and exponent.

Parameters:
man integer decimal mantissa value to set this Decimal number to.
exp integer base 10 exponent to set this Decimal number to.

tntdb::Decimal::Decimal ( MantissaType  man,
ExponentType  exp,
FlagsType  f,
PrintFlagsType  pf = infinityShort 
)

Initializes the Decimal-object with the given MantissaType mantissa and ExponentType exponent.

Parameters:
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


Member Function Documentation

MantissaType tntdb::Decimal::getMantissa (  )  const

Return the decimal mantissa.

Returns:
the decimal mantissa.

ExponentType tntdb::Decimal::getExponent (  )  const

Return the base 10 exponent.

Returns:
the base 10 exponent.

bool tntdb::Decimal::isPositive (  )  const [inline]

Is this Decimal number positive?

Returns:
true if this Decimal number is positive, else return false.

bool tntdb::Decimal::isInfinity (  )  const

Is this Decimal number positive or negative infinity?

Returns:
true if this Decimal number is either positive or negative infinity, else return false.

bool tntdb::Decimal::isInfinity ( bool  positiveInfinity  )  const

Is this Decimal number positive or negative infinity?

Parameters:
positiveInfinity 
Returns:
If postitiveInfinity is true, then return true if this Decimal number is postitive infinity. Else if postitiveInfinity is false, then return true if this Decimal number is negative infinity. Else return false.

bool tntdb::Decimal::isNaN (  )  const

Is this Decimal not a number?

Returns:
true if this Decimal number is Not a Number. Else return false.

bool tntdb::Decimal::isZero (  )  const

Is this Decimal number zero?

Returns:
true if this Number object is zero, else return false.

template<typename ManType>
void tntdb::Decimal::getIntegralFractionalExponent ( ManType &  integral,
ManType &  fractional,
ExponentType &  ex,
ExponentType  optionalUserSpecifiedExponentOffset = 0 
) const throw (std::overflow_error) [inline]

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.

Parameters:
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.
Exceptions:
std::overflow_error if the result will not fit

template<typename IntegerType>
IntegerType tntdb::Decimal::numberOfDigits ( IntegerType  n  )  const [inline]

Return the number of decimal digits in n.

template<typename IntegerType>
IntegerType tntdb::Decimal::getInteger ( RoundingAlgorithmType  roundingAlgorithm = round  )  const throw (std::overflow_error) [inline]

Return this number as a C++ integer type.

Parameters:
roundingAlgorithm Decimal::RoundingAlgorithmType
Returns:
integer result if the result will fit.
Exceptions:
std::overflow_error if the result will not fit

template<typename FloatingPointType>
FloatingPointType tntdb::Decimal::getFloatingPoint (  )  const [inline]

Return this number as a C++ floating point type.

Returns:
binary floating point result, which is computed with binary floating point arithmetic, and hence is an approximation.

int tntdb::Decimal::getInt (  )  const throw (std::overflow_error) [inline]

Return this decimal number rounded as a C++ int.

Returns:
int result if the result will fit.
Exceptions:
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.

Returns:
int32_t result if the result will fit.
Exceptions:
std::overflow_error if the result will not fit

unsigned tntdb::Decimal::getUnsigned (  )  const throw (std::overflow_error) [inline]

Return this decimal number rounded as a C++ unsigned.

Returns:
unsigned result if the result will fit.
Exceptions:
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.

Returns:
uint32_t result if the result will fit.
Exceptions:
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.

Returns:
int64_t result if the result will fit.
Exceptions:
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.

Returns:
uint64_t number, if the result will fit.
Exceptions:
std::overflow_error if the result will not fit

float tntdb::Decimal::getFloat (  )  const [inline]

Convert to a C++ float.

Returns:
float result, which is computed with binary floating point arithmetic, and hence is an approximation.

double tntdb::Decimal::getDouble (  )  const [inline]

Convert to a C++ double.

Returns:
double result, which is computed with binary floating point arithmetic, and hence is an approximation.

template<typename IntegerType>
void tntdb::Decimal::setInteger ( IntegerType  num  )  [inline]

Set this this tntdb::Decimal object to the value of the given integer type.

Parameters:
num integer value to set this Decimal number to.

template<typename FloatingPointType>
void tntdb::Decimal::setFloatingPoint ( FloatingPointType  num  )  [inline]

Set this this tntdb::Decimal object to the value of the given floating point type.

Parameters:
num floating point value to set this Decimal number to.

template<typename IntegerType>
void tntdb::Decimal::setDecimalInteger ( IntegerType  num,
int32_t  exponent 
) [inline]

Set this this tntdb::Decimal object to the value of the given integer type mantissa and base 10 exponent.

Parameters:
num integer decimal mantissa value to set this Decimal number to.
exponent integer base 10 exponent 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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
num integer value to set this Decimal number to.

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.

Parameters:
num decimal mantissa integer value to set this Decimal number to.
exp 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.

Parameters:
num decimal mantissa integer value to set this Decimal number to.
exp base 10 exponent.

void tntdb::Decimal::setFloat ( float  num  )  [inline]

Set this this tntdb::Decimal object to the value of the given float.

Parameters:
num float value to set this Decimal number to.

void tntdb::Decimal::setDouble ( double  num  )  [inline]

Set this this tntdb::Decimal object to the value of the given double.

Parameters:
num double value to set this Decimal number to.

std::string tntdb::Decimal::toString (  )  const

Return this Decimal number as a string.

Returns:
string representation of this decimal number.

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.

Parameters:
out output stream

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.

Parameters:
out output stream
printFlags the optional printFlags only affect how positive and negative infinity are printed.

std::istream& tntdb::Decimal::read ( std::istream &  in,
bool  ignoreOverflowReadingFraction = false 
)

Read a Decimal number.

Parameters:
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);

template<typename T>
bool tntdb::Decimal::overflowDetectedInMultiplyByTen ( T &  n  )  [inline, static]

Multiply an integer type by 10, checking for overflow.

Parameters:
n on input: the number to multply by 10, on output, the result of the multiplication by 10.
Returns:
true if overflow detected, else false.

template<typename ManType>
void tntdb::Decimal::divideByPowerOfTen ( const ManType  dividend,
ManType &  quotient,
ManType &  remainder,
ManType  divisorPowerOfTenDigits 
) throw (std::overflow_error) [inline, static]

Divide an integer type by a power of 10 divisor, checking for overflow.

Parameters:
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.
Returns:
true if overflow detected, else false.
Exceptions:
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.

Parameters:
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

static void tntdb::Decimal::printFraction ( std::ostream &  out,
ExponentType  fracDigits,
MantissaType  fractional 
) [static, protected]

Print fraction.


The documentation for this class was generated from the following file:

Generated on Mon Jun 16 10:08:35 2008 for tntdb by  doxygen 1.5.6