22 #include "OnixS/CME/DropCopy/Export.h" 30 namespace OnixS {
namespace CME {
namespace DropCopy {
48 Decimal(DecimalMantissa mantissa = 0, DecimalExponent exponent = 0);
53 Decimal(Double value,
size_t precision);
59 DecimalMantissa mantissa()
const;
62 void mantissa(DecimalMantissa);
65 DecimalExponent exponent()
const;
68 void exponent(DecimalExponent);
77 bool operator<(
const Decimal&)
const;
80 bool operator>(
const Decimal&)
const;
85 operator Int32()
const;
95 operator Int64()
const;
110 bool toNumber(Int32&)
const;
115 bool toNumber(UInt32&)
const;
120 bool toNumber(Int64&)
const;
125 bool toNumber(UInt64&)
const;
129 bool toNumber(Double&)
const;
136 Double toDoubleUnchecked()
const;
143 static Decimal fromDoubleUnchecked(Double value,
size_t precision);
146 void toString(std::string&)
const;
149 std::string toString()
const;
157 static bool tryParse(
const char* buffer,
size_t bufferSize,
Decimal&);
161 static Decimal parse(
const char* buffer,
size_t bufferSize);
165 DecimalMantissa mantissa_;
168 DecimalExponent exponent_;
172 : mantissa_(mantissa)
173 , exponent_(exponent)
178 : mantissa_(other.mantissa_)
179 , exponent_(other.exponent_)
205 mantissa_ = other.mantissa_;
206 exponent_ = other.exponent_;
211 inline Decimal::operator
Int32()
const 220 throw std::domain_error(
"Cannot cast value to target type. ");
232 throw std::domain_error(
"Cannot cast value to target type. ");
235 inline Decimal::operator
Int64()
const 244 throw std::domain_error(
"Cannot cast value to target type. ");
256 throw std::domain_error(
"Cannot cast value to target type. ");
268 throw std::domain_error(
"Cannot cast value to target type. ");
273 std::string presentation;
278 namespace NumericDetails {
279 static const int maxAbsPower10 = 20;
280 static const int powers10Size = maxAbsPower10 * 2 + 1;
282 static const double powers10[] = {1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12,
283 1E-11, 1E-10, 1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3,
284 1E-2, 1E-1, 1E0, 1E1, 1E2, 1E3, 1E4, 1E5, 1E6,
285 1E7, 1E8, 1E9, 1E10, 1E11, 1E12, 1E13, 1E14, 1E15,
286 1E16, 1E17, 1E18, 1E19, 1E20};
290 const int offset = power + maxAbsPower10;
291 if (offset >= 0 && offset < powers10Size)
293 return powers10[offset];
296 throw std::invalid_argument(
"Invalid power argument, must be in range [-20, 20]");
302 using namespace NumericDetails;
308 using namespace NumericDetails;
309 DecimalExponent convertedExponent = 0;
312 if (fabs(modf(value, &dummy)) != 0.0)
314 const DecimalExponent exp =
static_cast<DecimalExponent
>(precision);
318 value += (value > 0) ? rounding : -rounding;
321 convertedExponent = -exp;
324 return Decimal(static_cast<DecimalMantissa>(value), convertedExponent);
330 static bool tryParse(
const char* buffer,
size_t bufferSize, Int32& number);
331 static bool tryParse(
const char* buffer,
size_t bufferSize, UInt32& number);
332 static bool tryParse(
const char* buffer,
size_t bufferSize, Int64& number);
333 static bool tryParse(
const char* buffer,
size_t bufferSize, UInt64& number);
334 static bool tryParse(
const char* buffer,
size_t bufferSize, Double& number);
335 static bool tryParse(
const char* buffer,
size_t bufferSize,
Decimal& number);
345 typedef bool (*CompareFunction)(
const Decimal&,
const Decimal&);
355 const CompareFunction equalFunc,
356 const CompareFunction nonEqualFunc,
357 const CompareFunction lessFunc,
358 const CompareFunction greaterFunc
361 , nonEqual(nonEqualFunc)
363 , greater(greaterFunc)
371 bool fixedPoint()
const;
374 bool genericDecimal()
const;
378 namespace GenericDecimal {
403 namespace FixedPointDecimal {
448 ONIXS_CME_DROP_COPY_EXPORT std::ostream&
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.