Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

Decimal Class Reference

#include <tntdb/decimal.h>

List of all members.

Low level arithmetic methods

template<typename T>
static bool overflowDetectedInMultiplyByTen (T &n)
template<typename ManType>
static void divideByPowerOfTen (const ManType dividend, ManType &quotient, 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)


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

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.

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.

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

Decimal ( double  value  )  [explicit]

Initializes the Decimal-object with the given double value.

Parameters:
value double to initialize it with.

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.

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 getMantissa (  )  const

Return the decimal mantissa.

Returns:
the decimal mantissa.

ExponentType getExponent (  )  const

Return the base 10 exponent.

Returns:
the base 10 exponent.

bool isPositive (  )  const

Is this Decimal number positive?

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

References Decimal::positive.

bool 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 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 isNaN (  )  const

Is this Decimal not a number?

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

bool isZero (  )  const

Is this Decimal number zero?

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

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.

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

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.

Parameters:
roundingAlgorithm Decimal::RoundingAlgorithmType
Returns:
integer result if the result will fit.
Exceptions:
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.

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

References Decimal::positive.

int getInt (  )  const throw (std::overflow_error)

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 getInt32 (  )  const throw (std::overflow_error)

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 getUnsigned (  )  const throw (std::overflow_error)

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 getUnsigned32 (  )  const throw (std::overflow_error)

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 getInt64 (  )  const throw (std::overflow_error)

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 getUnsigned64 (  )  const throw (std::overflow_error)

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 getFloat (  )  const

Convert to a C++ float.

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

double getDouble (  )  const

Convert to a C++ double.

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

void setInteger ( IntegerType  num  ) 

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

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

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

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

void setInt ( int  num  ) 

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

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

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

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

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

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

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

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

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

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

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

std::string toString (  )  const

Return this Decimal number as a string.

Returns:
string representation of this decimal number.

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.

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

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

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

Referenced by Decimal::setFloatingPoint().

bool overflowDetectedInMultiplyByTen ( T &  n  )  [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.

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.

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

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.

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

Copyright © 2008 The Tntnet Development Team
Tntnet 1.6