From e98215c34788e7ee4a16859644fad4a166abf131 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Tue, 14 Feb 2023 12:58:54 +0100 Subject: [PATCH] refactor: examples refactored to benefit from the latest features --- example/box_example.cpp | 4 ++-- example/capacitor_time_curve.cpp | 2 +- example/glide_computer/glide_computer.cpp | 5 +++-- example/glide_computer/include/geographic.h | 8 ++----- .../glide_computer/include/glide_computer.h | 5 ++--- example/hello_units.cpp | 10 ++++----- example/measurement.cpp | 4 ++-- example/strong_angular_quantities.cpp | 3 +-- example/total_energy.cpp | 21 ++++++++++--------- 9 files changed, 29 insertions(+), 33 deletions(-) diff --git a/example/box_example.cpp b/example/box_example.cpp index 1a0edd6c..dbadd79c 100644 --- a/example/box_example.cpp +++ b/example/box_example.cpp @@ -55,9 +55,9 @@ public: [[nodiscard]] constexpr QuantityOf auto filled_weight() const { - const QuantityOf auto volume = isq::volume(base_ * height_); + const auto volume = isq::volume(base_ * height_); const QuantityOf auto mass = density_ * volume; - return quantity_cast(mass * g); + return isq::weight(mass * g); } [[nodiscard]] constexpr quantity fill_level(const quantity& measured_mass) const diff --git a/example/capacitor_time_curve.cpp b/example/capacitor_time_curve.cpp index b3914840..57ee3790 100644 --- a/example/capacitor_time_curve.cpp +++ b/example/capacitor_time_curve.cpp @@ -40,7 +40,7 @@ int main() constexpr auto R = isq::resistance(4.7 * si::kilo); for (auto t = 0 * ms; t <= 50 * ms; ++t) { - const QuantityOf auto Vt = V0 * mp_units::exp(-t / (R * C)); + const QuantityOf auto Vt = V0 * exp(-t / (R * C)); std::cout << "at " << t << " voltage is "; diff --git a/example/glide_computer/glide_computer.cpp b/example/glide_computer/glide_computer.cpp index cd8afb1e..250d89b7 100644 --- a/example/glide_computer/glide_computer.cpp +++ b/example/glide_computer/glide_computer.cpp @@ -61,7 +61,7 @@ altitude terrain_level_alt(const task& t, const flight_point& pos) distance glide_distance(const flight_point& pos, const glider& g, const task& t, const safety& s, altitude ground_alt) { const auto dist_to_finish = t.get_length() - pos.dist; - return distance((ground_alt + s.min_agl_height - pos.alt) / + return distance(quantity_cast(ground_alt + s.min_agl_height - pos.alt) / ((ground_alt - t.get_finish().alt) / dist_to_finish - 1 / glide_ratio(g.polar[0]))); } @@ -151,7 +151,8 @@ void estimate(timestamp start_ts, const glider& g, const weather& w, const task& pos = tow(start_ts, pos, at); // estimate the altitude needed to reach the finish line from this place - const altitude final_glide_alt = t.get_finish().alt + height(t.get_length() / glide_ratio(g.polar[0])); + const altitude final_glide_alt = + t.get_finish().alt + quantity_cast(t.get_length()) / glide_ratio(g.polar[0]); // how much height we still need to gain in the thermalls to reach the destination? height height_to_gain = final_glide_alt - pos.alt; diff --git a/example/glide_computer/include/geographic.h b/example/glide_computer/include/geographic.h index e94f2894..c31f2619 100644 --- a/example/glide_computer/include/geographic.h +++ b/example/glide_computer/include/geographic.h @@ -157,18 +157,14 @@ distance spherical_distance(position from, position to) // const auto central_angle = 2 * asin(sqrt(0.5 - cos(lat2_rad - lat1_rad) / 2 + cos(lat1_rad) * cos(lat2_rad) * (1 // - cos(lon2_rad - lon1_rad)) / 2)); - // TODO can we improve the below - // return quantity_cast(earth_radius * central_angle); - return earth_radius.number() * central_angle * isq::distance[earth_radius.unit]; + return quantity_cast(earth_radius * central_angle); } else { // the haversine formula const auto sin_lat = sin((lat2_rad - lat1_rad) / 2); const auto sin_lon = sin((lon2_rad - lon1_rad) / 2); const auto central_angle = 2 * asin(sqrt(sin_lat * sin_lat + cos(lat1_rad) * cos(lat2_rad) * sin_lon * sin_lon)); - // TODO can we improve the below - // return quantity_cast(earth_radius * central_angle); - return earth_radius.number() * central_angle * isq::distance[earth_radius.unit]; + return quantity_cast(earth_radius * central_angle); } } diff --git a/example/glide_computer/include/glide_computer.h b/example/glide_computer/include/glide_computer.h index 00ea9806..fe7cc734 100644 --- a/example/glide_computer/include/glide_computer.h +++ b/example/glide_computer/include/glide_computer.h @@ -56,7 +56,7 @@ namespace glide_computer { // https://en.wikipedia.org/wiki/Flight_planning#Units_of_measurement inline constexpr struct mean_sea_level : mp_units::absolute_point_origin { } mean_sea_level; -QUANTITY_SPEC(rate_of_climb_speed, mp_units::isq::height / mp_units::isq::time); +QUANTITY_SPEC(rate_of_climb_speed, mp_units::isq::speed, mp_units::isq::height / mp_units::isq::time); // length using distance = mp_units::quantity]>; @@ -196,8 +196,7 @@ constexpr height agl(altitude glider_alt, altitude terrain_level) { return glide inline mp_units::quantity]> length_3d(distance dist, height h) { - // TODO Should we be able to calculate this on quantity of different kinds? What to return? - return hypot(quantity_cast(dist), quantity_cast(h)); + return hypot(dist, h); } distance glide_distance(const flight_point& pos, const glider& g, const task& t, const safety& s, altitude ground_alt); diff --git a/example/hello_units.cpp b/example/hello_units.cpp index 04f0001f..ff6aaade 100644 --- a/example/hello_units.cpp +++ b/example/hello_units.cpp @@ -31,7 +31,7 @@ using namespace mp_units; constexpr QuantityOf auto avg_speed(QuantityOf auto d, QuantityOf auto t) { - return quantity_cast(d / t); + return d / t; } int main() @@ -40,10 +40,10 @@ int main() using namespace mp_units::international::unit_symbols; constexpr auto v1 = 110 * (km / h); - constexpr auto v2 = 70. * mph; - constexpr auto v3 = avg_speed(220 * km, 2 * h); - constexpr auto v4 = avg_speed(140 * mi, 2 * h); - constexpr auto v5 = value_cast(v3); + constexpr auto v2 = 70 * mph; + constexpr auto v3 = avg_speed(220. * km, 2 * h); + constexpr auto v4 = avg_speed(isq::distance(140. * mi), 2 * isq::duration[h]); + constexpr auto v5 = v3[m / s]; constexpr auto v6 = value_cast(v4); constexpr auto v7 = value_cast(v6); diff --git a/example/measurement.cpp b/example/measurement.cpp index 3d3fff4a..52f68376 100644 --- a/example/measurement.cpp +++ b/example/measurement.cpp @@ -132,12 +132,12 @@ void example() using namespace mp_units::si::unit_symbols; const auto a = isq::acceleration(measurement{9.8, 0.1} * (m / s2)); - const auto t = isq::time(measurement{1.2, 0.1} * s); + const auto t = measurement{1.2, 0.1} * s; const QuantityOf auto v = a * t; std::cout << a << " * " << t << " = " << v << " = " << v[km / h] << '\n'; - const auto length = isq::length(measurement{123., 1.} * m); + const auto length = measurement{123., 1.} * m; std::cout << "10 * " << length << " = " << 10 * length << '\n'; } diff --git a/example/strong_angular_quantities.cpp b/example/strong_angular_quantities.cpp index 4afa296c..5a45b442 100644 --- a/example/strong_angular_quantities.cpp +++ b/example/strong_angular_quantities.cpp @@ -41,8 +41,7 @@ int main() const auto lever = isq_angle::position_vector(20 * cm); const auto force = isq_angle::force(500 * N); const auto angle = isq_angle::angular_measure(90. * deg); - const auto torque = - quantity_cast(lever * force * angular::sin(angle) / (1 * isq_angle::cotes_angle)); + const auto torque = isq_angle::torque(lever * force * angular::sin(angle) / (1 * isq_angle::cotes_angle)); std::cout << "Applying a perpendicular force of " << force << " to a " << lever << " long lever results in " << torque[N * m / rad] << " of torque.\n"; diff --git a/example/total_energy.cpp b/example/total_energy.cpp index 822360c9..4858ad58 100644 --- a/example/total_energy.cpp +++ b/example/total_energy.cpp @@ -40,17 +40,18 @@ using namespace mp_units; QuantityOf auto total_energy(QuantityOf auto p, QuantityOf auto m, QuantityOf auto c) { - return quantity_cast(sqrt(pow<2>(p * c) + pow<2>(m * pow<2>(c)))); + return isq::mechanical_energy(sqrt(pow<2>(p * c) + pow<2>(m * pow<2>(c)))); } void si_example() { using namespace mp_units::si::unit_symbols; constexpr auto GeV = si::giga; - constexpr QuantityOf auto c = 1. * si::si2019::speed_of_light_in_vacuum; - const QuantityOf auto p1 = isq::mechanical_energy(4. * GeV) / c; - const QuantityOf auto m1 = isq::mechanical_energy(3. * GeV) / pow<2>(c); + auto c2 = pow<2>(c); + + const auto p1 = isq::momentum(4. * GeV / c); + const QuantityOf auto m1 = 3. * GeV / c2; const auto E = total_energy(p1, m1, c); std::cout << "\n*** SI units (c = " << c << " = " << c[si::metre / s] << ") ***\n"; @@ -60,8 +61,8 @@ void si_example() << "m = " << m1 << "\n" << "E = " << E << "\n"; - const auto p2 = p1[GeV / (si::metre / s)]; - const auto m2 = m1[si::giga / pow<2>(si::metre / s)]; + const auto p2 = p1[GeV / (m / s)]; + const auto m2 = m1[GeV / pow<2>(m / s)]; const auto E2 = total_energy(p2, m2, c)[GeV]; std::cout << "\n[in `GeV`]\n" @@ -69,7 +70,7 @@ void si_example() << "m = " << m2 << "\n" << "E = " << E2 << "\n"; - const auto p3 = p1[kg * si::metre / s]; + const auto p3 = p1[kg * m / s]; const auto m3 = m1[kg]; const auto E3 = total_energy(p3, m3, c)[J]; @@ -87,9 +88,9 @@ void natural_example() using namespace mp_units::natural; using namespace mp_units::natural::unit_symbols; - constexpr QuantityOf auto c = 1. * speed_of_light_in_vacuum; - const QuantityOf auto p = 4. * momentum[GeV]; - const QuantityOf auto m = 3. * mass[GeV]; + constexpr auto c = 1. * speed_of_light_in_vacuum; + const auto p = 4. * momentum[GeV]; + const auto m = 3. * mass[GeV]; const auto E = total_energy(p, m, c); std::cout << "\n*** Natural units (c = " << c << ") ***\n"