Add 'xo-unit/' from commit 'b531e382c2'

git-subtree-dir: xo-unit
git-subtree-mainline: e9ee6992ca
git-subtree-split: b531e382c2
This commit is contained in:
Roland Conybeare 2025-05-10 21:29:43 -05:00
commit d1fa15f248
105 changed files with 11790 additions and 0 deletions

View file

@ -0,0 +1,10 @@
add_subdirectory(ex1)
add_subdirectory(ex2)
add_subdirectory(ex3)
add_subdirectory(ex4)
add_subdirectory(ex5)
add_subdirectory(ex6)
add_subdirectory(ex7)
add_subdirectory(ex8)
add_subdirectory(ex_su)
add_subdirectory(ex_qty)

View file

@ -0,0 +1,12 @@
# xo-unit/example/ex1/CMakeLists.txt
set(SELF_EXE xo_unit_ex1)
set(SELF_SRCS ex1.cpp)
if (XO_ENABLE_EXAMPLES)
xo_add_executable(${SELF_EXE} ${SELF_SRCS})
xo_self_headeronly_dependency(${SELF_EXE} xo_unit)
xo_dependency(${SELF_EXE} xo_flatstring)
endif()
# end CMakeLists.txt

View file

@ -0,0 +1,53 @@
/** @file ex1.cpp **/
#include "xo/unit/quantity.hpp"
#include "xo/unit/quantity_iostream.hpp"
#include <iostream>
int
main () {
namespace q = xo::qty::qty;
namespace u = xo::qty::u;
using xo::qty::quantity;
using xo::flatstring;
using namespace std;
constexpr auto t = q::minutes(2);
constexpr auto d = q::kilometers(2.5);
constexpr auto t2 = t*t;
constexpr auto a = d / (t*t);
cerr << "t: " << t << ", d: " << d
<< ", t^2: " << t2
<< ", d.t^-2: " << a
<< endl;
static_assert(std::same_as<decltype(t)::repr_type, int>);
static_assert(sizeof(t) == sizeof(int));
static_assert(t.scale() == 2);
static_assert(t.abbrev() == flatstring("min"));
static_assert(std::same_as<decltype(d)::repr_type, double>);
static_assert(sizeof(d) == sizeof(double));
static_assert(d.scale() == 2.5);
static_assert(d.abbrev() == flatstring("km"));
static_assert(std::same_as<decltype(t2)::repr_type, int>);
static_assert(sizeof(t2) == sizeof(int));
static_assert(t2.scale() == 4);
static_assert(t2.abbrev() == flatstring("min^2"));
static_assert(std::same_as<decltype(a)::repr_type, double>);
static_assert(sizeof(a) == sizeof(double));
static_assert(a.scale() == 0.625);
static_assert(a.abbrev() == flatstring("km.min^-2"));
constexpr auto a2 = a.rescale_ext<(u::meter / (u::second * u::second))>();
cerr << "d.t^-2: " << a2 << endl;
static_assert(a2.abbrev() == flatstring("m.s^-2"));
}
/** end ex1.cpp **/

View file

@ -0,0 +1,12 @@
# xo-unit/example/ex2/CMakeLists.txt
set(SELF_EXE xo_unit_ex2)
set(SELF_SRCS ex2.cpp)
if (XO_ENABLE_EXAMPLES)
xo_add_executable(${SELF_EXE} ${SELF_SRCS})
xo_self_headeronly_dependency(${SELF_EXE} xo_unit)
xo_dependency(${SELF_EXE} xo_flatstring)
endif()
# end CMakeLists.txt

View file

