507 lines
17 KiB
C++
Executable File
507 lines
17 KiB
C++
Executable File
// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
|
//
|
|
// This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
|
|
// and international copyright treaties.
|
|
//
|
|
// Access to and use of the software is governed by the terms of the applicable OnixS Software
|
|
// Services Agreement (the Agreement) and Customer end user license agreements granting
|
|
// a non-assignable, non-transferable and non-exclusive license to use the software
|
|
// for it's own data processing purposes under the terms defined in the Agreement.
|
|
//
|
|
// Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
|
|
// of this source code or associated reference material to any other location for further reproduction
|
|
// or redistribution, and any amendments to this copyright notice, are expressly prohibited.
|
|
//
|
|
// Any reproduction or redistribution for sale or hiring of the Software not in accordance with
|
|
// the terms of the Agreement is a violation of copyright law.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include <cassert>
|
|
#include <cmath>
|
|
#include <ostream>
|
|
#include <string>
|
|
#include <stdexcept>
|
|
|
|
#include "OnixS/CME/MDP3/Config.h"
|
|
|
|
namespace OnixS
|
|
{
|
|
namespace CME
|
|
{
|
|
namespace MDP3
|
|
{
|
|
typedef int Int32;
|
|
typedef unsigned int UInt32;
|
|
|
|
typedef long long Int64;
|
|
typedef unsigned long long UInt64;
|
|
|
|
typedef double Double;
|
|
|
|
typedef Int64 DecimalMantissa;
|
|
typedef Int32 DecimalExponent;
|
|
|
|
/// Decimal type for better precision.
|
|
class
|
|
ONIXS_CME_MDP3_API
|
|
Decimal
|
|
{
|
|
public:
|
|
/// Initializes instance from compound components.
|
|
Decimal(
|
|
DecimalMantissa mantissa = 0,
|
|
DecimalExponent exponent = 0)
|
|
: mantissa_(mantissa)
|
|
, exponent_(exponent)
|
|
{}
|
|
|
|
/// Converts Double value to decimal.
|
|
/// @param value double precision floating-point value.
|
|
/// @param precision defines conversion precision.
|
|
Decimal(Double value, size_t precision);
|
|
|
|
/// Initializes as copy of given value.
|
|
Decimal(const Decimal& other)
|
|
: mantissa_(other.mantissa_)
|
|
, exponent_(other.exponent_)
|
|
{}
|
|
|
|
/// Reinitializes instance from another one.
|
|
Decimal& operator=(const Decimal& other)
|
|
{
|
|
mantissa_ = other.mantissa_;
|
|
exponent_ = other.exponent_;
|
|
return *this;
|
|
}
|
|
|
|
/// Returns mantissa part of decimal.
|
|
DecimalMantissa mantissa() const
|
|
{
|
|
return mantissa_;
|
|
}
|
|
|
|
/// Updates mantissa part of decimal.
|
|
void mantissa(DecimalMantissa value)
|
|
{
|
|
mantissa_ = value;
|
|
}
|
|
|
|
/// Returns exponent part of decimal.
|
|
DecimalExponent exponent() const
|
|
{
|
|
return exponent_;
|
|
}
|
|
|
|
/// Updates exponent part of decimal.
|
|
void exponent(DecimalExponent value)
|
|
{
|
|
exponent_ = value;
|
|
}
|
|
|
|
/// Null value.
|
|
static const Decimal Null;
|
|
|
|
/// Checks is decimal null.
|
|
bool null() const;
|
|
|
|
/// Compares two numbers.
|
|
bool operator ==(const Decimal&) const;
|
|
|
|
/// Compares two numbers.
|
|
bool operator !=(const Decimal&) const;
|
|
|
|
/// Establishes order between two values.
|
|
bool operator <(const Decimal&) const;
|
|
|
|
/// Establishes order between two values.
|
|
bool operator >(const Decimal&) const;
|
|
|
|
/// Casts to whole integer number as regular
|
|
/// floating point value is casted.
|
|
/// @throw domain_error exception on failure.
|
|
operator Int32() const;
|
|
|
|
/// Casts to whole integer number as regular
|
|
/// floating point value is casted.
|
|
/// @throw domain_error exception on failure.
|
|
operator UInt32() const;
|
|
|
|
/// Casts to whole integer number as regular
|
|
/// floating point value is casted.
|
|
/// @throw domain_error exception on failure.
|
|
operator Int64() const;
|
|
|
|
/// Casts to whole integer number as regular
|
|
/// floating point value is casted.
|
|
/// @throw domain_error exception on failure.
|
|
operator UInt64() const;
|
|
|
|
/// Casts to whole floating point as regular
|
|
/// value is casted to a smaller precision.
|
|
/// @throw domain_error exception on failure.
|
|
operator Double() const;
|
|
|
|
/// Casts to whole integer number as regular
|
|
/// floating point value is casted.
|
|
/// @return false if conversion fails.
|
|
bool toNumber(Int32&) const;
|
|
|
|
/// Casts to whole integer number as regular
|
|
/// floating point value is casted.
|
|
/// @return false if conversion fails.
|
|
bool toNumber(UInt32&) const;
|
|
|
|
/// Casts to whole integer number as regular
|
|
/// floating point value is casted.
|
|
/// @return false if conversion fails.
|
|
bool toNumber(Int64&) const;
|
|
|
|
/// Casts to whole integer number as regular
|
|
/// floating point value is casted.
|
|
/// @return false if conversion fails.
|
|
bool toNumber(UInt64&) const;
|
|
|
|
/// Casts to double floating point number.
|
|
/// @return false if conversion fails.
|
|
bool toNumber(Double&) const;
|
|
|
|
/// Fast cast to double floating point number.
|
|
/// @note For the sake of performance no range checks are done.
|
|
/// Result may be invalid if decimal cannot be represented as double value.
|
|
/// @warning Result may be different (less precise) from toNumber(Double&)
|
|
/// result due to performance vs precison trade-off.
|
|
Double toDoubleUnchecked() const;
|
|
|
|
/// Fast cast from double floating point number.
|
|
/// @note For the sake of performance no range checks are done.
|
|
/// Result may be invalid if decimal cannot be represented as double value.
|
|
/// @warning Result may be different (less precise) from Decimal(Double, size_t)
|
|
/// result due to performance vs precison trade-off.
|
|
static Decimal fromDoubleUnchecked(Double value, size_t precision);
|
|
|
|
/// Appends text presentation to given string.
|
|
void toString(std::string&) const;
|
|
|
|
/// Returns text presentation of decimal.
|
|
std::string toString() const
|
|
{
|
|
std::string str;
|
|
toString(str);
|
|
return str;
|
|
}
|
|
|
|
/// Attempts to parse decimal value
|
|
/// from its string/text presentation.
|
|
/// @return false on parsing failure.
|
|
static
|
|
bool
|
|
tryParse(
|
|
const char* buffer,
|
|
size_t bufferSize,
|
|
Decimal&);
|
|
|
|
/// Parses decimal from string presentation.
|
|
/// @throw std::exception on failure.
|
|
static
|
|
Decimal
|
|
parse(
|
|
const char* buffer,
|
|
size_t bufferSize);
|
|
|
|
private:
|
|
/// Mantissa.
|
|
DecimalMantissa mantissa_;
|
|
|
|
/// Exponent.
|
|
DecimalExponent exponent_;
|
|
};
|
|
|
|
inline
|
|
Decimal::operator Int32() const
|
|
{
|
|
Int32 number;
|
|
|
|
if (toNumber (number) )
|
|
return number;
|
|
|
|
throw std::domain_error (
|
|
"Cannot cast value to target type. ");
|
|
}
|
|
|
|
inline
|
|
Decimal::operator UInt32() const
|
|
{
|
|
UInt32 number;
|
|
|
|
if (toNumber (number) )
|
|
return number;
|
|
|
|
throw std::domain_error (
|
|
"Cannot cast value to target type. ");
|
|
}
|
|
|
|
inline
|
|
Decimal::operator Int64() const
|
|
{
|
|
Int64 number;
|
|
|
|
if (toNumber (number) )
|
|
return number;
|
|
|
|
throw std::domain_error (
|
|
"Cannot cast value to target type. ");
|
|
}
|
|
|
|
inline
|
|
Decimal::operator UInt64() const
|
|
{
|
|
UInt64 number;
|
|
|
|
if (toNumber (number) )
|
|
return number;
|
|
|
|
throw std::domain_error (
|
|
"Cannot cast value to target type. ");
|
|
}
|
|
|
|
inline
|
|
Decimal::operator Double() const
|
|
{
|
|
Double number;
|
|
|
|
if (toNumber (number) )
|
|
return number;
|
|
|
|
throw std::domain_error (
|
|
"Cannot cast value to target type. ");
|
|
}
|
|
|
|
namespace NumericDetails
|
|
{
|
|
static const int maxAbsPower10 = 20;
|
|
static const int powers10Size = maxAbsPower10 * 2 + 1; // Negative, positive and zero powers
|
|
|
|
static const double powers10[] =
|
|
{
|
|
1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12, 1E-11,
|
|
1E-10, 1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1,
|
|
1E0,
|
|
1E1, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8, 1E9, 1E10,
|
|
1E11, 1E12, 1E13, 1E14, 1E15, 1E16, 1E17, 1E18, 1E19, 1E20
|
|
};
|
|
|
|
inline OnixS::CME::MDP3::Double fastPower10(int power)
|
|
{
|
|
const int offset = power + maxAbsPower10;
|
|
if(offset >= 0 && offset < powers10Size)
|
|
{
|
|
return powers10[offset];
|
|
}
|
|
|
|
throw std::invalid_argument("Invalid power argument, must be in range [-20, 20]");
|
|
}
|
|
}
|
|
|
|
inline
|
|
Double Decimal::toDoubleUnchecked() const
|
|
{
|
|
assert(!null());
|
|
using namespace NumericDetails;
|
|
return mantissa_ * fastPower10(exponent_);
|
|
}
|
|
|
|
inline
|
|
Decimal Decimal::fromDoubleUnchecked(Double value, size_t precision)
|
|
{
|
|
using namespace NumericDetails;
|
|
DecimalExponent convertedExponent = 0;
|
|
|
|
Double dummy;
|
|
if(fabs(modf(value, &dummy)) != 0.0)
|
|
{
|
|
const DecimalExponent exp = static_cast<DecimalExponent>(precision);
|
|
|
|
// Apply last digit rounding
|
|
const Double rounding = 0.5 * fastPower10(-exp);
|
|
value += (value > 0) ? rounding : -rounding;
|
|
|
|
value *= fastPower10(exp);
|
|
convertedExponent = -exp;
|
|
}
|
|
|
|
return Decimal(static_cast<DecimalMantissa>(value), convertedExponent);
|
|
}
|
|
|
|
/// Helper class for conversion from string to number.
|
|
struct
|
|
ONIXS_CME_MDP3_API
|
|
Number
|
|
{
|
|
static
|
|
bool
|
|
tryParse(
|
|
const char* buffer,
|
|
size_t bufferSize,
|
|
Int32& number);
|
|
|
|
static
|
|
bool
|
|
tryParse(
|
|
const char* buffer,
|
|
size_t bufferSize,
|
|
UInt32& number);
|
|
|
|
static
|
|
bool
|
|
tryParse(
|
|
const char* buffer,
|
|
size_t bufferSize,
|
|
Int64& number);
|
|
|
|
static
|
|
bool
|
|
tryParse(
|
|
const char* buffer,
|
|
size_t bufferSize,
|
|
UInt64& number);
|
|
|
|
static
|
|
bool
|
|
tryParse(
|
|
const char* buffer,
|
|
size_t bufferSize,
|
|
Double& number);
|
|
|
|
static
|
|
bool
|
|
tryParse(
|
|
const char* buffer,
|
|
size_t bufferSize,
|
|
Decimal& number);
|
|
};
|
|
|
|
/// Comparator aggregate providing compare functions.
|
|
/// Such functions can be generic (slower, but able to deal
|
|
/// with any decimal) or fixed point based (faster, but assuming that
|
|
/// all compating values have the same exponent).
|
|
struct DecimalComparator
|
|
{
|
|
/// Comparing function signature.
|
|
typedef bool(*CompareFunction)(const Decimal&, const Decimal&);
|
|
|
|
/// Comparing functions.
|
|
const CompareFunction equal;
|
|
const CompareFunction nonEqual;
|
|
const CompareFunction less;
|
|
const CompareFunction greater;
|
|
|
|
/// Constructor.
|
|
DecimalComparator(
|
|
const CompareFunction equalFunc,
|
|
const CompareFunction nonEqualFunc,
|
|
const CompareFunction lessFunc,
|
|
const CompareFunction greaterFunc)
|
|
: equal(equalFunc)
|
|
, nonEqual(nonEqualFunc)
|
|
, less(lessFunc)
|
|
, greater(greaterFunc)
|
|
{}
|
|
|
|
// Assigment operator: not defined.
|
|
DecimalComparator& operator=(const DecimalComparator&);
|
|
|
|
/// Returns true if this instance is fixed point decimal comparator.
|
|
bool fixedPoint() const;
|
|
|
|
/// Returns true if this instance is generic decimal comparator.
|
|
bool genericDecimal() const;
|
|
};
|
|
|
|
/// Helper functions for generic decimal case.
|
|
namespace GenericDecimal
|
|
{
|
|
inline bool equal(const Decimal& lhs, const Decimal& rhs)
|
|
{
|
|
return lhs == rhs;
|
|
}
|
|
|
|
inline bool nonEqual(const Decimal& lhs, const Decimal& rhs)
|
|
{
|
|
return lhs != rhs;
|
|
}
|
|
|
|
inline bool less(const Decimal& lhs, const Decimal& rhs)
|
|
{
|
|
return lhs < rhs;
|
|
}
|
|
|
|
inline bool greater(const Decimal& lhs, const Decimal& rhs)
|
|
{
|
|
return lhs > rhs;
|
|
}
|
|
|
|
ONIXS_CME_MDP3_API extern const DecimalComparator Comparator;
|
|
}
|
|
|
|
/// Helper functions for CME fixed point decimal case.
|
|
namespace FixedPointDecimal
|
|
{
|
|
inline bool equal(const Decimal& lhs, const Decimal& rhs)
|
|
{
|
|
assert(lhs.exponent() == rhs.exponent() || lhs.null() || rhs.null());
|
|
return lhs.mantissa() == rhs.mantissa();
|
|
}
|
|
|
|
inline bool nonEqual(const Decimal& lhs, const Decimal& rhs)
|
|
{
|
|
assert(lhs.exponent() == rhs.exponent() || lhs.null() || rhs.null());
|
|
return lhs.mantissa() != rhs.mantissa();
|
|
}
|
|
|
|
inline bool less(const Decimal& lhs, const Decimal& rhs)
|
|
{
|
|
assert(lhs.exponent() == rhs.exponent() || lhs.null() || rhs.null());
|
|
return lhs.mantissa() < rhs.mantissa();
|
|
}
|
|
|
|
inline bool greater(const Decimal& lhs, const Decimal& rhs)
|
|
{
|
|
assert(lhs.exponent() == rhs.exponent() || lhs.null() || rhs.null());
|
|
return lhs.mantissa() > rhs.mantissa();
|
|
}
|
|
|
|
ONIXS_CME_MDP3_API extern const DecimalComparator Comparator;
|
|
}
|
|
|
|
inline
|
|
bool
|
|
DecimalComparator::fixedPoint() const
|
|
{
|
|
return this == &FixedPointDecimal::Comparator;
|
|
}
|
|
|
|
inline
|
|
bool
|
|
DecimalComparator::genericDecimal() const
|
|
{
|
|
return this == &GenericDecimal::Comparator;
|
|
}
|
|
|
|
static const DecimalComparator&
|
|
defaultDecimalComparator = GenericDecimal::Comparator;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace std
|
|
{
|
|
// Outputs decimal into standard stream.
|
|
ONIXS_CME_MDP3_API
|
|
std::ostream&
|
|
operator <<(
|
|
std::ostream&,
|
|
const OnixS::CME::MDP3::Decimal&);
|
|
}
|