xo-unit: multiply, volatility units, share operators
This commit is contained in:
parent
d7fa7156df
commit
eb63f5fdc8
6 changed files with 212 additions and 47 deletions
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "quantity2_concept.hpp"
|
||||
#include "quantity_ops.hpp"
|
||||
#include "scaled_unit.hpp"
|
||||
#include "natural_unit.hpp"
|
||||
|
||||
|
|
@ -60,10 +60,6 @@ namespace xo {
|
|||
ratio_int2x_type>(this->unit_, unit2);
|
||||
|
||||
if (rr.natural_unit_.is_dimensionless()) {
|
||||
/* FIXME: rr.outer_scale_exact_ can overflow since fixed precision.
|
||||
* accumulate in scale instead
|
||||
*/
|
||||
|
||||
repr_type r_scale = (::sqrt(rr.outer_scale_sq_)
|
||||
* rr.outer_scale_factor_.template convert_to<repr_type>()
|
||||
* this->scale_);
|
||||
|
|
@ -98,7 +94,7 @@ namespace xo {
|
|||
typename Quantity2::repr_type>;
|
||||
using r_int_type = std::common_type_t<typename Quantity::ratio_int_type,
|
||||
typename Quantity2::ratio_int_type>;
|
||||
using r_int2x_type = std::common_type_t<typename Quantity2::ratio_int2x_type,
|
||||
using r_int2x_type = std::common_type_t<typename Quantity::ratio_int2x_type,
|
||||
typename Quantity2::ratio_int2x_type>;
|
||||
|
||||
auto rr = detail::su_product<r_int_type, r_int2x_type>(x.unit(), y.unit());
|
||||
|
|
@ -216,6 +212,8 @@ namespace xo {
|
|||
return *this;
|
||||
}
|
||||
|
||||
// TODO: operator+=, operator-=
|
||||
|
||||
constexpr nu_abbrev_type abbrev() const { return unit_.abbrev(); }
|
||||
|
||||
private:
|
||||
|
|
@ -245,34 +243,6 @@ namespace xo {
|
|||
return Quantity<Repr, Int>(1.0, nu);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, typename Quantity2>
|
||||
requires quantity2_concept<Quantity> && quantity2_concept<Quantity2>
|
||||
constexpr auto
|
||||
operator* (const Quantity & x, const Quantity2 & y)
|
||||
{
|
||||
return Quantity::multiply(x, y);
|
||||
}
|
||||
|
||||
/** note: does not require unit scaling, so constexpr with c++23 **/
|
||||
template <typename Dimensionless, typename Quantity>
|
||||
requires std::is_arithmetic_v<Dimensionless> && quantity2_concept<Quantity>
|
||||
constexpr auto
|
||||
operator* (Dimensionless x, const Quantity & y)
|
||||
{
|
||||
return y.scale_by(x);
|
||||
}
|
||||
|
||||
/** note: does not require unit scaling, so constexpr with c++23 **/
|
||||
template <typename Dimensionless, typename Quantity>
|
||||
requires std::is_arithmetic_v<Dimensionless> && quantity2_concept<Quantity>
|
||||
constexpr auto
|
||||
operator* (const Quantity & x, Dimensionless y)
|
||||
{
|
||||
return x.scale_by(y);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, typename Quantity2>
|
||||
|
|
@ -321,16 +291,6 @@ namespace xo {
|
|||
return Quantity::subtract(x, y);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, typename Quantity2>
|
||||
requires quantity2_concept<Quantity> && quantity2_concept<Quantity2>
|
||||
constexpr auto
|
||||
operator== (const Quantity & x, const Quantity2 & y)
|
||||
{
|
||||
return (Quantity::compare(x, y) == 0);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, typename Quantity2>
|
||||
|
|
|
|||
|
|
@ -65,8 +65,9 @@ namespace xo {
|
|||
public:
|
||||
constexpr natural_unit() : n_bpu_{0} {}
|
||||
|
||||
static constexpr natural_unit from_bu(basis_unit bu) {
|
||||
return detail::nu_maker<Int>::make_nu(make_unit_power<Int>(bu));
|
||||
static constexpr natural_unit from_bu(basis_unit bu,
|
||||
power_ratio_type power = power_ratio_type(1)) {
|
||||
return detail::nu_maker<Int>::make_nu(bpu<Int>(bu, power));
|
||||
}
|
||||
|
||||
constexpr std::size_t n_bpu() const { return n_bpu_; }
|
||||
|
|
@ -399,6 +400,9 @@ namespace xo {
|
|||
constexpr auto currency = natural_unit<std::int64_t>::from_bu(bu::currency);
|
||||
|
||||
constexpr auto price = natural_unit<std::int64_t>::from_bu(bu::price);
|
||||
|
||||
constexpr auto volatility_250d = natural_unit<std::int64_t>::from_bu(bu::year250, power_ratio_type(-1,2));
|
||||
constexpr auto volatility_360d = natural_unit<std::int64_t>::from_bu(bu::year360, power_ratio_type(-1,2));
|
||||
} /*namespace nu*/
|
||||
} /*namespace qty*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "quantity_ops.hpp"
|
||||
#include "natural_unit.hpp"
|
||||
#include "scaled_unit.hpp"
|
||||
|
||||
|
|
@ -20,7 +21,7 @@ namespace xo {
|
|||
typename Repr = double,
|
||||
typename Int = std::int64_t,
|
||||
natural_unit<Int> NaturalUnit = natural_unit<Int>(),
|
||||
typename Int2x = detail::width2x<Int>
|
||||
typename Int2x = detail::width2x_t<Int>
|
||||
>
|
||||
class quantity {
|
||||
public:
|
||||
|
|
@ -36,6 +37,86 @@ namespace xo {
|
|||
constexpr const repr_type & scale() const { return scale_; }
|
||||
constexpr const unit_type & unit() const { return s_unit; }
|
||||
|
||||
// is_dimensionless
|
||||
|
||||
// unit_qty
|
||||
// zero_qty
|
||||
// reciprocal
|
||||
|
||||
/* parallel implementation to Quantity<Repr, Int>::rescale(),
|
||||
* except that NaturalUnit2 is a compile-time-only template-argument
|
||||
*
|
||||
* NOTE: constexpr as long as no fractional units involved.
|
||||
*/
|
||||
template <natural_unit<Int> NaturalUnit2>
|
||||
constexpr
|
||||
auto rescale() const {
|
||||
/* conversion factor from .unit -> unit2*/
|
||||
auto rr = detail::su_ratio<ratio_int_type,
|
||||
ratio_int2x_type>(NaturalUnit, NaturalUnit2);
|
||||
|
||||
if (rr.natural_unit_.is_dimensionless()) {
|
||||
repr_type r_scale = (((rr.outer_scale_sq_ == 1.0)
|
||||
? 1.0
|
||||
: ::sqrt(rr.outer_scale_sq_))
|
||||
* rr.outer_scale_factor_.template convert_to<repr_type>()
|
||||
* this->scale_);
|
||||
return quantity<Repr, Int, NaturalUnit2, Int2x>(r_scale);
|
||||
} else {
|
||||
return quantity<Repr, Int, NaturalUnit2, Int2x>(std::numeric_limits<repr_type>::quiet_NaN());
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Dimensionless>
|
||||
requires std::is_arithmetic_v<Dimensionless>
|
||||
constexpr auto scale_by(Dimensionless x) const {
|
||||
return quantity(x * this->scale_);
|
||||
}
|
||||
|
||||
// divide_by
|
||||
// divide_into
|
||||
|
||||
/* parallel implementation to Quantity<Repr, Int>,
|
||||
* but return type will have dimension computed at compile-time
|
||||
*/
|
||||
template <typename Quantity2>
|
||||
static constexpr auto multiply(const quantity & x, const Quantity2 & y) {
|
||||
using r_repr_type = std::common_type_t<typename quantity::repr_type,
|
||||
typename Quantity2::repr_type>;
|
||||
using r_int_type = std::common_type_t<typename quantity::ratio_int_type,
|
||||
typename Quantity2::ratio_int_type>;
|
||||
using r_int2x_type = std::common_type_t<typename quantity::ratio_int2x_type,
|
||||
typename Quantity2::ratio_int2x_type>;
|
||||
|
||||
constexpr auto rr = detail::su_product<r_int_type, r_int2x_type>(x.unit(), y.unit());
|
||||
|
||||
r_repr_type r_scale = (::sqrt(rr.outer_scale_sq_)
|
||||
* rr.outer_scale_factor_.template convert_to<r_repr_type>()
|
||||
* static_cast<r_repr_type>(x.scale())
|
||||
* static_cast<r_repr_type>(y.scale()));
|
||||
|
||||
return quantity<r_repr_type, r_int_type, rr.natural_unit_, r_int2x_type>(r_scale);
|
||||
}
|
||||
|
||||
// divide
|
||||
// add
|
||||
// subtract
|
||||
|
||||
/* parallel implementation to Quantity<Repr, Int> */
|
||||
template <typename Quantity2>
|
||||
static constexpr
|
||||
auto compare(const quantity &x, const Quantity2 & y) {
|
||||
quantity y2 = y.template rescale<s_unit>();
|
||||
|
||||
return x.scale() <=> y2.scale();
|
||||
}
|
||||
|
||||
// operator-
|
||||
// operator+=
|
||||
// operator-=
|
||||
// operator*=
|
||||
// operator/=
|
||||
|
||||
constexpr nu_abbrev_type abbrev() const { return s_unit.abbrev(); }
|
||||
|
||||
public: /* need public members so that a quantity instance can be a non-type template parameter (is a structural type) */
|
||||
|
|
@ -89,6 +170,12 @@ namespace xo {
|
|||
inline constexpr auto year360s(double x) { return quantity<double, std::int64_t, nu::year360>(x); }
|
||||
inline constexpr auto year365s(double x) { return quantity<double, std::int64_t, nu::year365>(x); }
|
||||
//inline constexpr auto year366s(double x) { return quantity<double, std::int64_t, nu::year366>(x); }
|
||||
|
||||
// ----- volatility -----
|
||||
|
||||
/* volatility in units of 1/yr */
|
||||
inline constexpr auto volatility_250d(double x) { return quantity<double, std::int64_t, nu::volatility_250d>(x); }
|
||||
inline constexpr auto volatility_360d(double x) { return quantity<double, std::int64_t, nu::volatility_360d>(x); }
|
||||
}
|
||||
} /*namespace qty*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ namespace xo {
|
|||
typename Quantity::unit_type;
|
||||
typename Quantity::repr_type;
|
||||
|
||||
//{ Quantity::multiply(qty, qty) };
|
||||
|
||||
{ qty.scale() } -> std::same_as<const typename Quantity::repr_type &>;
|
||||
{ qty.unit() } -> std::same_as<const typename Quantity::unit_type &>;
|
||||
//{ Quantity::unit_cstr() } -> std::same_as<char const *>;
|
||||
|
|
|
|||
55
include/xo/unit/quantity_ops.hpp
Normal file
55
include/xo/unit/quantity_ops.hpp
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/** @file quantity_ops.hpp
|
||||
*
|
||||
* Author: Roland Conybeare
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "quantity2_concept.hpp"
|
||||
//#include <cstdint>
|
||||
|
||||
namespace xo {
|
||||
namespace qty {
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, typename Quantity2>
|
||||
requires quantity2_concept<Quantity> && quantity2_concept<Quantity2>
|
||||
constexpr auto
|
||||
operator* (const Quantity & x, const Quantity2 & y)
|
||||
{
|
||||
return Quantity::multiply(x, y);
|
||||
}
|
||||
|
||||
/** note: does not require unit scaling, so constexpr with c++23 **/
|
||||
template <typename Dimensionless, typename Quantity>
|
||||
requires std::is_arithmetic_v<Dimensionless> && quantity2_concept<Quantity>
|
||||
constexpr auto
|
||||
operator* (Dimensionless x, const Quantity & y)
|
||||
{
|
||||
return y.scale_by(x);
|
||||
}
|
||||
|
||||
/** note: does not require unit scaling, so constexpr with c++23 **/
|
||||
template <typename Dimensionless, typename Quantity>
|
||||
requires std::is_arithmetic_v<Dimensionless> && quantity2_concept<Quantity>
|
||||
constexpr auto
|
||||
operator* (const Quantity & x, Dimensionless y)
|
||||
{
|
||||
return x.scale_by(y);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, typename Quantity2>
|
||||
requires quantity2_concept<Quantity> && quantity2_concept<Quantity2>
|
||||
constexpr auto
|
||||
operator== (const Quantity & x, const Quantity2 & y)
|
||||
{
|
||||
return (Quantity::compare(x, y) == 0);
|
||||
}
|
||||
} /*namespace qty*/
|
||||
|
||||
} /*namespace xo*/
|
||||
|
||||
|
||||
/** end quantity_ops.hpp **/
|
||||
Loading…
Add table
Add a link
Reference in a new issue