mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-31 19:04:27 +02:00
refactor: example applications refactored to benefit more from references
This commit is contained in:
@@ -74,8 +74,8 @@ void example()
|
||||
// SI (int)
|
||||
{
|
||||
using namespace units::isq::si::references;
|
||||
constexpr Length auto distance = 220 * km; // constructed from a reference
|
||||
constexpr si::time<si::hour, int> duration(2); // constructed from a value
|
||||
constexpr auto distance = 220 * km;
|
||||
constexpr auto duration = 2 * h;
|
||||
|
||||
std::cout << "SI units with 'int' as representation\n";
|
||||
|
||||
@@ -88,8 +88,8 @@ void example()
|
||||
// SI (double)
|
||||
{
|
||||
using namespace units::isq::si::references;
|
||||
constexpr Length auto distance = 220. * km; // constructed from a reference
|
||||
constexpr si::time<si::hour> duration(2); // constructed from a value
|
||||
constexpr auto distance = 220. * km;
|
||||
constexpr auto duration = 2. * h;
|
||||
|
||||
std::cout << "\nSI units with 'double' as representation\n";
|
||||
|
||||
@@ -103,9 +103,10 @@ void example()
|
||||
|
||||
// Customary Units (int)
|
||||
{
|
||||
using namespace units::isq::si::international::references;
|
||||
constexpr Length auto distance = 140 * mi; // constructed from a reference
|
||||
constexpr si::time<si::hour, int> duration(2); // constructed from a value
|
||||
using namespace units::isq::si::time_references;
|
||||
using namespace units::isq::si::international::length_references;
|
||||
constexpr auto distance = 140 * mi;
|
||||
constexpr auto duration = 2 * h;
|
||||
|
||||
std::cout << "\nUS Customary Units with 'int' as representation\n";
|
||||
|
||||
@@ -119,9 +120,10 @@ void example()
|
||||
|
||||
// Customary Units (double)
|
||||
{
|
||||
using namespace units::isq::si::international::references;
|
||||
constexpr Length auto distance = 140. * mi; // constructed from a reference
|
||||
constexpr si::time<si::hour> duration(2); // constructed from a value
|
||||
using namespace units::isq::si::time_references;
|
||||
using namespace units::isq::si::international::length_references;
|
||||
constexpr auto distance = 140. * mi;
|
||||
constexpr auto duration = 2. * h;
|
||||
|
||||
std::cout << "\nUS Customary Units with 'double' as representation\n";
|
||||
|
||||
@@ -137,9 +139,10 @@ void example()
|
||||
|
||||
// CGS (int)
|
||||
{
|
||||
using namespace units::isq::si::cgs::references;
|
||||
constexpr Length auto distance = 22'000'000 * cm; // constructed from a reference
|
||||
constexpr si::cgs::time<si::hour, int> duration(2); // constructed from a value
|
||||
using namespace units::isq::si::time_references;
|
||||
using namespace units::isq::si::cgs::length_references;
|
||||
constexpr auto distance = 22'000'000 * cm;
|
||||
constexpr auto duration = 2 * h;
|
||||
|
||||
std::cout << "\nCGS units with 'int' as representation\n";
|
||||
|
||||
@@ -156,9 +159,10 @@ void example()
|
||||
|
||||
// CGS (double)
|
||||
{
|
||||
using namespace units::isq::si::cgs::references;
|
||||
constexpr Length auto distance = 22'000'000. * cm; // constructed from a reference
|
||||
constexpr si::cgs::time<si::hour> duration(2); // constructed from a value
|
||||
using namespace units::isq::si::time_references;
|
||||
using namespace units::isq::si::cgs::length_references;
|
||||
constexpr auto distance = 22'000'000. * cm;
|
||||
constexpr auto duration = 2. * h;
|
||||
|
||||
std::cout << "\nCGS units with 'double' as representation\n";
|
||||
|
||||
|
@@ -82,10 +82,11 @@ public:
|
||||
int main()
|
||||
{
|
||||
using namespace units;
|
||||
using namespace si::length_references;
|
||||
using namespace si::time_references;
|
||||
using namespace units::isq::si;
|
||||
using namespace length_references;
|
||||
using namespace time_references;
|
||||
|
||||
const si::length<si::metre> height = 200.0 * mm;
|
||||
const auto height = quantity_cast<metre>(200.0 * mm);
|
||||
auto box = Box(1000.0 * mm, 500.0 * mm, height);
|
||||
box.set_contents_density(1000.0 * (kg / m3));
|
||||
|
||||
|
@@ -60,14 +60,14 @@ void quantities_with_typed_units()
|
||||
using namespace units::isq::si::references;
|
||||
using namespace units::isq::si::international::references;
|
||||
|
||||
constexpr si::length<si::kilometre> kilometers = 1.0 * km;
|
||||
constexpr si::length<si::international::mile> miles = 1.0 * mi;
|
||||
constexpr auto kilometers = 1.0 * km;
|
||||
constexpr auto miles = 1.0 * mi;
|
||||
|
||||
std::cout.precision(6);
|
||||
|
||||
constexpr si::time<si::second> seconds = 1 * s;
|
||||
constexpr si::time<si::minute> minutes = 1 * min;
|
||||
constexpr si::time<si::hour> hours = 1 * h;
|
||||
constexpr auto seconds = 1 * s;
|
||||
constexpr auto minutes = 1 * min;
|
||||
constexpr auto hours = 1 * h;
|
||||
|
||||
std::cout << "A more flexible option is to provide separate types for each unit,\n\n";
|
||||
std::cout << kilometers << '\n';
|
||||
@@ -76,7 +76,7 @@ void quantities_with_typed_units()
|
||||
std::cout << minutes << '\n';
|
||||
std::cout << hours << "\n\n";
|
||||
|
||||
constexpr si::length<si::metre> meter = 1 * m;
|
||||
constexpr auto meter = 1 * m;
|
||||
std::cout << "then a wide range of pre-defined units can be defined and converted,\n"
|
||||
" for consistency and repeatability across applications:\n\n";
|
||||
|
||||
@@ -111,28 +111,28 @@ void calcs_comparison()
|
||||
"when adding two values of the same very big\n"
|
||||
"or very small type:\n\n";
|
||||
|
||||
si::length<si::femtometre, float> L1A = 2. * fm;
|
||||
si::length<si::femtometre, float> L2A = 3. * fm;
|
||||
si::length<si::femtometre, float> LrA = L1A + L2A;
|
||||
const auto L1A = 2.f * fm;
|
||||
const auto L2A = 3.f * fm;
|
||||
const Length auto LrA = L1A + L2A;
|
||||
fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, LrA);
|
||||
|
||||
std::cout << "The single unit method must convert large\n"
|
||||
"or small values in other units to the base unit.\n"
|
||||
"This is both inefficient and inaccurate\n\n";
|
||||
|
||||
si::length<si::metre, float> L1B = L1A;
|
||||
si::length<si::metre, float> L2B = L2A;
|
||||
si::length<si::metre, float> LrB = L1B + L2B;
|
||||
const auto L1B = quantity_cast<si::metre>(L1A);
|
||||
const auto L2B = quantity_cast<si::metre>(L2A);
|
||||
const Length auto LrB = L1B + L2B;
|
||||
fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, LrB);
|
||||
|
||||
std::cout << "In multiplication and division:\n\n";
|
||||
|
||||
si::area<si::square_femtometre, float> ArA = L1A * L2A;
|
||||
const Area auto ArA = L1A * L2A;
|
||||
fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, ArA);
|
||||
|
||||
std::cout << "similar problems arise\n\n";
|
||||
|
||||
si::area<si::square_metre, float> ArB = L1B * L2B;
|
||||
const Area auto ArB = L1B * L2B;
|
||||
fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, ArB);
|
||||
}
|
||||
|
||||
|
@@ -47,8 +47,8 @@ int main()
|
||||
|
||||
std::cout << "conversion factor in mp-units...\n\n";
|
||||
|
||||
constexpr length<metre> lengthA = 2.0 * m;
|
||||
constexpr length<millimetre> lengthB = lengthA;
|
||||
constexpr auto lengthA = 2.0 * m;
|
||||
constexpr auto lengthB = quantity_cast<millimetre>(lengthA);
|
||||
|
||||
std::cout << fmt::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB)
|
||||
<< "represent the same length in different units.\n\n";
|
||||
|
@@ -67,16 +67,16 @@ void print_details(std::string_view description, const Ship& ship)
|
||||
using namespace units::isq::si::fps::references;
|
||||
const auto waterDensity = 62.4 * (lb / ft3);
|
||||
std::cout << fmt::format("{}\n", description);
|
||||
std::cout << fmt::format("{:20} : {}\n", "length", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.length))
|
||||
<< fmt::format("{:20} : {}\n", "draft", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.draft))
|
||||
<< fmt::format("{:20} : {}\n", "beam", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.beam))
|
||||
<< fmt::format("{:20} : {}\n", "mass", fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::tonne>>(ship.mass))
|
||||
<< fmt::format("{:20} : {}\n", "speed", fmt_line<si::fps::speed<si::fps::knot>, si::speed<si::kilometre_per_hour>>(ship.speed))
|
||||
<< fmt::format("{:20} : {}\n", "power", fmt_line<si::fps::power<si::fps::horse_power>, si::power<si::kilowatt>>(ship.power))
|
||||
<< fmt::format("{:20} : {}\n", "main guns", fmt_line<si::fps::length<si::fps::inch>, si::length<si::millimetre>>(ship.mainGuns))
|
||||
<< fmt::format("{:20} : {}\n", "fire shells weighing",fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::kilogram>>(ship.shellMass))
|
||||
<< fmt::format("{:20} : {}\n", "fire shells at",fmt_line<si::fps::speed<si::fps::mile_per_hour>, si::speed<si::kilometre_per_hour>>(ship.shellSpeed))
|
||||
<< fmt::format("{:20} : {}\n", "volume underwater", fmt_line<si::volume<si::cubic_metre>, si::volume<si::litre>>(ship.mass / waterDensity));
|
||||
std::cout << fmt::format("{:20} : {}\n", "length", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.length))
|
||||
<< fmt::format("{:20} : {}\n", "draft", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.draft))
|
||||
<< fmt::format("{:20} : {}\n", "beam", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.beam))
|
||||
<< fmt::format("{:20} : {}\n", "mass", fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::tonne>>(ship.mass))
|
||||
<< fmt::format("{:20} : {}\n", "speed", fmt_line<si::fps::speed<si::fps::knot>, si::speed<si::kilometre_per_hour>>(ship.speed))
|
||||
<< fmt::format("{:20} : {}\n", "power", fmt_line<si::fps::power<si::fps::horse_power>, si::power<si::kilowatt>>(ship.power))
|
||||
<< fmt::format("{:20} : {}\n", "main guns", fmt_line<si::fps::length<si::fps::inch>, si::length<si::millimetre>>(ship.mainGuns))
|
||||
<< fmt::format("{:20} : {}\n", "fire shells weighing", fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::kilogram>>(ship.shellMass))
|
||||
<< fmt::format("{:20} : {}\n", "fire shells at", fmt_line<si::fps::speed<si::fps::mile_per_hour>, si::speed<si::kilometre_per_hour>>(ship.shellSpeed))
|
||||
<< fmt::format("{:20} : {}\n", "volume underwater", fmt_line<si::volume<si::cubic_metre>, si::volume<si::litre>>(ship.mass / waterDensity));
|
||||
}
|
||||
|
||||
int main()
|
||||
|
Reference in New Issue
Block a user