refactor: First examples refactored to a new quantity creation syntax

This commit is contained in:
Mateusz Pusz
2022-12-22 18:06:20 +01:00
parent 0a1ee606f3
commit 858cbb472f
8 changed files with 46 additions and 46 deletions

View File

@ -61,8 +61,8 @@ void example()
{
// SI (int)
{
constexpr auto distance = 220 * isq::length[km];
constexpr auto duration = 2 * isq::time[h];
constexpr auto distance = isq::length(220, km);
constexpr auto duration = isq::time(2, h);
std::cout << "SI units with 'int' as representation\n";
@ -73,8 +73,8 @@ void example()
// SI (double)
{
constexpr auto distance = 220. * isq::length[km];
constexpr auto duration = 2. * isq::time[h];
constexpr auto distance = isq::length(220., km);
constexpr auto duration = isq::time(2., h);
std::cout << "\nSI units with 'double' as representation\n";
@ -89,8 +89,8 @@ void example()
{
using namespace units::si::international::unit_symbols;
constexpr auto distance = 140 * isq::length[mi];
constexpr auto duration = 2 * isq::time[h];
constexpr auto distance = isq::length(140, mi);
constexpr auto duration = isq::time(2, h);
std::cout << "\nUS Customary Units with 'int' as representation\n";
@ -104,8 +104,8 @@ void example()
// Customary Units (double)
{
using namespace units::si::international::unit_symbols;
constexpr auto distance = 140. * isq::length[mi];
constexpr auto duration = 2. * isq::time[h];
constexpr auto distance = isq::length(140., mi);
constexpr auto duration = isq::time(2., h);
std::cout << "\nUS Customary Units with 'double' as representation\n";
@ -121,8 +121,8 @@ void example()
// CGS (int)
{
constexpr auto distance = 22'000'000 * isq::length[si::cgs::centimetre];
constexpr auto duration = 7200 * isq::time[si::cgs::second];
constexpr auto distance = isq::length(22'000'000, si::cgs::centimetre);
constexpr auto duration = isq::time(7200, si::cgs::second);
std::cout << "\nCGS units with 'int' as representation\n";
@ -135,8 +135,8 @@ void example()
// CGS (double)
{
constexpr auto distance = 22'000'000. * isq::length[si::cgs::centimetre];
constexpr auto duration = 7200. * isq::time[si::cgs::second];
constexpr auto distance = isq::length(22'000'000., si::cgs::centimetre);
constexpr auto duration = isq::time(7200., si::cgs::second);
std::cout << "\nCGS units with 'double' as representation\n";

View File

@ -40,8 +40,8 @@ namespace {
using namespace units;
using namespace units::si::unit_symbols;
inline constexpr auto g = 1 * si::standard_gravity;
inline constexpr auto air_density = 1.225 * isq::mass_density[kg / m3];
inline constexpr auto g = si::standard_gravity(1);
inline constexpr auto air_density = isq::mass_density(1.225, kg / m3);
class Box {
quantity<isq::area[m2]> base_;
@ -87,12 +87,12 @@ int main()
constexpr auto mm = isq::length[unit_symbols::mm]; // helper reference object
const auto height = (200.0 * mm)[metre];
auto box = Box(1000.0 * mm, 500.0 * mm, height);
box.set_contents_density(1000.0 * isq::mass_density[kg / m3]);
const auto height = mm(200.0)[metre];
auto box = Box(mm(1000.0), mm(500.0), height);
box.set_contents_density(isq::mass_density(1000.0, kg / m3));
const auto fill_time = 200.0 * isq::time[s]; // time since starting fill
const auto measured_mass = 20.0 * isq::mass[kg]; // measured mass at fill_time
const auto fill_time = isq::time(200.0, s); // time since starting fill
const auto measured_mass = isq::mass(20.0, kg); // measured mass at fill_time
const auto fill_level = box.fill_level(measured_mass);
const auto spare_capacity = box.spare_capacity(measured_mass);
const auto filled_weight = box.filled_weight();

View File

@ -35,22 +35,22 @@ int main()
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout.precision(3);
constexpr auto C = 0.47 * isq::capacitance[uF];
constexpr auto V0 = 5.0 * isq::voltage[V];
constexpr auto R = 4.7 * isq::resistance[si::kilo<si::ohm>];
constexpr auto C = isq::capacitance(0.47, uF);
constexpr auto V0 = isq::voltage(5.0, V);
constexpr auto R = isq::resistance(4.7, si::kilo<si::ohm>);
for (auto t = 0 * isq::time[ms]; t <= 50 * isq::time[ms]; ++t) {
for (auto t = isq::time(0, ms); t <= isq::time(50, ms); ++t) {
const weak_quantity_of<isq::voltage> auto Vt = V0 * units::exp(-t / (R * C));
std::cout << "at " << t << " voltage is ";
if (Vt >= 1 * isq::voltage[V])
if (Vt >= isq::voltage(1, V))
std::cout << Vt[V];
else if (Vt >= 1 * isq::voltage[mV])
else if (Vt >= isq::voltage(1, mV))
std::cout << Vt[mV];
else if (Vt >= 1 * isq::voltage[uV])
else if (Vt >= isq::voltage(1, uV))
std::cout << Vt[uV];
else if (Vt >= 1 * isq::voltage[nV])
else if (Vt >= isq::voltage(1, nV))
std::cout << Vt[nV];
else
std::cout << Vt[pV];

View File

@ -43,7 +43,7 @@ int main()
std::cout << "conversion factor in mp-units...\n\n";
constexpr auto lengthA = 2.0 * isq::length[si::metre];
constexpr auto lengthA = isq::length(2.0, si::metre);
constexpr auto lengthB = lengthA[si::milli<si::metre>];
std::cout << STD_FMT::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB)

View File

@ -40,9 +40,9 @@ int main()
using namespace units::si::unit_symbols;
using namespace units::si::international::unit_symbols;
constexpr auto v1 = 110 * isq::speed[km / h];
constexpr auto v2 = 70. * isq::speed[mph];
constexpr auto v3 = avg_speed(220 * isq::distance[km], 2 * isq::duration[h]);
constexpr auto v1 = isq::speed(110, km / h);
constexpr auto v2 = isq::speed(70., mph);
constexpr auto v3 = avg_speed(isq::distance(220, km), isq::duration(2, h));
constexpr auto v4 = avg_speed(quantity<isq::distance[mi]>{140}, quantity<isq::duration[h]>{2});
constexpr auto v5 = quantity_cast<quantity<isq::speed[m / s]>>(v3);
constexpr auto v6 = quantity_cast<m / s>(v4);

View File

@ -131,13 +131,13 @@ void example()
using namespace units;
using namespace units::si::unit_symbols;
const auto a = measurement{9.8, 0.1} * isq::acceleration[m / s2];
const auto t = measurement{1.2, 0.1} * isq::time[s];
const auto a = isq::acceleration(measurement{9.8, 0.1}, m / s2);
const auto t = isq::time(measurement{1.2, 0.1}, s);
const weak_quantity_of<isq::velocity> auto v = a * t;
std::cout << a << " * " << t << " = " << v << " = " << v[km / h] << '\n';
const auto length = measurement{123., 1.} * isq::length[si::metre];
const auto length = isq::length(measurement{123., 1.}, si::metre);
std::cout << "10 * " << length << " = " << 10 * length << '\n';
}

View File

@ -36,19 +36,19 @@ int main()
std::cout << "The seven defining constants of the SI and the seven corresponding units they define:\n";
std::cout << STD_FMT::format("- hyperfine transition frequency of Cs: {} = {:%.0Q %q}\n",
1. * si2019::hyperfine_structure_transition_frequency_of_cs,
(1. * si2019::hyperfine_structure_transition_frequency_of_cs)[Hz]);
si2019::hyperfine_structure_transition_frequency_of_cs(1.),
si2019::hyperfine_structure_transition_frequency_of_cs(1.)[Hz]);
std::cout << STD_FMT::format("- speed of light in vacuum: {} = {:%.0Q %q}\n",
1. * si2019::speed_of_light_in_vacuum, (1. * si2019::speed_of_light_in_vacuum)[m / s]);
si2019::speed_of_light_in_vacuum(1.), si2019::speed_of_light_in_vacuum(1.)[m / s]);
std::cout << STD_FMT::format("- Planck constant: {} = {:%.8eQ %q}\n",
1. * si2019::planck_constant, (1. * si2019::planck_constant)[J * s]);
si2019::planck_constant(1.), si2019::planck_constant(1.)[J * s]);
std::cout << STD_FMT::format("- elementary charge: {} = {:%.9eQ %q}\n",
1. * si2019::elementary_charge, (1. * si2019::elementary_charge)[C]);
si2019::elementary_charge(1.), si2019::elementary_charge(1.)[C]);
std::cout << STD_FMT::format("- Boltzmann constant: {} = {:%.6eQ %q}\n",
1. * si2019::boltzmann_constant, (1. * si2019::boltzmann_constant)[J / K]);
si2019::boltzmann_constant(1.), si2019::boltzmann_constant(1.)[J / K]);
std::cout << STD_FMT::format("- Avogadro constant: {} = {:%.8eQ %q}\n",
1. * si2019::avogadro_constant, (1. * si2019::avogadro_constant)[1 / mol]);
si2019::avogadro_constant(1.), si2019::avogadro_constant(1.)[1 / mol]);
// TODO uncomment the below when ISQ is done
// std::cout << STD_FMT::format("- luminous efficacy: {} = {}\n", 1. * si2019::luminous_efficacy,
// (1. * si2019::luminous_efficacy)[lm / W]);
// std::cout << STD_FMT::format("- luminous efficacy: {} = {}\n", si2019::luminous_efficacy(1.),
// si2019::luminous_efficacy(1.)[lm / W]);
}

View File

@ -49,9 +49,9 @@ void si_example()
using namespace units::si::unit_symbols;
constexpr auto GeV = si::giga<si::electronvolt>;
constexpr quantity_of<isq::speed> auto c = 1. * si::si2019::speed_of_light_in_vacuum;
const weak_quantity_of<isq::momentum> auto p1 = 4. * isq::mechanical_energy[GeV] / c;
const weak_quantity_of<isq::mass> auto m1 = 3. * isq::mechanical_energy[GeV] / pow<2>(c);
constexpr quantity_of<isq::speed> auto c = si::si2019::speed_of_light_in_vacuum(1.);
const weak_quantity_of<isq::momentum> auto p1 = isq::mechanical_energy(4., GeV) / c;
const weak_quantity_of<isq::mass> auto m1 = isq::mechanical_energy(3., GeV) / pow<2>(c);
const auto E = total_energy(p1, m1, c);
std::cout << "\n*** SI units (c = " << c << " = " << c[si::metre / s] << ") ***\n";