mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-04 12:54:25 +02:00
CGS case added to avg_speed example
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
// SOFTWARE.
|
||||
|
||||
#include <units/physical/si/velocity.h>
|
||||
#include <units/physical/cgs/velocity.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
@@ -54,7 +55,7 @@ constexpr units::Velocity AUTO avg_speed(units::Length AUTO d, units::Time AUTO
|
||||
template<units::Length D, units::Time T, units::Velocity V>
|
||||
void print_result(D distance, T duration, V velocity)
|
||||
{
|
||||
const auto result_in_kmph = units::quantity_cast<units::si::kilometre_per_hour>(velocity);
|
||||
const auto result_in_kmph = units::quantity_cast<units::si::velocity<units::si::kilometre_per_hour>>(velocity);
|
||||
std::cout << "Average speed of a car that makes " << distance << " in "
|
||||
<< duration << " is " << result_in_kmph << ".\n";
|
||||
}
|
||||
@@ -62,10 +63,10 @@ void print_result(D distance, T duration, V velocity)
|
||||
void example()
|
||||
{
|
||||
using namespace units;
|
||||
using namespace units::si::literals;
|
||||
|
||||
// SI (int)
|
||||
{
|
||||
using namespace units::si::literals;
|
||||
constexpr Length AUTO distance = 220km; // constructed from a UDL
|
||||
constexpr si::time<si::hour, int> duration(2); // constructed from a value
|
||||
|
||||
@@ -73,15 +74,13 @@ void example()
|
||||
|
||||
print_result(distance, duration, fixed_int_si_avg_speed(distance, duration));
|
||||
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
|
||||
|
||||
// the framework will not allow a division (and multiplication) of different dimensions
|
||||
// with two integral representation (at least one of them have to ba floating-point one)
|
||||
print_result(distance, duration, si_avg_speed(quantity_cast<double>(distance), duration));
|
||||
print_result(distance, duration, avg_speed(quantity_cast<double>(distance), duration));
|
||||
print_result(distance, duration, si_avg_speed(distance, duration));
|
||||
print_result(distance, duration, avg_speed(distance, duration));
|
||||
}
|
||||
|
||||
// SI (double)
|
||||
{
|
||||
using namespace units::si::literals;
|
||||
constexpr Length AUTO distance = 220.km; // constructed from a UDL
|
||||
constexpr si::time<si::hour> duration(2); // constructed from a value
|
||||
|
||||
@@ -97,6 +96,7 @@ void example()
|
||||
|
||||
// Customary Units (int)
|
||||
{
|
||||
using namespace units::si::literals;
|
||||
constexpr Length AUTO distance = 140mi; // constructed from a UDL
|
||||
constexpr si::time<si::hour, int> duration(2); // constructed from a value
|
||||
|
||||
@@ -106,15 +106,13 @@ void example()
|
||||
// (explicit cast needed)
|
||||
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<si::metre>(distance), duration));
|
||||
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
|
||||
|
||||
// the framework will not allow a division (and multiplication) of different dimensions
|
||||
// with two integral representation (at least one of them have to ba floating-point one)
|
||||
print_result(distance, duration, si_avg_speed(quantity_cast<double>(distance), duration));
|
||||
print_result(distance, duration, avg_speed(quantity_cast<double>(distance), duration));
|
||||
print_result(distance, duration, si_avg_speed(distance, duration));
|
||||
print_result(distance, duration, avg_speed(distance, duration));
|
||||
}
|
||||
|
||||
// Customary Units (double)
|
||||
{
|
||||
using namespace units::si::literals;
|
||||
constexpr Length AUTO distance = 140.mi; // constructed from a UDL
|
||||
constexpr si::time<si::hour> duration(2); // constructed from a value
|
||||
|
||||
@@ -129,6 +127,47 @@ void example()
|
||||
print_result(distance, duration, si_avg_speed(distance, duration));
|
||||
print_result(distance, duration, avg_speed(distance, duration));
|
||||
}
|
||||
|
||||
// CGS (int)
|
||||
{
|
||||
using namespace units::cgs::literals;
|
||||
constexpr Length AUTO distance = 22'000'000cm; // constructed from a UDL
|
||||
constexpr cgs::time<si::hour, int> duration(2); // constructed from a value
|
||||
|
||||
std::cout << "\nCGS units with 'int' as representation\n";
|
||||
|
||||
// it is not possible to make a lossless conversion of centimeters to meters on an integral type
|
||||
// (explicit cast needed)
|
||||
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<si::metre>(distance), duration));
|
||||
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
|
||||
|
||||
// not possible to convert both a dimension and a unit with implicit cast
|
||||
print_result(distance, duration, si_avg_speed(quantity_cast<si::dim_length>(distance), duration));
|
||||
|
||||
print_result(distance, duration, avg_speed(distance, duration));
|
||||
}
|
||||
|
||||
// CGS (double)
|
||||
{
|
||||
using namespace units::cgs::literals;
|
||||
constexpr Length AUTO distance = 22'000'000.cm; // constructed from a UDL
|
||||
constexpr cgs::time<si::hour> duration(2); // constructed from a value
|
||||
|
||||
std::cout << "\nCGS units with 'double' as representation\n";
|
||||
|
||||
// conversion from a floating-point to an integral type is a truncating one so an explicit cast is needed
|
||||
// it is not possible to make a lossless conversion of centimeters to meters on an integral type
|
||||
// (explicit cast needed)
|
||||
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<si::length<si::metre, int>>(distance), quantity_cast<int>(duration)));
|
||||
|
||||
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
|
||||
|
||||
// not possible to convert both a dimension and a unit with implicit cast
|
||||
print_result(distance, duration, si_avg_speed(quantity_cast<si::dim_length>(distance), duration));
|
||||
|
||||
print_result(distance, duration, avg_speed(distance, duration));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
@@ -1,39 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/unit.h>
|
||||
|
||||
namespace units::detail {
|
||||
|
||||
template<Dimension Dim1, Unit U1, Dimension Dim2, Unit U2,
|
||||
typename Ratio1 = ratio_divide<typename U1::ratio, typename dimension_unit<Dim1>::ratio>,
|
||||
typename Ratio2 = ratio_divide<typename U2::ratio, typename dimension_unit<Dim2>::ratio>>
|
||||
using unit_ratio_multiply = ratio_multiply<Ratio1, Ratio2>;
|
||||
|
||||
template<Dimension Dim1, Unit U1, Dimension Dim2, Unit U2,
|
||||
typename Ratio1 = ratio_divide<typename U1::ratio, typename dimension_unit<Dim1>::ratio>,
|
||||
typename Ratio2 = ratio_divide<typename U2::ratio, typename dimension_unit<Dim2>::ratio>>
|
||||
using unit_ratio_divide = ratio_divide<Ratio1, Ratio2>;
|
||||
|
||||
} // namespace units::detail
|
@@ -24,7 +24,6 @@
|
||||
|
||||
#include <units/bits/concepts.h>
|
||||
#include <units/bits/dimension_op.h>
|
||||
#include <units/bits/unit_op.h>
|
||||
#include <units/bits/unit_text.h>
|
||||
|
||||
#if __GNUC__ >= 10
|
||||
@@ -182,8 +181,7 @@ struct cast_ratio<FromD, FromU, ToD, ToU> {
|
||||
*
|
||||
* This cast gets the target quantity type to cast to. For example:
|
||||
*
|
||||
* using seconds = units::time<units::si::second, double>;
|
||||
* auto q1 = units::quantity_cast<seconds>(1ms);
|
||||
* auto q1 = units::quantity_cast<units::si::time<units::si::second>>(1ms);
|
||||
*
|
||||
* @tparam To a target quantity type to cast to
|
||||
*/
|
||||
@@ -194,8 +192,8 @@ template<Quantity To, typename D, typename U, typename Rep>
|
||||
{
|
||||
using c_ratio = detail::cast_ratio<D, U, typename To::dimension, typename To::unit>::type;
|
||||
using c_rep = std::common_type_t<typename To::rep, Rep, intmax_t>;
|
||||
using ret_unit = downcast_unit<D, typename To::unit::ratio>;
|
||||
using ret = quantity<D, ret_unit, typename To::rep>;
|
||||
using ret_unit = downcast_unit<typename To::dimension, typename To::unit::ratio>;
|
||||
using ret = quantity<typename To::dimension, ret_unit, typename To::rep>;
|
||||
using cast = detail::quantity_cast_impl<ret, c_ratio, c_rep, c_ratio::num == 1, c_ratio::den == 1>;
|
||||
return cast::cast(q);
|
||||
}
|
||||
@@ -595,7 +593,7 @@ template<Scalar Value, typename D, typename U, typename Rep>
|
||||
|
||||
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Scalar AUTO operator*(const quantity<D1, U1, Rep1>& lhs, const quantity<D2, U2, Rep2>& rhs)
|
||||
requires equivalent_dim<D1, dim_invert<D2>> && detail::basic_arithmetic<Rep1, Rep2>
|
||||
requires detail::basic_arithmetic<Rep1, Rep2> && equivalent_dim<D1, dim_invert<D2>>
|
||||
{
|
||||
using common_rep = decltype(lhs.count() * rhs.count());
|
||||
using ratio = ratio_multiply<typename U1::ratio, typename U2::ratio>;
|
||||
@@ -604,13 +602,12 @@ template<typename D1, typename U1, typename Rep1, typename D2, typename U2, type
|
||||
|
||||
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity AUTO operator*(const quantity<D1, U1, Rep1>& lhs, const quantity<D2, U2, Rep2>& rhs)
|
||||
requires (!equivalent_dim<D1, dim_invert<D2>>) && // TODO equivalent_derived_dim?
|
||||
(treat_as_floating_point<decltype(lhs.count() * rhs.count())> ||
|
||||
detail::unit_ratio_multiply<D1, U1, D2, U2>::den == 1) &&
|
||||
detail::basic_arithmetic<Rep1, Rep2>
|
||||
requires detail::basic_arithmetic<Rep1, Rep2> && (!equivalent_dim<D1, dim_invert<D2>>) // TODO equivalent_derived_dim?
|
||||
{
|
||||
using dim = dimension_multiply<D1, D2>;
|
||||
using ratio = ratio_multiply<detail::unit_ratio_multiply<D1, U1, D2, U2>, typename dimension_unit<dim>::ratio>; ;
|
||||
using ratio1 = ratio_divide<typename U1::ratio, typename dimension_unit<D1>::ratio>;
|
||||
using ratio2 = ratio_divide<typename U2::ratio, typename dimension_unit<D2>::ratio>;
|
||||
using ratio = ratio_multiply<ratio_multiply<ratio1, ratio2>, typename dimension_unit<dim>::ratio>;
|
||||
using unit = downcast_unit<dim, ratio>;
|
||||
using common_rep = decltype(lhs.count() * rhs.count());
|
||||
using ret = quantity<dim, unit, common_rep>;
|
||||
@@ -655,15 +652,15 @@ template<typename D1, typename U1, typename Rep1, typename D2, typename U2, type
|
||||
|
||||
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity AUTO operator/(const quantity<D1, U1, Rep1>& lhs, const quantity<D2, U2, Rep2>& rhs)
|
||||
requires detail::basic_arithmetic<Rep1, Rep2> && (!equivalent_dim<D1, D2>) && // TODO equivalent_derived_dim?
|
||||
(treat_as_floating_point<decltype(lhs.count() / rhs.count())> ||
|
||||
detail::unit_ratio_divide<D1, U1, D2, U2>::den == 1)
|
||||
requires detail::basic_arithmetic<Rep1, Rep2> && (!equivalent_dim<D1, D2>) // TODO equivalent_derived_dim?
|
||||
{
|
||||
Expects(rhs.count() != 0);
|
||||
|
||||
using common_rep = decltype(lhs.count() / rhs.count());
|
||||
using dim = dimension_divide<D1, D2>;
|
||||
using ratio = ratio_multiply<detail::unit_ratio_divide<D1, U1, D2, U2>, typename dimension_unit<dim>::ratio>;
|
||||
using ratio1 = ratio_divide<typename U1::ratio, typename dimension_unit<D1>::ratio>;
|
||||
using ratio2 = ratio_divide<typename U2::ratio, typename dimension_unit<D2>::ratio>;
|
||||
using ratio = ratio_multiply<ratio_divide<ratio1, ratio2>, typename dimension_unit<dim>::ratio>;
|
||||
using unit = downcast_unit<dim, ratio>;
|
||||
using ret = quantity<dim, unit, common_rep>;
|
||||
return ret(lhs.count() / rhs.count());
|
||||
|
Reference in New Issue
Block a user