forked from mpusz/mp-units
refactor: the rest of the systems refactored for V2
This commit is contained in:
@@ -20,11 +20,11 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include <units/cgs/cgs.h>
|
||||
#include <units/isq/space_and_time.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <units/si/cgs/cgs.h>
|
||||
#include <units/si/international/length.h>
|
||||
#include <units/si/unit_symbols.h>
|
||||
#include <units/international/international.h>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
|
||||
@@ -87,7 +87,7 @@ void example()
|
||||
|
||||
// Customary Units (int)
|
||||
{
|
||||
using namespace units::si::international::unit_symbols;
|
||||
using namespace units::international::unit_symbols;
|
||||
|
||||
constexpr auto distance = isq::length(140, mi);
|
||||
constexpr auto duration = isq::time(2, h);
|
||||
@@ -103,7 +103,8 @@ void example()
|
||||
|
||||
// Customary Units (double)
|
||||
{
|
||||
using namespace units::si::international::unit_symbols;
|
||||
using namespace units::international::unit_symbols;
|
||||
|
||||
constexpr auto distance = isq::length(140., mi);
|
||||
constexpr auto duration = isq::time(2., h);
|
||||
|
||||
@@ -121,8 +122,8 @@ void example()
|
||||
|
||||
// CGS (int)
|
||||
{
|
||||
constexpr auto distance = isq::length(22'000'000, si::cgs::centimetre);
|
||||
constexpr auto duration = isq::time(7200, si::cgs::second);
|
||||
constexpr auto distance = isq::length(22'000'000, cgs::centimetre);
|
||||
constexpr auto duration = isq::time(7200, cgs::second);
|
||||
|
||||
std::cout << "\nCGS units with 'int' as representation\n";
|
||||
|
||||
@@ -135,8 +136,8 @@ void example()
|
||||
|
||||
// CGS (double)
|
||||
{
|
||||
constexpr auto distance = isq::length(22'000'000., si::cgs::centimetre);
|
||||
constexpr auto duration = isq::time(7200., si::cgs::second);
|
||||
constexpr auto distance = isq::length(22'000'000., cgs::centimetre);
|
||||
constexpr auto duration = isq::time(7200., cgs::second);
|
||||
|
||||
std::cout << "\nCGS units with 'double' as representation\n";
|
||||
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
Copyright (c) 2003-2019 Andy Little.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see http://www.gnu.org/licenses./
|
||||
*/
|
||||
|
||||
#include <units/format.h>
|
||||
#include <units/iau/iau.h>
|
||||
#include <units/imperial/imperial.h>
|
||||
#include <units/international/international.h>
|
||||
#include <units/isq/space_and_time.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <units/si/si.h>
|
||||
#include <units/typographic/typographic.h>
|
||||
#include <units/usc/usc.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
||||
void simple_quantities()
|
||||
{
|
||||
using namespace units;
|
||||
using namespace units::si;
|
||||
using namespace units::international;
|
||||
|
||||
using distance = quantity<isq::distance[kilo<metre>]>;
|
||||
using duration = quantity<isq::duration[second]>;
|
||||
|
||||
constexpr distance km = isq::distance[kilo<metre>](1.0);
|
||||
constexpr distance miles = isq::distance[mile](1.0);
|
||||
|
||||
constexpr duration sec = isq::duration[second](1);
|
||||
constexpr duration min = isq::duration[minute](1);
|
||||
constexpr duration hr = isq::duration[hour](1);
|
||||
|
||||
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 << km << '\n';
|
||||
std::cout << miles << '\n';
|
||||
std::cout << sec << '\n';
|
||||
std::cout << min << '\n';
|
||||
std::cout << hr << "\n\n";
|
||||
}
|
||||
|
||||
void quantities_with_typed_units()
|
||||
{
|
||||
using namespace units;
|
||||
using namespace units::si;
|
||||
using namespace units::international;
|
||||
|
||||
constexpr auto km = isq::distance[kilo<metre>](1.0);
|
||||
constexpr auto miles = isq::distance[mile](1.0);
|
||||
|
||||
std::cout.precision(6);
|
||||
|
||||
constexpr auto sec = isq::duration[second](1);
|
||||
constexpr auto min = isq::duration[minute](1);
|
||||
constexpr auto hr = isq::duration[hour](1);
|
||||
|
||||
std::cout << "A more flexible option is to provide separate types for each unit,\n\n";
|
||||
std::cout << km << '\n';
|
||||
std::cout << miles << '\n';
|
||||
std::cout << sec << '\n';
|
||||
std::cout << min << '\n';
|
||||
std::cout << hr << "\n\n";
|
||||
|
||||
constexpr quantity<isq::length[metre]> meter{1};
|
||||
std::cout << "then a wide range of pre-defined units can be defined and converted,\n"
|
||||
" for consistency and repeatability across applications:\n\n";
|
||||
|
||||
std::cout << meter << '\n';
|
||||
|
||||
std::cout << " = " << meter[astronomical_unit] << '\n';
|
||||
std::cout << " = " << meter[iau::angstrom] << '\n';
|
||||
std::cout << " = " << meter[imperial::chain] << '\n';
|
||||
std::cout << " = " << meter[imperial::fathom] << '\n';
|
||||
std::cout << " = " << meter[usc::fathom] << '\n';
|
||||
std::cout << " = " << meter[international::foot] << '\n';
|
||||
std::cout << " = " << meter[usc::survey1893::us_survey_foot] << '\n';
|
||||
std::cout << " = " << meter[international::inch] << '\n';
|
||||
std::cout << " = " << meter[iau::light_year] << '\n';
|
||||
std::cout << " = " << meter[international::mile] << '\n';
|
||||
std::cout << " = " << meter[international::nautical_mile] << '\n';
|
||||
std::cout << " = " << meter[iau::parsec] << '\n';
|
||||
std::cout << " = " << meter[typographic::pica_dtp] << '\n';
|
||||
std::cout << " = " << meter[typographic::pica_us] << '\n';
|
||||
std::cout << " = " << meter[typographic::point_dtp] << '\n';
|
||||
std::cout << " = " << meter[typographic::point_us] << '\n';
|
||||
std::cout << " = " << meter[imperial::rod] << '\n';
|
||||
std::cout << " = " << meter[international::yard] << '\n';
|
||||
}
|
||||
|
||||
void calcs_comparison()
|
||||
{
|
||||
using namespace units;
|
||||
using namespace units::si::unit_symbols;
|
||||
|
||||
std::cout << "\nA distinct unit for each type is efficient and accurate\n"
|
||||
"when adding two values of the same very big\n"
|
||||
"or very small type:\n\n";
|
||||
|
||||
const auto L1A = isq::length[fm](2.f);
|
||||
const auto L2A = isq::length[fm](3.f);
|
||||
const auto LrA = L1A + L2A;
|
||||
std::cout << STD_FMT::format("{:%.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";
|
||||
|
||||
const auto L1B = L1A[m];
|
||||
const auto L2B = L2A[m];
|
||||
const auto LrB = L1B + L2B;
|
||||
std::cout << STD_FMT::format("{:%.30eQ %q}\n + {:%.30eQ %q}\n = {:%.30eQ %q}\n\n", L1B, L2B, LrB);
|
||||
|
||||
std::cout << "In multiplication and division:\n\n";
|
||||
|
||||
const quantity<isq::area[square<fm>], float> ArA = L1A * L2A;
|
||||
std::cout << STD_FMT::format("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, ArA);
|
||||
|
||||
std::cout << "similar problems arise\n\n";
|
||||
|
||||
const quantity<isq::area[m2], float> ArB = L1B * L2B;
|
||||
std::cout << STD_FMT::format("{:%.30eQ %q}\n * {:%.30eQ %q}\n = {:%.30eQ %q}\n\n", L1B, L2B, ArB);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "This demo was originally posted on com.lang.c++.moderated in 2006\n";
|
||||
std::cout << "https://groups.google.com/g/comp.lang.c++.moderated/c/upv7hZExtf4/m/XruKUk8LhXYJ\n";
|
||||
std::cout << "Here converted to use mp-units library.\n\n";
|
||||
|
||||
simple_quantities();
|
||||
quantities_with_typed_units();
|
||||
calcs_comparison();
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2018 Mateusz Pusz
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include <units/format.h>
|
||||
#include <units/imperial/imperial.h>
|
||||
#include <units/international/international.h>
|
||||
#include <units/isq/space_and_time.h>
|
||||
#include <units/si/unit_symbols.h>
|
||||
#include <iostream>
|
||||
#include <string_view>
|
||||
|
||||
using namespace units;
|
||||
using namespace units::international::unit_symbols;
|
||||
using namespace units::si::unit_symbols;
|
||||
|
||||
|
||||
// Some basic specs for the warship
|
||||
struct Ship {
|
||||
quantity<isq::length[ft]> length;
|
||||
quantity<isq::length[ft]> draft;
|
||||
quantity<isq::length[ft]> beam;
|
||||
|
||||
quantity<isq::speed[ft / s]> speed;
|
||||
quantity<isq::mass[lb]> mass;
|
||||
|
||||
quantity<isq::length[in]> mainGuns;
|
||||
quantity<isq::mass[lb]> shellMass;
|
||||
quantity<isq::speed[ft / s]> shellSpeed;
|
||||
quantity<isq::power[ft * pdl / s]> power;
|
||||
};
|
||||
|
||||
// Print 'q' in its current units and print its value cast to the units in each of Us
|
||||
template<Unit auto... Us, Quantity Q>
|
||||
auto fmt_line(const Q& q)
|
||||
{
|
||||
return STD_FMT::format("{:22}", q) + (STD_FMT::format(",{:20}", quantity_cast<Us>(q)) + ...);
|
||||
}
|
||||
|
||||
// Print the ship details in the units as defined in the Ship struct, in other si::imperial units, and in SI
|
||||
void print_details(std::string_view description, const Ship& ship)
|
||||
{
|
||||
const auto waterDensity = isq::density[lb / cubic<ft>](62.4);
|
||||
std::cout << STD_FMT::format("{}\n", description);
|
||||
std::cout << STD_FMT::format("{:20} : {}\n", "length", fmt_line<yd, m>(ship.length))
|
||||
<< STD_FMT::format("{:20} : {}\n", "draft", fmt_line<yd, m>(ship.draft))
|
||||
<< STD_FMT::format("{:20} : {}\n", "beam", fmt_line<yd, m>(ship.beam))
|
||||
<< STD_FMT::format("{:20} : {}\n", "mass", fmt_line<imperial::long_ton, t>(ship.mass))
|
||||
<< STD_FMT::format("{:20} : {}\n", "speed", fmt_line<kt, km / h>(ship.speed))
|
||||
<< STD_FMT::format("{:20} : {}\n", "power", fmt_line<hp, kW>(ship.power))
|
||||
<< STD_FMT::format("{:20} : {}\n", "main guns", fmt_line<in, mm>(ship.mainGuns))
|
||||
<< STD_FMT::format("{:20} : {}\n", "fire shells weighing", fmt_line<imperial::long_ton, kg>(ship.shellMass))
|
||||
<< STD_FMT::format("{:20} : {}\n", "fire shells at", fmt_line<mph, km / h>(ship.shellSpeed))
|
||||
<< STD_FMT::format("{:20} : {}\n", "volume underwater", fmt_line<m3, l>(ship.mass / waterDensity));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
using units::international::unit_symbols::ft; // collides with si::femto<si::tonne>
|
||||
|
||||
// KMS Bismark, using the units the Germans would use, taken from Wiki
|
||||
auto bismark = Ship{.length{isq::length[m](251.)},
|
||||
.draft{isq::length[m](9.3)},
|
||||
.beam{isq::length[m](36)},
|
||||
.speed{isq::speed[km / h](56)},
|
||||
.mass{isq::mass[t](50'300)},
|
||||
.mainGuns{isq::length[mm](380)},
|
||||
.shellMass{isq::mass[kg](800)},
|
||||
.shellSpeed{isq::speed[m / s](820.)},
|
||||
.power{isq::power[kW](110.45)}};
|
||||
|
||||
// USS Iowa, using units from the foot-pound-second system
|
||||
auto iowa = Ship{.length{isq::length[ft](860.)},
|
||||
.draft{isq::length[ft](37.) + isq::length[in](2.)},
|
||||
.beam{isq::length[ft](108.) + isq::length[in](2.)},
|
||||
.speed{isq::speed[kt](33)},
|
||||
.mass{isq::mass[imperial::long_ton](57'540)},
|
||||
.mainGuns{isq::length[in](16)},
|
||||
.shellMass{isq::mass[lb](2700)},
|
||||
.shellSpeed{isq::speed[ft / s](2690.)},
|
||||
.power{isq::power[hp](212'000)}};
|
||||
|
||||
// HMS King George V, using units from the foot-pound-second system
|
||||
auto kgv = Ship{.length{isq::length[ft](745.1)},
|
||||
.draft{isq::length[ft](33.) + isq::length[in](7.5)},
|
||||
.beam{isq::length[ft](103.2) + isq::length[in](2.5)},
|
||||
.speed{isq::speed[kt](28.3)},
|
||||
.mass{isq::mass[imperial::long_ton](42'245)},
|
||||
.mainGuns{isq::length[in](14)},
|
||||
.shellMass{isq::mass[lb](1'590)},
|
||||
.shellSpeed{isq::speed[ft / s](2483.)},
|
||||
.power{isq::power[hp](110'000)}};
|
||||
|
||||
print_details("KMS Bismark, defined in appropriate units from the SI system", bismark);
|
||||
std::cout << "\n\n";
|
||||
print_details("USS Iowa, defined in appropriate units foot-pound-second system", iowa);
|
||||
std::cout << "\n\n";
|
||||
print_details("HMS King George V, defined in appropriate units foot-pound-second system", kgv);
|
||||
}
|
||||
@@ -23,8 +23,8 @@
|
||||
#include "glide_computer.h"
|
||||
#include <units/bits/fmt_hacks.h>
|
||||
#include <units/chrono.h>
|
||||
#include <units/international/international.h>
|
||||
#include <units/math.h>
|
||||
#include <units/si/international/length.h>
|
||||
#include <units/si/unit_symbols.h>
|
||||
#include <array>
|
||||
#include <exception>
|
||||
@@ -67,7 +67,7 @@ auto get_weather_conditions()
|
||||
auto get_waypoints()
|
||||
{
|
||||
using namespace geographic::literals;
|
||||
using namespace units::si::international::unit_symbols;
|
||||
using namespace units::international::unit_symbols;
|
||||
static const std::array waypoints = {
|
||||
waypoint{"EPPR", {54.24772_N, 18.6745_E}, altitude{16. * isq::altitude[ft]}}, // N54°14'51.8" E18°40'28.2"
|
||||
waypoint{"EPGI", {53.52442_N, 18.84947_E}, altitude{115. * isq::altitude[ft]}} // N53°31'27.9" E18°50'58.1"
|
||||
|
||||
@@ -21,10 +21,9 @@
|
||||
// SOFTWARE.
|
||||
|
||||
#include <units/format.h>
|
||||
#include <units/international/international.h>
|
||||
#include <units/isq/space_and_time.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <units/si/international/length.h>
|
||||
#include <units/si/international/speed.h>
|
||||
#include <units/si/unit_symbols.h>
|
||||
#include <iostream>
|
||||
|
||||
@@ -38,7 +37,7 @@ constexpr quantity_of<isq::speed> auto avg_speed(quantity_of<isq::distance> auto
|
||||
int main()
|
||||
{
|
||||
using namespace units::si::unit_symbols;
|
||||
using namespace units::si::international::unit_symbols;
|
||||
using namespace units::international::unit_symbols;
|
||||
|
||||
constexpr auto v1 = isq::speed(110, km / h);
|
||||
constexpr auto v2 = isq::speed(70., mph);
|
||||
|
||||
Reference in New Issue
Block a user