@ -0,0 +1,49 @@
/** @file ex2.cpp **/
#include "xo/unit/quantity.hpp"
#include "xo/unit/quantity_iostream.hpp"
#include <iostream>
int
main () {
namespace q = xo::qty::qty;
namespace u = xo::qty::u;
using xo::qty::with_units_from;
using xo::qty::with_units;
using xo::qty::quantity;
using xo::flatstring;
using namespace std;
constexpr auto t = q::minutes(2);
constexpr auto d = q::kilometers(2.5);
constexpr auto t2 = t*t;
constexpr auto a = d / (t*t);
cerr << "t: " << t << ", d: " << d
<< ", t^2: " << t2
<< ", d.t^-2: " << a
<< endl;
constexpr auto a2 = a.rescale_ext<u::meter / (u::second * u::second)>();
static_assert(a2.abbrev() == flatstring("m.s^-2"));
cerr << "a2: " << a2 << endl;
constexpr auto a3 = with_units<u::meter / (u::second * u::second)>(a);
static_assert(a3.abbrev() == flatstring("m.s^-2"));
cerr << "a3: " << a3 << endl;
constexpr auto au = q::meter / (q::second * q::second);
constexpr auto a4 = with_units_from(a, au);
static_assert(a4.abbrev() == flatstring("m.s^-2"));
cerr << "a4: " << a4 << endl;
}
/** end ex2.cpp **/

View file

@ -0,0 +1,12 @@
# xo-unit/example/ex3/CMakeLists.txt
set(SELF_EXE xo_unit_ex3)
set(SELF_SRCS ex3.cpp)
if (XO_ENABLE_EXAMPLES)
xo_add_executable(${SELF_EXE} ${SELF_SRCS})
xo_self_headeronly_dependency(${SELF_EXE} xo_unit)
xo_dependency(${SELF_EXE} xo_flatstring)
endif()
# end CMakeLists.txt

View file

@ -0,0 +1,25 @@
/** @file ex3.cpp **/
#include "xo/unit/quantity.hpp"
#include "xo/unit/quantity_iostream.hpp"
#include <iostream>
int
main () {
namespace q = xo::qty::qty;
namespace u = xo::qty::u;
using xo::qty::quantity;
using namespace std;
constexpr quantity<u::second> t = q::minutes(2);
constexpr quantity<u::meter> d = q::kilometers(2.5);
constexpr auto t2 = t*t;
constexpr auto a = d / (t*t);
cerr << "t: " << t << ", d: " << d
<< ", d.t^-2: " << a
<< endl;
}
/** end ex3.cpp **/

View file

@ -0,0 +1,12 @@
# xo-unit/example/ex4/CMakeLists.txt
set(SELF_EXE xo_unit_ex4)
set(SELF_SRCS ex4.cpp)
if (XO_ENABLE_EXAMPLES)
xo_add_executable(${SELF_EXE} ${SELF_SRCS})
xo_self_headeronly_dependency(${SELF_EXE} xo_unit)
xo_dependency(${SELF_EXE} xo_flatstring)
endif()
# end CMakeLists.txt

View file

@ -0,0 +1,30 @@
/** @file ex4.cpp **/
#include "xo/unit/quantity.hpp"
#include "xo/unit/quantity_iostream.hpp"
#include <iostream>
int
main () {
namespace q = xo::qty::qty;
auto t1 = q::milliseconds(1);
auto t2 = q::minutes(1);
auto r1 = t1 / with_repr<double>(t2);
static_assert(r1.is_dimensionless());
static_assert(!t2.is_dimensionless());
static_assert(std::same_as<decltype(static_cast<double>(r1)), double>);
/* r1_value: assignment compiles, since r1 dimensionless */
double r1_value = r1;
/* r2_value: assignment won't compile, 'cannot convert' error */
//double r2_value = t2;
std::cerr << "t1: " << t1 << ", t2: " << t2 << ", t1/t2: " << r1 << std::endl;
}
/** end ex4.cpp */

View file

@ -0,0 +1,12 @@
# xo-unit/example/ex5/CMakeLists.txt
set(SELF_EXE xo_unit_ex5)
set(SELF_SRCS ex5.cpp)
if (XO_ENABLE_EXAMPLES)
xo_add_executable(${SELF_EXE} ${SELF_SRCS})
xo_self_headeronly_dependency(${SELF_EXE} xo_unit)
xo_dependency(${SELF_EXE} xo_flatstring)
endif()
# end CMakeLists.txt

View file

