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{ } // namespace length
template <typename Rep = double> namespace acceleration {
using mps2 = units::si::acceleration<units::si::metre_per_second_sq,Rep>;
template <typename Rep = double> template<typename Rep = double>
constexpr mps2<> g{static_cast<Rep>(9.80665)}; using mps2 = units::si::acceleration<units::si::metre_per_second_sq, Rep>;
}
namespace force{ template<typename Rep = double>
constexpr mps2<> g{static_cast<Rep>(9.80665)};
template <typename Rep = double> } // namespace acceleration
using N = units::si::force<units::si::newton,Rep>;
}
namespace mass { namespace force {
template <typename Rep = double> template<typename Rep = double>
using kg = units::si::mass<units::si::kilogram,Rep>; using N = units::si::force<units::si::newton, Rep>;
}
namespace density {
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;
}
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}; static constexpr density::kgpm3<> air_density{1.225};
length::m<> length; length::m<> length;
length::m<> width; length::m<> width;
length::m<> height; 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;
}
}; };
#include <iostream> #include <iostream>
@@ -110,16 +108,11 @@ int main()
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 << "mpusz/units box example...\n";
std::cout << "fill height at " << fill_time << " = " std::cout << "fill height at " << fill_time << " = " << box.fill_level(measured_mass) << " ("
<< box.fill_level(measured_mass) << "( " << (box.fill_level(measured_mass)/ box.height)*100 << "% full)\n"; << (box.fill_level(measured_mass) / box.height) * 100 << "% full)\n";
std::cout << "spare_capacity at " << fill_time << " = " std::cout << "spare_capacity at " << fill_time << " = " << box.spare_capacity(measured_mass) << '\n';
<< box.spare_capacity(measured_mass) <<'\n'; std::cout << "input flow rate after " << fill_time << " = " << measured_mass / fill_time << '\n';
std::cout << "input flow rate after " << fill_time std::cout << "float rise rate = " << box.fill_level(measured_mass) / fill_time << '\n';
<< " = " << measured_mass / fill_time <<'\n'; auto fill_time_left = (box.height / box.fill_level(measured_mass) - 1) * fill_time;
std::cout << "float rise rate = " std::cout << "box full E.T.A. at current flow rate = " << fill_time_left << '\n';
<< 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)
else if( Vt >= 1mV ) std::cout << voltage::mV<>{Vt}; std::cout << Vt;
else if( Vt >= 1uV ) std::cout << voltage::uV<>{Vt}; else if (Vt >= 1mV)
else if( Vt >= 1nV ) std::cout << voltage::nV<>{Vt}; std::cout << voltage::mV<>{Vt};
else std::cout << voltage::pV<>{Vt}; else if (Vt >= 1uV)
std::cout << voltage::uV<>{Vt};
else if (Vt >= 1nV)
std::cout << voltage::nV<>{Vt};
else
std::cout << voltage::pV<>{Vt};
std::cout << "\n"; std::cout << "\n";
} }
} }

View File

@@ -1,4 +1,3 @@
/* /*
Copyright (c) 2003-2019 Andy Little. Copyright (c) 2003-2019 Andy Little.
@@ -16,110 +15,112 @@
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;
@@ -147,7 +148,7 @@ void simple_quantities()
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);
@@ -167,7 +168,7 @@ void quantities_with_typed_units()
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';
@@ -214,10 +215,10 @@ void calcs_comparison()
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';

View File

@@ -1,4 +1,3 @@
/* /*
Copyright (c) 2003-2020 Andy Little. Copyright (c) 2003-2020 Andy Little.
@@ -17,6 +16,7 @@
*/ */
#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
@@ -25,51 +25,41 @@
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
>
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(); return target{source{1}}.count();
} }
// get at the units text of the quantity, without its numeric value // get at the units text of the quantity, without its numeric value
auto inline constexpr units_str( const units::Quantity & q) auto inline constexpr units_str(const units::Quantity& q)
{ {
typedef std::remove_cvref_t<decltype(q)> qtype; typedef std::remove_cvref_t<decltype(q)> qtype;
return units::detail::unit_text<typename qtype::dimension, typename qtype::unit>(); 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";
@@ -78,14 +68,12 @@ int main()
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";
} }