examples, alternative namespaces : Part2 of put alternative namespace definitions in local headers.

examples, clcpp_response,alternative_namespaces::clcpp_response : use fmt functions for showing precision of quantities
examples, alternative namespaces : combined the alternative namespace quantity typedefs in header files.
examples, alternative namespaces : add simple timer example
This commit is contained in:
Andy Little
2020-03-21 18:23:05 +01:00
committed by Mateusz Pusz
parent c886590b03
commit 6b7e5893d2
13 changed files with 283 additions and 182 deletions
+21 -123
View File
@@ -15,127 +15,30 @@
along with this program. If not, see http://www.gnu.org/licenses./
*/
#include <units/physical/iau/length.h>
#include <units/physical/imperial/length.h>
#include <units/physical/international/length.h>
#include <units/physical/si/area.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>
#include <units/format.h>
namespace {
namespace length {
template<typename Rep = double>
using m = units::si::length<units::si::metre, Rep>;
template<typename Rep = double>
using mm = units::si::length<units::si::millimetre, Rep>;
template<typename Rep = double>
using fm = units::si::length<units::si::femtometre, Rep>;
template<typename Rep = double>
using km = units::si::length<units::si::kilometre, Rep>;
template<typename Rep = double>
using AU = units::si::length<units::si::astronomical_unit, Rep>;
template<typename Rep = double>
using in = units::si::length<units::international::inch, Rep>;
template<typename Rep = double>
using angstrom = units::si::length<units::iau::angstrom, Rep>;
template<typename Rep = double>
using ch = units::si::length<units::imperial::chain, Rep>;
template<typename Rep = double>
using fathom = units::si::length<units::international::fathom, Rep>;
template<typename Rep = double>
using fathom_us = units::si::length<units::us::fathom, Rep>;
template<typename Rep = double>
using ft = units::si::length<units::international::foot, Rep>;
template<typename Rep = double>
using ft_us = units::si::length<units::us::foot, Rep>;
template<typename Rep = double>
using ly = units::si::length<units::iau::light_year, Rep>;
template<typename Rep = double>
using mi = units::si::length<units::international::mile, Rep>;
template<typename Rep = double>
using mi_naut = units::si::length<units::international::nautical_mile, Rep>;
template<typename Rep = double>
using pc = units::si::length<units::iau::parsec, Rep>;
template<typename Rep = double>
using pica_comp = units::si::length<units::typographic::pica_comp, Rep>;
template<typename Rep = double>
using pica_prn = units::si::length<units::typographic::pica_prn, Rep>;
template<typename Rep = double>
using point_comp = units::si::length<units::typographic::point_comp, Rep>;
template<typename Rep = double>
using point_prn = units::si::length<units::typographic::point_prn, Rep>;
template<typename Rep = double>
using rd = units::si::length<units::imperial::rod, Rep>;
template<typename Rep = double>
using yd = units::si::length<units::international::yard, Rep>;
} // namespace length
namespace time {
template<typename Rep = double>
using s = units::si::time<units::si::second, Rep>;
template<typename Rep = double>
using min = units::si::time<units::si::minute, Rep>;
template<typename Rep = double>
using h = units::si::time<units::si::hour, Rep>;
} // namespace time
namespace area {
template<typename Rep = double>
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 "./length.h"
#include "./volume.h"
#include "./time.h"
#include "./area.h"
#include "./units_str.h"
using namespace units::si::literals;
using namespace units::international;
using namespace units::experimental;
void simple_quantities()
{
using distance = length::m<>;
using time = time::s<>;
using q_time = q_time::s<>;
constexpr distance km = 1.0q_km;
constexpr distance miles = 1.0q_mi;
constexpr time sec = 1q_s;
constexpr time min = 1q_min;
constexpr time hr = 1q_h;
constexpr q_time sec = 1q_s;
constexpr q_time min = 1q_min;
constexpr q_time hr = 1q_h;
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";
@@ -151,11 +54,9 @@ void quantities_with_typed_units()
constexpr length::km<> km = 1.0q_km;
constexpr length::mi<> miles = 1.0q_mi;
std::cout.precision(6);
constexpr time::s<> sec = 1q_s;
constexpr time::min<> min = 1q_min;
constexpr time::h<> hr = 1q_h;
constexpr q_time::s<> sec = 1q_s;
constexpr q_time::min<> min = 1q_min;
constexpr q_time::h<> hr = 1q_h;
std::cout << "A more flexible option is to provide separate types for each unit,\n\n";
std::cout << km << '\n';
@@ -192,16 +93,14 @@ void quantities_with_typed_units()
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<float> L1A = 2q_fm;
length::fm<float> L2A = 3q_fm;
length::fm<float> L1A = 2.q_fm;
length::fm<float> L2A = 3.q_fm;
length::fm<float> LrA = L1A + L2A;
std::cout << L1A << " + " << L2A << " = " << LrA << "\n\n";
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"
@@ -210,23 +109,22 @@ void calcs_comparison()
length::m<float> L1B = L1A;
length::m<float> L2B = L2A;
length::m<float> LrB = L1B + L2B;
std::cout << L1B << " + " << L2B << " = " << LrB << "\n\n";
fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n",L1B,L2B,LrB);
std::cout << "In multiplication and division:\n\n";
area::fm2<float> ArA = L1A * L2A;
std::cout << L1A << " * " << L2A << " = " << ArA << "\n\n";
fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n",L1A,L2A,ArA);
std::cout << "similar problems arise\n\n";
area::m2<float> ArB = L1B * L2B;
std::cout << L1B << " * " << L2B << "\n = " << ArB << '\n';
fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n",L1B,L2B,ArB);
}
int main()
{
std::cout << "This demo was originally posted on com.lang.c++.moderated in 2006\n";
std::cout << "This demo was originally posted on comp.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";