@ -0,0 +1,24 @@
/** @file ex5.cpp **/
#include "xo/unit/quantity.hpp"
#include "xo/unit/quantity_iostream.hpp"
#include <iostream>
int
main () {
//namespace u = xo::unit::units;
namespace q = xo::qty::qty;
using namespace std;
/* 20% volatility over 250 days (approx number of trading days in one year) */
auto q1 = q::volatility_250d(0.2);
/* 10% volatility over 30 days */
auto q2 = q::volatility_30d(0.1);
auto sum = q1 + q2;
auto prod = q1 * q2;
cerr << "q1: " << q1 << ", q2: " << q2 << ", q1+q2: " << sum << ", q1*q2: " << prod << endl;
}
/** end ex5.cpp */

View file

@ -0,0 +1,12 @@
# xo-unit/example/ex6/CMakeLists.txt
set(SELF_EXE xo_unit_ex6)
set(SELF_SRCS ex6.cpp)
if (XO_ENABLE_EXAMPLES)
xo_add_executable(${SELF_EXE} ${SELF_SRCS})
xo_self_headeronly_dependency(${SELF_EXE} xo_unit)
xo_dependency(${SELF_EXE} xo_flatstring)
endif()
# end CMakeLists.txt

View file

@ -0,0 +1,36 @@
/** @file ex6.cpp **/
#include "xo/unit/quantity.hpp"
#include "xo/unit/quantity_iostream.hpp"
#include <iostream>
int
main () {
using xo::qty::quantity;
namespace q = xo::qty::qty;
using xo::flatstring;
/* 20% volatility over 360 days */
auto q1 = q::volatility_360d(0.2);
/* 10% volatility over 30 days */
auto q2 = q::volatility_30d(0.1);
/* 10% volatility per 30 days
* ~ (10% * sqrt(360/30)) volatility over 360 days
* ~ (10% * 3.4641)
* ~ 0.34641yr360^(-1/2)
*/
auto sum = q1 + q2;
auto prod = q1 * q2;
static_assert(sum.abbrev() == flatstring("yr360^(-1/2)"));
static_assert(prod.abbrev() == flatstring("yr360^-1"));
std::cerr << "q1: " << q1 << std::endl;
std::cerr << "q2: " << q2 << std::endl;
std::cerr << "q1+q2: " << sum << std::endl;
std::cerr << "q1*q2: " << prod << std::endl;
}
/** end ex6.cpp */

View file

@ -0,0 +1,12 @@
# xo-unit/example/ex7/CMakeLists.txt
set(SELF_EXE xo_unit_ex7)
set(SELF_SRCS ex7.cpp)
if (XO_ENABLE_EXAMPLES)
xo_add_executable(${SELF_EXE} ${SELF_SRCS})
xo_self_headeronly_dependency(${SELF_EXE} xo_unit)
xo_dependency(${SELF_EXE} xo_flatstring)
endif()
# end CMakeLists.txt

View file

@ -0,0 +1,31 @@
/** @file ex7.cpp **/
#include "xo/unit/quantity.hpp"
#include "xo/unit/quantity_iostream.hpp"
#include <iostream>
int
main () {
using namespace xo::qty;
namespace u = xo::qty::u;
namespace q = xo::qty::qty;
using namespace std;
quantity qty1 = 7.55 * q::kilometer / q::minute / q::minute;
quantity qty2 = q::nanograms(123);
quantity qty3 = qty2 * qty1;
cerr << "qty1: " << qty1 << endl;
cerr << "qty2: " << qty2 << endl;
cerr << "qty3: " << qty3 << endl;
/* rescale to not-so-absurd units */
/* kg.m.s^-2 */
quantity res = qty3.rescale_ext<u::kilogram * u::meter / (u::second * u::second)>();
/* 2.57958e-10kg.m.s^-2 */
cerr << "res: " << res << endl;
}
/** end ex7.cpp */

View file

@ -0,0 +1,12 @@
# xo-unit/example/ex8/CMakeLists.txt
set(SELF_EXE xo_unit_ex8)
set(SELF_SRCS ex8.cpp)
if (XO_ENABLE_EXAMPLES)
xo_add_executable(${SELF_EXE} ${SELF_SRCS})
xo_self_headeronly_dependency(${SELF_EXE} xo_unit)
xo_dependency(${SELF_EXE} xo_flatstring)
endif()
# end CMakeLists.txt

View file

@ -0,0 +1,37 @@
/** @file ex8.cpp **/
#include "xo/unit/xquantity.hpp"
#include "xo/unit/xquantity_iostream.hpp"
#include <iostream>
int
main () {
using namespace xo::qty;
namespace u = xo::qty::u;
using namespace std;
constexpr xquantity qty1(7, u::foot);
constexpr xquantity qty2(6.0, u::inch);
// constexpr not supported for xquantity addition
xquantity qty3 = qty1 + qty2;
cerr << "qty1: " << qty1 << endl;
cerr << "qty2: " << qty2 << endl;
cerr << "qty3: " << qty3 << endl;
/* rescale to mm */
xquantity res = qty3.rescale_ext(xo::qty::u::millimeter);
/* 2286mm */
cerr << "res: " << res << endl;
/* 12 */
xquantity qty4 = qty1 / qty2;
auto res2 = qty4 + 4;
cerr << "res2: " << res << endl;
}
/** end ex8.cpp */

View file

@ -0,0 +1,12 @@
# xo-unit/example/ex_qty/CMakeLists.txt
set(SELF_EXE xo_unit_ex_qty)
set(SELF_SRCS ex_qty.cpp)
if (XO_ENABLE_EXAMPLES)
xo_add_executable(${SELF_EXE} ${SELF_SRCS})
xo_self_headeronly_dependency(${SELF_EXE} xo_unit)
xo_dependency(${SELF_EXE} xo_flatstring)
endif()
# end CMakeLists.txt

View file

@ -0,0 +1,18 @@
/* @file ex_qty.cpp */
#include "xo/unit/quantity.hpp"
#include "xo/unit/quantity_iostream.hpp"
using namespace std;
int
main() {
using namespace xo::qty;
namespace u = xo::qty::u;
static_assert(u::meter.n_bpu() == 1);
//constexpr auto q = qty::meters(2) + u::meter;
}
/* end ex_qty.cpp */

View file

@ -0,0 +1,12 @@
# xo-unit/example/su/CMakeLists.txt
set(SELF_EXE xo_unit_ex_su)
set(SELF_SRCS ex_su.cpp)
if (XO_ENABLE_EXAMPLES)
xo_add_executable(${SELF_EXE} ${SELF_SRCS})
xo_self_headeronly_dependency(${SELF_EXE} xo_unit)
xo_dependency(${SELF_EXE} xo_flatstring)
endif()
# end CMakeLists.txt

View file

@ -0,0 +1,38 @@
/** @file ex_su.cpp **/
#include "xo/unit/scaled_unit.hpp"
#include "xo/unit/scaled_unit_iostream.hpp"
#include <iostream>
using namespace std;
int
main() {
using namespace xo::qty;
constexpr auto u_prod = u::meter * u::kilometer;
static_assert(u_prod[0].bu() == detail::bu::meter);
static_assert(u_prod[0].power() == power_ratio_type(2));
static_assert(u_prod.outer_scale_factor_ == xo::ratio::ratio<int64_t>(1000));
static_assert(u_prod.outer_scale_sq_ == 1.0); // used if fractional dimension
constexpr auto u_div = u::meter / u::kilometer;
static_assert(u_div.n_bpu() == 0);
static_assert(u_div.outer_scale_factor_ == xo::ratio::ratio<int64_t>(1,1000));
static_assert(u_prod.outer_scale_sq_ == 1.0); // used if fractional dimension
constexpr auto u2_prod = u::meter * u::hour * u::kilometer * u::minute;
static_assert(u2_prod.n_bpu() == 2);
static_assert(u2_prod[0].bu() == detail::bu::meter);
static_assert(u2_prod[1].bu() == detail::bu::hour);
// *1000 from converting kilometers -> meters
// /60 from converting minutes -> hours
// 1000/60 = 50/3 in lowest terms
static_assert(u2_prod.outer_scale_factor_ == xo::ratio::ratio<int64_t>(50,3)); // used if fractional dimension
static_assert(u2_prod.outer_scale_sq_ == 1.0); // used if fractional dimension
}
/** end ex_su.cpp **/