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
|
|
@ -7,4 +7,4 @@ xo_docdir_sphinx_config(
|
|||
quantity-reference.rst quantity-class.rst quantity-factoryfunctions.rst quantity-unitvars.rst
|
||||
unit-reference.rst unit-concept.rst unit-quantities.rst
|
||||
)
|
||||
xo_utest_coverage_config2()
|
||||
#xo_utest_coverage_config2()
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
set(SELF_EXE utest.unit)
|
||||
set(SELF_SRCS
|
||||
unit_utest_main.cpp #mpl_unit.test.cpp
|
||||
Quantity.test.cpp
|
||||
xquantity.test.cpp
|
||||
quantity.test.cpp
|
||||
bpu.test.cpp
|
||||
basis_unit.test.cpp
|
||||
|
|
|
|||
|
|
@ -20,14 +20,14 @@ namespace xo {
|
|||
constexpr su64_type su_reciprocal = su.reciprocal();
|
||||
|
||||
TEST_CASE("scaled_unit", "[scaled_unit]") {
|
||||
static_assert(su_reciprocal<scaled_unit(nu::gram, xo::ratio::ratio(1L), 1)>.natural_unit_ == nu::gram.reciprocal());
|
||||
REQUIRE(su_reciprocal<scaled_unit(nu::gram, xo::ratio::ratio(1L), 1)>.natural_unit_ == nu::gram.reciprocal());
|
||||
static_assert(su_reciprocal<scaled_unit(nu::gram, xo::ratio::ratio(1LL), 1)>.natural_unit_ == nu::gram.reciprocal());
|
||||
REQUIRE(su_reciprocal<scaled_unit(nu::gram, xo::ratio::ratio(1LL), 1)>.natural_unit_ == nu::gram.reciprocal());
|
||||
|
||||
static_assert(su_reciprocal<scaled_unit(nu::gram, xo::ratio::ratio(1L), 1)>.outer_scale_factor_ == 1);
|
||||
REQUIRE(su_reciprocal<scaled_unit(nu::gram, xo::ratio::ratio(1L), 1)>.outer_scale_factor_ == 1);
|
||||
static_assert(su_reciprocal<scaled_unit(nu::gram, xo::ratio::ratio(1LL), 1)>.outer_scale_factor_ == 1);
|
||||
REQUIRE(su_reciprocal<scaled_unit(nu::gram, xo::ratio::ratio(1LL), 1)>.outer_scale_factor_ == 1);
|
||||
|
||||
static_assert(su_reciprocal<scaled_unit(nu::gram, xo::ratio::ratio(1L), 1)>.outer_scale_sq_ == 1.0);
|
||||
REQUIRE(su_reciprocal<scaled_unit(nu::gram, xo::ratio::ratio(1L), 1)>.outer_scale_sq_ == 1.0);
|
||||
static_assert(su_reciprocal<scaled_unit(nu::gram, xo::ratio::ratio(1LL), 1)>.outer_scale_sq_ == 1.0);
|
||||
REQUIRE(su_reciprocal<scaled_unit(nu::gram, xo::ratio::ratio(1LL), 1)>.outer_scale_sq_ == 1.0);
|
||||
} /*TEST_CASE(scaled_unit)*/
|
||||
|
||||
TEST_CASE("su_product", "[scaled_unit]") {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
/* @file Quantity.test.cpp */
|
||||
|
||||
#include "Quantity.hpp"
|
||||
#include "Quantity_iostream.hpp"
|
||||
#include "xquantity.hpp"
|
||||
#include "xquantity_iostream.hpp"
|
||||
#include "xo/randomgen/random_seed.hpp"
|
||||
#include "xo/randomgen/xoshiro256.hpp"
|
||||
#include "xo/indentlog/scope.hpp"
|
||||
Loading…
Add table
Add a link
Reference in a new issue