clang-format on new examples

This commit is contained in:
Mateusz Pusz
2020-01-14 12:50:30 +01:00
parent 1c4624303e
commit 15e656aba6
4 changed files with 311 additions and 325 deletions

View File

@@ -1,101 +1,99 @@
#include <units/physical/si/acceleration.h> #include <units/physical/si/acceleration.h>
#include <units/physical/si/length.h>
#include <units/physical/si/volume.h>
#include <units/physical/si/time.h>
#include <units/physical/si/force.h>
#include <units/physical/si/mass.h>
#include <units/physical/si/density.h> #include <units/physical/si/density.h>
#include <units/physical/si/force.h>
#include <units/physical/si/length.h>
#include <units/physical/si/mass.h>
#include <units/physical/si/time.h>
#include <units/physical/si/volume.h>
#include <cassert> #include <cassert>
namespace{ namespace {
namespace length{ namespace length {
template <typename Rep = double> template<typename Rep = double>
using m = units::si::length<units::si::metre,Rep>; using m = units::si::length<units::si::metre, Rep>;
template <typename Rep = double> template<typename Rep = double>
using mm = units::si::length<units::si::millimetre,Rep>; using mm = units::si::length<units::si::millimetre, Rep>;
}
namespace acceleration{
template <typename Rep = double> } // namespace length
using mps2 = units::si::acceleration<units::si::metre_per_second_sq,Rep>;
template <typename Rep = double> namespace acceleration {
constexpr mps2<> g{static_cast<Rep>(9.80665)};
}
namespace force{ template<typename Rep = double>
using mps2 = units::si::acceleration<units::si::metre_per_second_sq, Rep>;
template <typename Rep = double> template<typename Rep = double>
using N = units::si::force<units::si::newton,Rep>; constexpr mps2<> g{static_cast<Rep>(9.80665)};
}
namespace mass { } // namespace acceleration
template <typename Rep = double> namespace force {
using kg = units::si::mass<units::si::kilogram,Rep>;
}
namespace density { template<typename Rep = double>
using N = units::si::force<units::si::newton, Rep>;
template <typename Rep = double>
using kgpm3 = units::si::density<units::si::kilogram_per_metre_cub,Rep>;
}
namespace volume {
template <typename Rep = double>
using m3 = units::si::volume<units::si::cubic_metre,Rep>;
}
} }
struct Box{ namespace mass {
Box(const length::m<> & l, template<typename Rep = double>
const length::m<> & w, using kg = units::si::mass<units::si::kilogram, Rep>;
const length::m<> & h
): length{l},width{w},height{h}{}
force::N<> filled_weight()const }
{
const volume::m3<> volume
= length * width * height;
const mass::kg<> mass = contents.density * volume;
return mass * acceleration::g<>;
}
length::m<> fill_level(const mass::kg<> & measured_mass)const namespace density {
{
return height
* (measured_mass * acceleration::g<>) / filled_weight();
}
volume::m3<> spare_capacity(const mass::kg<> & measured_mass)const template<typename Rep = double>
{ using kgpm3 = units::si::density<units::si::kilogram_per_metre_cub, Rep>;
return (height - fill_level(measured_mass)) * width * length;
}
struct contents{ }
contents():density{air_density}{}
density::kgpm3<> density;
}contents;
void set_contents_density(const density::kgpm3<> & density_in) namespace volume {
{
assert( density_in > air_density );
contents.density = density_in;
}
static constexpr density::kgpm3<> air_density{1.225}; template<typename Rep = double>
using m3 = units::si::volume<units::si::cubic_metre, Rep>;
}
} // namespace
struct Box {
static constexpr density::kgpm3<> air_density{1.225};
length::m<> length;
length::m<> width;
length::m<> height;
struct contents {
density::kgpm3<> density = air_density;
} contents;
Box(const length::m<>& l, const length::m<>& w, const length::m<>& h) : length{l}, width{w}, height{h} {}
force::N<> filled_weight() const
{
const volume::m3<> volume = length * width * height;
const mass::kg<> mass = contents.density * volume;
return mass * acceleration::g<>;
}
length::m<> fill_level(const mass::kg<>& measured_mass) const
{
return height * (measured_mass * acceleration::g<>) / filled_weight();
}
volume::m3<> spare_capacity(const mass::kg<>& measured_mass) const
{
return (height - fill_level(measured_mass)) * width * length;
}
void set_contents_density(const density::kgpm3<>& density_in)
{
assert(density_in > air_density);
contents.density = density_in;
}
length::m<> length;
length::m<> width;
length::m<> height;
}; };
#include <iostream> #include <iostream>
@@ -103,23 +101,18 @@ struct Box{
using namespace units::si::literals; using namespace units::si::literals;
int main() int main()
{ {
auto box = Box{1000.0mm, 500.0mm, 200.0mm}; auto box = Box{1000.0mm, 500.0mm, 200.0mm};
box.set_contents_density(1000.0kgpm3); box.set_contents_density(1000.0kgpm3);
auto fill_time = 200.0s; // time since starting fill auto fill_time = 200.0s; // time since starting fill
auto measured_mass = 20.0kg; // measured mass at fill_time auto measured_mass = 20.0kg; // measured mass at fill_time
std::cout << "mpusz/units box example...\n";
std::cout << "fill height at " << fill_time << " = "
<< box.fill_level(measured_mass) << "( " << (box.fill_level(measured_mass)/ box.height)*100 << "% full)\n";
std::cout << "spare_capacity at " << fill_time << " = "
<< box.spare_capacity(measured_mass) <<'\n';
std::cout << "input flow rate after " << fill_time
<< " = " << measured_mass / fill_time <<'\n';
std::cout << "float rise rate = "
<< box.fill_level(measured_mass) / fill_time <<'\n';
auto fill_time_left
= (box.height / box.fill_level(measured_mass) - 1) * fill_time ;
std::cout << "box full E.T.A. at current flow rate = " << fill_time_left <<'\n';
std::cout << "mpusz/units box example...\n";
std::cout << "fill height at " << fill_time << " = " << box.fill_level(measured_mass) << " ("
<< (box.fill_level(measured_mass) / box.height) * 100 << "% full)\n";
std::cout << "spare_capacity at " << fill_time << " = " << box.spare_capacity(measured_mass) << '\n';
std::cout << "input flow rate after " << fill_time << " = " << measured_mass / fill_time << '\n';
std::cout << "float rise rate = " << box.fill_level(measured_mass) / fill_time << '\n';
auto fill_time_left = (box.height / box.fill_level(measured_mass) - 1) * fill_time;
std::cout << "box full E.T.A. at current flow rate = " << fill_time_left << '\n';
} }

View File

@@ -1,4 +1,3 @@
/* /*
Copyright (c) 2003-2020 Andy Little. Copyright (c) 2003-2020 Andy Little.
@@ -23,54 +22,59 @@
#include <units/physical/si/capacitance.h> #include <units/physical/si/capacitance.h>
#include <units/physical/si/resistance.h> #include <units/physical/si/resistance.h>
#include <units/physical/si/voltage.h>
#include <units/physical/si/time.h> #include <units/physical/si/time.h>
#include <units/physical/si/voltage.h>
#include <cmath> #include <cmath>
namespace {
namespace voltage {
template <typename Rep = double>
using V = units::si::voltage<units::si::volt,Rep>;
template <typename Rep = double>
using mV = units::si::voltage<units::si::millivolt,Rep>;
template <typename Rep = double>
using uV = units::si::voltage<units::si::microvolt,Rep>;
template <typename Rep = double>
using nV = units::si::voltage<units::si::nanovolt,Rep>;
template <typename Rep = double>
using pV = units::si::voltage<units::si::picovolt,Rep>;
}
}
#include <iostream> #include <iostream>
namespace {
namespace voltage {
template<typename Rep = double>
using V = units::si::voltage<units::si::volt, Rep>;
template<typename Rep = double>
using mV = units::si::voltage<units::si::millivolt, Rep>;
template<typename Rep = double>
using uV = units::si::voltage<units::si::microvolt, Rep>;
template<typename Rep = double>
using nV = units::si::voltage<units::si::nanovolt, Rep>;
template<typename Rep = double>
using pV = units::si::voltage<units::si::picovolt, Rep>;
} // namespace voltage
} // namespace
using namespace units::si::literals; using namespace units::si::literals;
int main() int main()
{ {
std::cout << "mpusz/units capacitor time curve example...\n"; std::cout << "mpusz/units capacitor time curve example...\n";
std::cout.setf(std::ios_base::fixed,std::ios_base::floatfield); std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout.precision(3); std::cout.precision(3);
constexpr auto C = 0.47uF; constexpr auto C = 0.47uF;
constexpr auto V0 = 5.0V; constexpr auto V0 = 5.0V;
constexpr auto R = 4.7kR; constexpr auto R = 4.7kR;
for ( auto t = 0ms ; t <= 50ms; ++t ){ for (auto t = 0ms; t <= 50ms; ++t) {
const auto Vt = V0 * std::exp(-t / (R * C));
const auto Vt = V0 * std::exp(-t / (R * C)); std::cout << "at " << t << " voltage is ";
std::cout << "at " << t << " voltage is " ; if (Vt >= 1V)
std::cout << Vt;
if ( Vt >= 1V ) std::cout << Vt ; else if (Vt >= 1mV)
else if( Vt >= 1mV ) std::cout << voltage::mV<>{Vt}; std::cout << voltage::mV<>{Vt};
else if( Vt >= 1uV ) std::cout << voltage::uV<>{Vt}; else if (Vt >= 1uV)
else if( Vt >= 1nV ) std::cout << voltage::nV<>{Vt}; std::cout << voltage::uV<>{Vt};
else std::cout << voltage::pV<>{Vt}; else if (Vt >= 1nV)
std::cout << "\n"; std::cout << voltage::nV<>{Vt};
} else
std::cout << voltage::pV<>{Vt};
std::cout << "\n";
}
} }

View File

@@ -1,4 +1,3 @@
/* /*
Copyright (c) 2003-2019 Andy Little. Copyright (c) 2003-2019 Andy Little.
@@ -16,220 +15,222 @@
along with this program. If not, see http://www.gnu.org/licenses./ along with this program. If not, see http://www.gnu.org/licenses./
*/ */
#include <units/physical/si/length.h>
#include <units/physical/international/length.h>
#include <units/physical/us/length.h>
#include <units/physical/iau/length.h> #include <units/physical/iau/length.h>
#include <units/physical/typographic/length.h>
#include <units/physical/imperial/length.h> #include <units/physical/imperial/length.h>
#include <units/physical/international/length.h>
#include <units/physical/si/area.h> #include <units/physical/si/area.h>
#include <units/physical/si/volume.h> #include <units/physical/si/length.h>
#include <units/physical/si/time.h> #include <units/physical/si/time.h>
#include <units/physical/si/volume.h>
#include <units/physical/typographic/length.h>
#include <units/physical/us/length.h>
#include <iostream>
namespace { namespace {
namespace length{ namespace length {
template <typename Rep = double> template<typename Rep = double>
using m = units::si::length<units::si::metre,Rep>; using m = units::si::length<units::si::metre, Rep>;
template <typename Rep = double> template<typename Rep = double>
using mm = units::si::length<units::si::millimetre,Rep>; using mm = units::si::length<units::si::millimetre, Rep>;
template <typename Rep = double> template<typename Rep = double>
using fm = units::si::length<units::si::femtometre,Rep>; using fm = units::si::length<units::si::femtometre, Rep>;
template <typename Rep = double> template<typename Rep = double>
using km = units::si::length<units::si::kilometre,Rep>; using km = units::si::length<units::si::kilometre, Rep>;
template <typename Rep = double> template<typename Rep = double>
using AU = units::si::length<units::si::astronomical_unit,Rep>; using AU = units::si::length<units::si::astronomical_unit, Rep>;
template <typename Rep = double> template<typename Rep = double>
using in = units::si::length<units::international::inch,Rep>; using in = units::si::length<units::international::inch, Rep>;
template <typename Rep = double> template<typename Rep = double>
using angstrom = units::si::length<units::iau::angstrom,Rep>; using angstrom = units::si::length<units::iau::angstrom, Rep>;
template <typename Rep = double> template<typename Rep = double>
using ch = units::si::length<units::imperial::chain,Rep>; using ch = units::si::length<units::imperial::chain, Rep>;
template <typename Rep = double> template<typename Rep = double>
using fathom = units::si::length<units::international::fathom,Rep>; using fathom = units::si::length<units::international::fathom, Rep>;
template <typename Rep = double> template<typename Rep = double>
using fathom_us = units::si::length<units::us::fathom,Rep>; using fathom_us = units::si::length<units::us::fathom, Rep>;
template <typename Rep = double> template<typename Rep = double>
using ft = units::si::length<units::international::foot,Rep>; using ft = units::si::length<units::international::foot, Rep>;
template <typename Rep = double> template<typename Rep = double>
using ft_us = units::si::length<units::us::foot,Rep>; using ft_us = units::si::length<units::us::foot, Rep>;
template <typename Rep = double> template<typename Rep = double>
using ly = units::si::length<units::iau::light_year,Rep>; using ly = units::si::length<units::iau::light_year, Rep>;
template <typename Rep = double> template<typename Rep = double>
using mi = units::si::length<units::international::mile,Rep>; using mi = units::si::length<units::international::mile, Rep>;
template <typename Rep = double> template<typename Rep = double>
using mi_naut = units::si::length<units::international::nautical_mile,Rep>; using mi_naut = units::si::length<units::international::nautical_mile, Rep>;
template <typename Rep = double> template<typename Rep = double>
using pc = units::si::length<units::iau::parsec,Rep>; using pc = units::si::length<units::iau::parsec, Rep>;
template <typename Rep = double> template<typename Rep = double>
using pica_comp = units::si::length<units::typographic::pica_comp,Rep>; using pica_comp = units::si::length<units::typographic::pica_comp, Rep>;
template <typename Rep = double> template<typename Rep = double>
using pica_prn = units::si::length<units::typographic::pica_prn,Rep>; using pica_prn = units::si::length<units::typographic::pica_prn, Rep>;
template <typename Rep = double> template<typename Rep = double>
using point_comp = units::si::length<units::typographic::point_comp,Rep>; using point_comp = units::si::length<units::typographic::point_comp, Rep>;
template <typename Rep = double> template<typename Rep = double>
using point_prn = units::si::length<units::typographic::point_prn,Rep>; using point_prn = units::si::length<units::typographic::point_prn, Rep>;
template <typename Rep = double> template<typename Rep = double>
using rd = units::si::length<units::imperial::rod,Rep>; using rd = units::si::length<units::imperial::rod, Rep>;
template <typename Rep = double> template<typename Rep = double>
using yd = units::si::length<units::international::yard,Rep>; using yd = units::si::length<units::international::yard, Rep>;
} } // namespace length
namespace time{ namespace time {
template <typename Rep = double> template<typename Rep = double>
using s = units::si::time<units::si::second,Rep>; using s = units::si::time<units::si::second, Rep>;
template <typename Rep = double> template<typename Rep = double>
using min = units::si::time<units::si::minute,Rep>; using min = units::si::time<units::si::minute, Rep>;
template <typename Rep = double> template<typename Rep = double>
using h = units::si::time<units::si::hour,Rep>; using h = units::si::time<units::si::hour, Rep>;
}
namespace area{ } // namespace time
template <typename Rep = double> namespace area {
using m2 = units::si::area<units::si::square_metre,Rep>;
template <typename Rep = double> template<typename Rep = double>
using fm2 = units::si::area<units::si::square_femtometre,Rep>; using m2 = units::si::area<units::si::square_metre, Rep>;
}
} template<typename Rep = double>
using fm2 = units::si::area<units::si::square_femtometre, Rep>;
} // namespace area
} // namespace
#include <iostream>
using namespace units::si::literals; using namespace units::si::literals;
using namespace units::international; using namespace units::international;
void simple_quantities() void simple_quantities()
{ {
using distance = length::m<>; using distance = length::m<>;
using time = time::s<>; using time = time::s<>;
constexpr distance km = 1.0km; constexpr distance km = 1.0km;
constexpr distance miles = 1.0mi; constexpr distance miles = 1.0mi;
constexpr time sec = 1s; constexpr time sec = 1s;
constexpr time min = 1min; constexpr time min = 1min;
constexpr time hr = 1h; constexpr time hr = 1h;
std::cout << "A physical quantities library can choose the simple\n"; std::cout << "A physical quantities library can choose the simple\n";
std::cout << "option to provide output using a single type for each base unit:\n\n"; std::cout << "option to provide output using a single type for each base unit:\n\n";
std::cout << km << '\n'; std::cout << km << '\n';
std::cout << miles << '\n'; std::cout << miles << '\n';
std::cout << sec << '\n'; std::cout << sec << '\n';
std::cout << min << '\n'; std::cout << min << '\n';
std::cout << hr << "\n\n"; std::cout << hr << "\n\n";
} }
void quantities_with_typed_units() void quantities_with_typed_units()
{ {
constexpr length::km<> km = 1km; constexpr length::km<> km = 1.0km;
constexpr length::mi<> miles = 1.0mi; constexpr length::mi<> miles = 1.0mi;
std::cout.precision(6); std::cout.precision(6);
constexpr time::s<> sec = 1s; constexpr time::s<> sec = 1s;
constexpr time::min<> min = 1min; constexpr time::min<> min = 1min;
constexpr time::h<> hr = 1h; constexpr time::h<> hr = 1h;
std::cout << "A more flexible option is to provide separate types for each unit,\n\n"; std::cout << "A more flexible option is to provide separate types for each unit,\n\n";
std::cout << km << '\n'; std::cout << km << '\n';
std::cout << miles << '\n'; std::cout << miles << '\n';
std::cout << sec << '\n'; std::cout << sec << '\n';
std::cout << min << '\n'; std::cout << min << '\n';
std::cout << hr << "\n\n"; std::cout << hr << "\n\n";
constexpr length::m<> meter = 1m; constexpr length::m<> meter = 1m;
std::cout << "then a wide range of pre-defined units can be defined and converted,\n" std::cout << "then a wide range of pre-defined units can be defined and converted,\n"
" for consistency and repeatability across applications:\n\n"; " for consistency and repeatability across applications:\n\n";
std::cout << meter << '\n' ; std::cout << meter << '\n';
std::cout << " = " << length::AU<>(meter) << '\n'; std::cout << " = " << length::AU<>(meter) << '\n';
std::cout << " = " << length::angstrom<>(meter) << '\n'; std::cout << " = " << length::angstrom<>(meter) << '\n';
std::cout << " = " << length::ch<>(meter) << '\n'; std::cout << " = " << length::ch<>(meter) << '\n';
std::cout << " = " << length::fathom<>(meter) << '\n'; std::cout << " = " << length::fathom<>(meter) << '\n';
std::cout << " = " << length::fathom_us<>(meter) << '\n'; std::cout << " = " << length::fathom_us<>(meter) << '\n';
std::cout << " = " << length::ft<>(meter) << '\n'; std::cout << " = " << length::ft<>(meter) << '\n';
std::cout << " = " << length::ft_us<>(meter) << '\n'; std::cout << " = " << length::ft_us<>(meter) << '\n';
std::cout << " = " << length::in<>(meter) << '\n'; std::cout << " = " << length::in<>(meter) << '\n';
std::cout << " = " << length::ly<>(meter) << '\n'; std::cout << " = " << length::ly<>(meter) << '\n';
std::cout << " = " << length::mi<>(meter) << '\n'; std::cout << " = " << length::mi<>(meter) << '\n';
std::cout << " = " << length::mi_naut<>(meter) << '\n'; std::cout << " = " << length::mi_naut<>(meter) << '\n';
std::cout << " = " << length::pc<>(meter) << '\n'; std::cout << " = " << length::pc<>(meter) << '\n';
std::cout << " = " << length::pica_comp<>(meter) << '\n'; std::cout << " = " << length::pica_comp<>(meter) << '\n';
std::cout << " = " << length::pica_prn<>(meter) << '\n'; std::cout << " = " << length::pica_prn<>(meter) << '\n';
std::cout << " = " << length::point_comp<>(meter) << '\n'; std::cout << " = " << length::point_comp<>(meter) << '\n';
std::cout << " = " << length::point_prn<>(meter) << '\n'; std::cout << " = " << length::point_prn<>(meter) << '\n';
std::cout << " = " << length::rd<>(meter) << '\n'; std::cout << " = " << length::rd<>(meter) << '\n';
std::cout << " = " << length::yd<>(meter) << '\n'; std::cout << " = " << length::yd<>(meter) << '\n';
} }
void calcs_comparison() void calcs_comparison()
{ {
std::cout.precision(20); std::cout.precision(20);
std::cout << "\nA distinct unit for each type is efficient and accurate\n" std::cout << "\nA distinct unit for each type is efficient and accurate\n"
"when adding two values of the same very big\n" "when adding two values of the same very big\n"
"or very small type:\n\n"; "or very small type:\n\n";
length::fm<float> L1A = 2fm; length::fm<float> L1A = 2fm;
length::fm<float> L2A = 3fm; length::fm<float> L2A = 3fm;
length::fm<float> LrA = L1A + L2A; length::fm<float> LrA = L1A + L2A;
std::cout << L1A << " + " << L2A << " = " << LrA << "\n\n"; std::cout << L1A << " + " << L2A << " = " << LrA << "\n\n";
std::cout << "The single unit method must convert large\n" std::cout << "The single unit method must convert large\n"
"or small values in other units to the base unit.\n" "or small values in other units to the base unit.\n"
"This is both inefficient and inaccurate\n\n"; "This is both inefficient and inaccurate\n\n";
length::m<float> L1B = L1A; length::m<float> L1B = L1A;
length::m<float> L2B = L2A; length::m<float> L2B = L2A;
length::m<float> LrB = L1B + L2B; length::m<float> LrB = L1B + L2B;
std::cout << L1B << " + " << L2B << " = " << LrB << "\n\n"; std::cout << L1B << " + " << L2B << " = " << LrB << "\n\n";
std::cout << "In multiplication and division:\n\n"; std::cout << "In multiplication and division:\n\n";
area::fm2<float> ArA = L1A * L2A ; area::fm2<float> ArA = L1A * L2A;
std::cout << L1A << " * " << L2A << " = " << ArA << "\n\n"; std::cout << L1A << " * " << L2A << " = " << ArA << "\n\n";
std::cout <<"similar problems arise\n\n"; std::cout << "similar problems arise\n\n";
area::m2<float> ArB = L1B * L2B; area::m2<float> ArB = L1B * L2B;
std::cout << L1B << " * " << L2B << "\n = " << ArB << '\n'; std::cout << L1B << " * " << L2B << "\n = " << ArB << '\n';
} }
int main() int main()
{ {
std::cout << "This demo was originally posted on com.lang.c++.moderated in 2006\n"; std::cout << "This demo was originally posted on com.lang.c++.moderated in 2006\n";
std::cout << "http://compgroups.net/comp.lang.c++.moderated/dimensional-analysis-units/51712\n"; std::cout << "http://compgroups.net/comp.lang.c++.moderated/dimensional-analysis-units/51712\n";
std::cout << "Here converted to use mpusz/units library.\n\n"; std::cout << "Here converted to use mpusz/units library.\n\n";
simple_quantities(); simple_quantities();
quantities_with_typed_units(); quantities_with_typed_units();
calcs_comparison(); calcs_comparison();
} }

View File

@@ -1,4 +1,3 @@
/* /*
Copyright (c) 2003-2020 Andy Little. Copyright (c) 2003-2020 Andy Little.
@@ -17,75 +16,64 @@
*/ */
#include <units/physical/si/length.h> #include <units/physical/si/length.h>
#include <iostream>
/* /*
get conversion factor from one dimensionally equivalent get conversion factor from one dimensionally equivalent
quantity type to another quantity type to another
*/ */
namespace { namespace {
template < template<units::Quantity Target, units::Quantity Source>
units::Quantity Target, requires units::equivalent_dim<typename Source::dimension, typename Target::dimension>
units::Quantity Source constexpr inline std::common_type_t<typename Target::rep, typename Source::rep> conversion_factor(Target, Source)
> {
requires units::equivalent_dim<typename Source::dimension,typename Target::dimension> // get quantities looking like inputs but with Q::rep that doesn't have narrowing conversion
constexpr inline typedef std::common_type_t<typename Target::rep, typename Source::rep> rep;
std::common_type_t< typedef units::quantity<typename Source::dimension, typename Source::unit, rep> source;
typename Target::rep, typedef units::quantity<typename Target::dimension, typename Target::unit, rep> target;
typename Source::rep return target{source{1}}.count();
>
conversion_factor(Target , Source)
{
// get quantities looking like inputs but with Q::rep that doesnt have narrowing conversion
typedef std::common_type_t<
typename Target::rep,
typename Source::rep
> rep;
typedef units::quantity<typename Source::dimension,typename Source::unit,rep> source;
typedef units::quantity<typename Target::dimension,typename Target::unit,rep> target;
return target{source{1}}.count();
}
// get at the units text of the quantity, without its numeric value
auto inline constexpr units_str( const units::Quantity & q)
{
typedef std::remove_cvref_t<decltype(q)> qtype;
return units::detail::unit_text<typename qtype::dimension, typename qtype::unit>();
}
} }
// get at the units text of the quantity, without its numeric value
auto inline constexpr units_str(const units::Quantity& q)
{
typedef std::remove_cvref_t<decltype(q)> qtype;
return units::detail::unit_text<typename qtype::dimension, typename qtype::unit>();
}
} // namespace
namespace { namespace {
namespace length{ namespace length {
template <typename Rep = double> template<typename Rep = double>
using m = units::si::length<units::si::metre,Rep>; using m = units::si::length<units::si::metre, Rep>;
template <typename Rep = double> template<typename Rep = double>
using mm = units::si::length<units::si::millimetre,Rep>; using mm = units::si::length<units::si::millimetre, Rep>;
}
}
#include <iostream> } // namespace length
} // namespace
using namespace units::si::literals; using namespace units::si::literals;
int main() int main()
{ {
std::cout << "conversion factor in mpusz/units...\n\n"; std::cout << "conversion factor in mpusz/units...\n\n";
constexpr length::m<> lengthA = 2.0m; constexpr length::m<> lengthA = 2.0m;
constexpr length::mm<> lengthB = lengthA; constexpr length::mm<> lengthB = lengthA;
std::cout << "lengthA( " << lengthA << " ) and lengthB( " << lengthB << " )\n" std::cout << "lengthA( " << lengthA << " ) and lengthB( " << lengthB << " )\n"
"represent the same length in different units.\n\n"; << "represent the same length in different units.\n\n";
std::cout << "therefore ratio lengthA / lengthB == " << lengthA / lengthB << "\n\n"; std::cout << "therefore ratio lengthA / lengthB == " << lengthA / lengthB << "\n\n";
std::cout << "conversion factor from " std::cout << "conversion factor from lengthA::unit of "
"lengthA::unit of " << units_str(lengthA) << units_str(lengthA) << " to lengthB::unit of " << units_str(lengthB) << " :\n\n"
<< " to lengthB::unit of " << units_str(lengthB) << " :\n\n" << "lengthB.count( " << lengthB.count() << " ) == lengthA.count( " << lengthA.count()
"lengthB.count( " << lengthB.count() << " ) == " << " ) * conversion_factor( " << conversion_factor(lengthB, lengthA) << " )\n";
"lengthA.count( " << lengthA.count() << " ) * "
"conversion_factor( " << conversion_factor(lengthB, lengthA) << " )\n";
} }