Merge branch 'main' of github.com:rconybea/xo-unit
This commit is contained in:
commit
1dd6ac7d80
8 changed files with 151 additions and 38 deletions
|
|
@ -1,28 +0,0 @@
|
|||
/** @file quantity2.hpp
|
||||
*
|
||||
* Author: Roland Conybeare
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "bpu_array.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace qty {
|
||||
/** @class quantity
|
||||
* @brief represent a scalar quantity with attached units. enforce dimensional consistency.
|
||||
*
|
||||
* Constexpr implementation, can compute units at compile time
|
||||
**/
|
||||
template <typename Repr = double, typename Int = std::int64_t>
|
||||
class quantity2 {
|
||||
public:
|
||||
using repr_type = Repr;
|
||||
|
||||
private:
|
||||
};
|
||||
} /*namespace qty*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
||||
/** end quantity2.hpp **/
|
||||
47
include/xo/unit/quantity2_old.hpp
Normal file
47
include/xo/unit/quantity2_old.hpp
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/** @file quantity.hpp
|
||||
*
|
||||
* Author: Roland Conybeare
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "bpu_array.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace qty {
|
||||
/** @class quantity
|
||||
* @brief represent a scalar quantity with attached units. enforce dimensional consistency.
|
||||
*
|
||||
* Constexpr implementation, can compute units at compile time
|
||||
**/
|
||||
template <typename Repr = double,
|
||||
typename Int = std::int64_t,
|
||||
natural_unit<Int> NaturalUnit>
|
||||
class quantity {
|
||||
public:
|
||||
using repr_type = Repr;
|
||||
using unit_type = natural_unit<int>;
|
||||
|
||||
public:
|
||||
constexpr quantity(Repr scale)
|
||||
: scale_{scale} {}
|
||||
|
||||
constexpr const repr_type & scale() const { return scale_; }
|
||||
constexpr unit_type unit() const { return NaturalUnit; }
|
||||
|
||||
constexpr bool is_dimensionless() const { return s_unit.is_dimensionless(); }
|
||||
|
||||
constexpr quantity unit_qty() { return quantity(1); }
|
||||
|
||||
private:
|
||||
/** @brief unit (established at compile time) for this quantity **/
|
||||
static NaturalUnit s_unit = NaturalUnit;
|
||||
|
||||
/** @brief quantity represents this multiple of unit amount **/
|
||||
Repr scale_ = Repr();
|
||||
};
|
||||
} /*namespace qty*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
||||
/** end quantity.hpp **/
|
||||
|
|
@ -281,6 +281,26 @@ namespace xo {
|
|||
return Quantity::add(x, y);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity>
|
||||
requires quantity2_concept<Quantity>
|
||||
constexpr auto
|
||||
operator+ (const Quantity & x, double y)
|
||||
{
|
||||
return x + Quantity(y, nu::dimensionless);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity>
|
||||
requires quantity2_concept<Quantity>
|
||||
constexpr auto
|
||||
operator+ (double x, const Quantity & y)
|
||||
{
|
||||
return Quantity(x, nu::dimensionless) + y;
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, typename Quantity2>
|
||||
|
|
@ -291,6 +311,56 @@ namespace xo {
|
|||
return Quantity::subtract(x, y);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity>
|
||||
requires quantity2_concept<Quantity>
|
||||
constexpr auto
|
||||
operator- (const Quantity & x, double y)
|
||||
{
|
||||
return x - Quantity(y, nu::dimensionless);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity>
|
||||
requires quantity2_concept<Quantity>
|
||||
constexpr auto
|
||||
operator- (double x, const Quantity & y)
|
||||
{
|
||||
return Quantity(x, nu::dimensionless) - 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>
|
||||
requires quantity2_concept<Quantity>
|
||||
constexpr auto
|
||||
operator== (const Quantity & x, double y)
|
||||
{
|
||||
return (x == Quantity(y, nu::dimensionless));
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity>
|
||||
requires quantity2_concept<Quantity>
|
||||
constexpr auto
|
||||
operator== (double x, const Quantity & y)
|
||||
{
|
||||
return (Quantity(x, nu::dimensionless) == y);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, typename Quantity2>
|
||||
|
|
@ -301,6 +371,26 @@ namespace xo {
|
|||
return Quantity::compare(x, y);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, double>
|
||||
requires quantity2_concept<Quantity>
|
||||
constexpr auto
|
||||
operator<=> (const Quantity & x, double y)
|
||||
{
|
||||
return Quantity::compare(x, Quantity(y, nu::dimensionless));
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, double>
|
||||
requires quantity2_concept<Quantity>
|
||||
constexpr auto
|
||||
operator<=> (double x, const Quantity & y)
|
||||
{
|
||||
return Quantity::compare(Quantity(x, nu::dimensionless), y);
|
||||
}
|
||||
|
||||
namespace unit {
|
||||
constexpr auto nanogram = natural_unit_qty(nu::nanogram);
|
||||
}
|
||||
|
|
@ -17,10 +17,14 @@ namespace xo {
|
|||
operator<< (std::ostream & os,
|
||||
const Quantity<Repr, Int> & x)
|
||||
{
|
||||
os << x.scale() << x.abbrev();
|
||||
|
||||
#ifdef NOT_USING
|
||||
os << "<qty"
|
||||
<< xtag("scale", x.scale())
|
||||
<< xtag("unit", x.unit())
|
||||
<< ">";
|
||||
#endif
|
||||
|
||||
return os;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue