/* 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 #include #include #include #include #include #include #include #include namespace { namespace length{ template using m = units::si::length; template using mm = units::si::length; template using fm = units::si::length; template using km = units::si::length; template using AU = units::si::length; template using in = units::si::length; template using angstrom = units::si::length; template using ch = units::si::length; template using fathom = units::si::length; template using fathom_us = units::si::length; template using ft = units::si::length; template using ft_us = units::si::length; template using ly = units::si::length; template using mi = units::si::length; template using mi_naut = units::si::length; template using pc = units::si::length; template using pica_comp = units::si::length; template using pica_prn = units::si::length; template using point_comp = units::si::length; template using point_prn = units::si::length; template using rd = units::si::length; template using yd = units::si::length; } namespace time{ template using s = units::si::time; template using min = units::si::time; template using h = units::si::time; } namespace area{ template using m2 = units::si::area; template using fm2 = units::si::area; } } #include using namespace units::si::literals; using namespace units::international; void simple_quantities() { using distance = length::m<>; using time = time::s<>; constexpr distance km = 1.0km; constexpr distance miles = 1.0mi; constexpr time sec = 1s; constexpr time min = 1min; constexpr time hr = 1h; 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() { constexpr length::km<> km = 1km; constexpr length::mi<> miles = 1.0mi; std::cout.precision(6); constexpr time::s<> sec = 1s; constexpr time::min<> min = 1min; constexpr time::h<> hr = 1h; 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 length::m<> meter = 1m; 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 << " = " << length::AU<>(meter) << '\n'; std::cout << " = " << length::angstrom<>(meter) << '\n'; std::cout << " = " << length::ch<>(meter) << '\n'; std::cout << " = " << length::fathom<>(meter) << '\n'; std::cout << " = " << length::fathom_us<>(meter) << '\n'; std::cout << " = " << length::ft<>(meter) << '\n'; std::cout << " = " << length::ft_us<>(meter) << '\n'; std::cout << " = " << length::in<>(meter) << '\n'; std::cout << " = " << length::ly<>(meter) << '\n'; std::cout << " = " << length::mi<>(meter) << '\n'; std::cout << " = " << length::mi_naut<>(meter) << '\n'; std::cout << " = " << length::pc<>(meter) << '\n'; std::cout << " = " << length::pica_comp<>(meter) << '\n'; std::cout << " = " << length::pica_prn<>(meter) << '\n'; std::cout << " = " << length::point_comp<>(meter) << '\n'; std::cout << " = " << length::point_prn<>(meter) << '\n'; std::cout << " = " << length::rd<>(meter) << '\n'; std::cout << " = " << length::yd<>(meter) << '\n'; } void calcs_comparison() { std::cout.precision(20); 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"; length::fm L1A = 2fm; length::fm L2A = 3fm; length::fm LrA = L1A + L2A; std::cout << L1A << " + " << L2A << " = " << LrA << "\n\n"; 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"; length::m L1B = L1A; length::m L2B = L2A; length::m LrB = L1B + L2B; std::cout << L1B << " + " << L2B << " = " << LrB << "\n\n"; std::cout << "In multiplication and division:\n\n"; area::fm2 ArA = L1A * L2A ; std::cout << L1A << " * " << L2A << " = " << ArA << "\n\n"; std::cout <<"similar problems arise\n\n"; area::m2 ArB = L1B * L2B; std::cout << L1B << " * " << L2B << "\n = " << ArB << '\n'; } int main() { 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 << "Here converted to use mpusz/units library.\n\n"; simple_quantities(); quantities_with_typed_units(); calcs_comparison(); }