21 #include "OnixS/CME/DropCopy/Export.h" 29 namespace OnixS {
namespace CME {
namespace DropCopy {
47 Decimal (DecimalMantissa mantissa = 0, DecimalExponent exponent = 0);
52 Decimal (Double value,
size_t precision);
58 DecimalMantissa mantissa()
const;
61 void mantissa (DecimalMantissa);
64 DecimalExponent exponent()
const;
67 void exponent (DecimalExponent);
76 bool operator <(
const Decimal&)
const;
79 bool operator >(
const Decimal&)
const;
84 operator Int32()
const;
94 operator Int64()
const;
109 bool toNumber (Int32&)
const;
114 bool toNumber (UInt32&)
const;
119 bool toNumber (Int64&)
const;
124 bool toNumber (UInt64&)
const;
128 bool toNumber (Double&)
const;
135 Double toDoubleUnchecked()
const;
142 static Decimal fromDoubleUnchecked(Double value,
size_t precision);
145 void toString(std::string&)
const;
148 std::string toString()
const;
156 static bool tryParse(
const char* buffer,
size_t bufferSize,
Decimal&);
160 static Decimal parse(
const char* buffer,
size_t bufferSize);
164 DecimalMantissa mantissa_;
167 DecimalExponent exponent_;
171 : mantissa_(mantissa)
172 , exponent_(exponent)
177 : mantissa_(other.mantissa_)
178 , exponent_(other.exponent_)
204 mantissa_ = other.mantissa_;
205 exponent_ = other.exponent_;
210 inline Decimal::operator
Int32()
const 217 throw std::domain_error(
"Cannot cast value to target type. ");
227 throw std::domain_error(
"Cannot cast value to target type. ");
230 inline Decimal::operator
Int64()
const 237 throw std::domain_error(
"Cannot cast value to target type. ");
247 throw std::domain_error(
"Cannot cast value to target type. ");
257 throw std::domain_error(
"Cannot cast value to target type. ");
262 std::string presentation;
267 namespace NumericDetails {
268 static const int maxAbsPower10 = 20;
269 static const int powers10Size = maxAbsPower10 * 2 + 1;
271 static const double powers10[] =
273 1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12, 1E-11,
274 1E-10, 1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1,
276 1E1, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8, 1E9, 1E10,
277 1E11, 1E12, 1E13, 1E14, 1E15, 1E16, 1E17, 1E18, 1E19, 1E20
282 const int offset = power + maxAbsPower10;
283 if(offset >= 0 && offset < powers10Size)
285 return powers10[offset];
288 throw std::invalid_argument(
"Invalid power argument, must be in range [-20, 20]");
294 using namespace NumericDetails;
300 using namespace NumericDetails;
301 DecimalExponent convertedExponent = 0;
304 if(fabs(modf(value, &dummy)) != 0.0)
306 const DecimalExponent exp =
static_cast<DecimalExponent
>(precision);
310 value += (value > 0) ? rounding : -rounding;
313 convertedExponent = -exp;
316 return Decimal(static_cast<DecimalMantissa>(value), convertedExponent);
322 static bool tryParse(
const char* buffer,
size_t bufferSize, Int32& number);
323 static bool tryParse(
const char* buffer,
size_t bufferSize, UInt32& number);
324 static bool tryParse(
const char* buffer,
size_t bufferSize, Int64& number);
325 static bool tryParse(
const char* buffer,
size_t bufferSize, UInt64& number);
326 static bool tryParse(
const char* buffer,
size_t bufferSize, Double& number);
327 static bool tryParse(
const char* buffer,
size_t bufferSize,
Decimal& number);
337 typedef bool(*CompareFunction)(
const Decimal&,
const Decimal&);
347 const CompareFunction equalFunc
348 ,
const CompareFunction nonEqualFunc
349 ,
const CompareFunction lessFunc
350 ,
const CompareFunction greaterFunc
353 , nonEqual(nonEqualFunc)
355 , greater(greaterFunc)
362 bool fixedPoint()
const;
365 bool genericDecimal()
const;
369 namespace GenericDecimal {
394 namespace FixedPointDecimal {
const CompareFunction greater
Double toDoubleUnchecked() const
static bool tryParse(const char *buffer, size_t bufferSize, Decimal &)
DecimalComparator(const CompareFunction equalFunc, const CompareFunction nonEqualFunc, const CompareFunction lessFunc, const CompareFunction greaterFunc)
Constructor.
bool nonEqual(const Decimal &lhs, const Decimal &rhs)
const CompareFunction less
Decimal & operator=(const Decimal &other)
Reinitializes instance from another one.
bool greater(const Decimal &lhs, const Decimal &rhs)
bool operator==(const FieldValueRef &ref, const std::string &str)
DecimalMantissa mantissa() const
Returns mantissa part of decimal.
bool operator!=(const FieldValueRef &ref, const std::string &str)
unsigned long long UInt64
OnixS::CME::DropCopy::Double fastPower10(int power)
bool fixedPoint() const
Returns true if this instance is fixed point decimal comparator.
const DecimalComparator Comparator
const CompareFunction equal
Comparing functions.
Helper class for conversion from string to number.
bool toNumber(Int32 &) const
static Decimal fromDoubleUnchecked(Double value, size_t precision)
std::string toString() const
Returns text presentation of decimal.
DecimalExponent exponent() const
Returns exponent part of decimal.
bool equal(const Decimal &lhs, const Decimal &rhs)
const CompareFunction nonEqual
const DecimalComparator Comparator
Decimal type for better precision.
bool less(const Decimal &lhs, const Decimal &rhs)
Decimal(DecimalMantissa mantissa=0, DecimalExponent exponent=0)
Initializes instance from compound components.
bool genericDecimal() const
Returns true if this instance is generic decimal comparator.