mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-30 18:37:15 +02:00
clang-format on new examples
This commit is contained in:
@ -1,12 +1,10 @@
|
||||
|
||||
|
||||
#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/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>
|
||||
|
||||
namespace {
|
||||
@ -18,7 +16,8 @@ namespace{
|
||||
|
||||
template<typename Rep = double>
|
||||
using mm = units::si::length<units::si::millimetre, Rep>;
|
||||
}
|
||||
|
||||
} // namespace length
|
||||
|
||||
namespace acceleration {
|
||||
|
||||
@ -27,52 +26,61 @@ namespace{
|
||||
|
||||
template<typename Rep = double>
|
||||
constexpr mps2<> g{static_cast<Rep>(9.80665)};
|
||||
}
|
||||
|
||||
} // namespace acceleration
|
||||
|
||||
namespace force {
|
||||
|
||||
template<typename Rep = double>
|
||||
using N = units::si::force<units::si::newton, Rep>;
|
||||
|
||||
}
|
||||
|
||||
namespace mass {
|
||||
|
||||
template<typename Rep = double>
|
||||
using kg = units::si::mass<units::si::kilogram, 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>;
|
||||
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
struct Box {
|
||||
static constexpr density::kgpm3<> air_density{1.225};
|
||||
|
||||
Box(const length::m<> & l,
|
||||
const length::m<> & w,
|
||||
const length::m<> & h
|
||||
): length{l},width{w},height{h}{}
|
||||
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 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();
|
||||
return height * (measured_mass * acceleration::g<>) / filled_weight();
|
||||
}
|
||||
|
||||
volume::m3<> spare_capacity(const mass::kg<>& measured_mass) const
|
||||
@ -80,22 +88,12 @@ struct Box{
|
||||
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)
|
||||
{
|
||||
assert(density_in > air_density);
|
||||
contents.density = density_in;
|
||||
}
|
||||
|
||||
static constexpr density::kgpm3<> air_density{1.225};
|
||||
|
||||
length::m<> length;
|
||||
length::m<> width;
|
||||
length::m<> height;
|
||||
};
|
||||
|
||||
#include <iostream>
|
||||
@ -110,16 +108,11 @@ int main()
|
||||
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 << "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';
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
/*
|
||||
Copyright (c) 2003-2020 Andy Little.
|
||||
|
||||
@ -23,9 +22,10 @@
|
||||
|
||||
#include <units/physical/si/capacitance.h>
|
||||
#include <units/physical/si/resistance.h>
|
||||
#include <units/physical/si/voltage.h>
|
||||
#include <units/physical/si/time.h>
|
||||
#include <units/physical/si/voltage.h>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
namespace voltage {
|
||||
@ -44,12 +44,12 @@ namespace {
|
||||
|
||||
template<typename Rep = double>
|
||||
using pV = units::si::voltage<units::si::picovolt, Rep>;
|
||||
}
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
} // namespace voltage
|
||||
} // namespace
|
||||
|
||||
using namespace units::si::literals;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "mpusz/units capacitor time curve example...\n";
|
||||
@ -61,16 +61,20 @@ int main()
|
||||
constexpr auto R = 4.7kR;
|
||||
|
||||
for (auto t = 0ms; t <= 50ms; ++t) {
|
||||
|
||||
const auto Vt = V0 * std::exp(-t / (R * C));
|
||||
|
||||
std::cout << "at " << t << " voltage is ";
|
||||
|
||||
if ( Vt >= 1V ) std::cout << Vt ;
|
||||
else if( Vt >= 1mV ) std::cout << voltage::mV<>{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};
|
||||
if (Vt >= 1V)
|
||||
std::cout << Vt;
|
||||
else if (Vt >= 1mV)
|
||||
std::cout << voltage::mV<>{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";
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
/*
|
||||
Copyright (c) 2003-2019 Andy Little.
|
||||
|
||||
@ -16,15 +15,16 @@
|
||||
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/typographic/length.h>
|
||||
#include <units/physical/imperial/length.h>
|
||||
#include <units/physical/international/length.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/volume.h>
|
||||
#include <units/physical/typographic/length.h>
|
||||
#include <units/physical/us/length.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
namespace length {
|
||||
@ -95,7 +95,7 @@ namespace {
|
||||
template<typename Rep = double>
|
||||
using yd = units::si::length<units::international::yard, Rep>;
|
||||
|
||||
}
|
||||
} // namespace length
|
||||
|
||||
namespace time {
|
||||
|
||||
@ -107,7 +107,8 @@ namespace {
|
||||
|
||||
template<typename Rep = double>
|
||||
using h = units::si::time<units::si::hour, Rep>;
|
||||
}
|
||||
|
||||
} // namespace time
|
||||
|
||||
namespace area {
|
||||
|
||||
@ -116,10 +117,10 @@ namespace {
|
||||
|
||||
template<typename Rep = double>
|
||||
using fm2 = units::si::area<units::si::square_femtometre, Rep>;
|
||||
}
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
} // namespace area
|
||||
} // namespace
|
||||
|
||||
|
||||
using namespace units::si::literals;
|
||||
using namespace units::international;
|
||||
@ -147,7 +148,7 @@ void simple_quantities()
|
||||
|
||||
void quantities_with_typed_units()
|
||||
{
|
||||
constexpr length::km<> km = 1km;
|
||||
constexpr length::km<> km = 1.0km;
|
||||
constexpr length::mi<> miles = 1.0mi;
|
||||
|
||||
std::cout.precision(6);
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
/*
|
||||
Copyright (c) 2003-2020 Andy Little.
|
||||
|
||||
@ -17,6 +16,7 @@
|
||||
*/
|
||||
|
||||
#include <units/physical/si/length.h>
|
||||
#include <iostream>
|
||||
|
||||
/*
|
||||
get conversion factor from one dimensionally equivalent
|
||||
@ -25,23 +25,12 @@
|
||||
|
||||
namespace {
|
||||
|
||||
template <
|
||||
units::Quantity Target,
|
||||
units::Quantity Source
|
||||
>
|
||||
template<units::Quantity Target, units::Quantity Source>
|
||||
requires units::equivalent_dim<typename Source::dimension, typename Target::dimension>
|
||||
constexpr inline
|
||||
std::common_type_t<
|
||||
typename Target::rep,
|
||||
typename Source::rep
|
||||
>
|
||||
conversion_factor(Target , Source)
|
||||
constexpr inline std::common_type_t<typename Target::rep, 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;
|
||||
// get quantities looking like inputs but with Q::rep that doesn't 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();
|
||||
@ -53,7 +42,8 @@ namespace {
|
||||
typedef std::remove_cvref_t<decltype(q)> qtype;
|
||||
return units::detail::unit_text<typename qtype::dimension, typename qtype::unit>();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace {
|
||||
|
||||
@ -64,12 +54,12 @@ namespace {
|
||||
|
||||
template<typename Rep = double>
|
||||
using mm = units::si::length<units::si::millimetre, Rep>;
|
||||
}
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
} // namespace length
|
||||
} // namespace
|
||||
|
||||
using namespace units::si::literals;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "conversion factor in mpusz/units...\n\n";
|
||||
@ -78,14 +68,12 @@ int main()
|
||||
constexpr length::mm<> lengthB = lengthA;
|
||||
|
||||
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 << "conversion factor from "
|
||||
"lengthA::unit of " << units_str(lengthA)
|
||||
<< " to lengthB::unit of " << units_str(lengthB) << " :\n\n"
|
||||
"lengthB.count( " << lengthB.count() << " ) == "
|
||||
"lengthA.count( " << lengthA.count() << " ) * "
|
||||
"conversion_factor( " << conversion_factor(lengthB, lengthA) << " )\n";
|
||||
std::cout << "conversion factor from lengthA::unit of "
|
||||
<< units_str(lengthA) << " to lengthB::unit of " << units_str(lengthB) << " :\n\n"
|
||||
<< "lengthB.count( " << lengthB.count() << " ) == lengthA.count( " << lengthA.count()
|
||||
<< " ) * conversion_factor( " << conversion_factor(lengthB, lengthA) << " )\n";
|
||||
}
|
||||
|
Reference in New Issue
Block a user