forked from mpusz/mp-units
Design cleanup
- unknown_unit added - examples refactored - base_type renamed to downcast_base_type - scaled_unit renamed to named_scaled_unit - detail::reference_unit renamed to scaled_unit - quantity_test cleanup
This commit is contained in:
+7
-11
@@ -20,15 +20,11 @@
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
# example app
|
||||
add_executable(example example.cpp)
|
||||
target_link_libraries(example
|
||||
PRIVATE
|
||||
mp::units
|
||||
)
|
||||
function(add_example target)
|
||||
add_executable(${target} ${target}.cpp)
|
||||
target_link_libraries(${target} PRIVATE mp::units)
|
||||
endfunction()
|
||||
|
||||
add_executable(measurement measurement.cpp)
|
||||
target_link_libraries(measurement
|
||||
PRIVATE
|
||||
mp::units
|
||||
)
|
||||
add_example(avg_velocity)
|
||||
add_example(measurement)
|
||||
add_example(unknown_dimension)
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
// 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.
|
||||
|
||||
#include <units/physical/si/velocity.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr units::si::velocity<units::si::metre_per_second, int>
|
||||
fixed_int_si_avg_speed(units::si::length<units::si::metre, int> d,
|
||||
units::si::time<units::si::second, int> t)
|
||||
{
|
||||
return d / t;
|
||||
}
|
||||
|
||||
constexpr units::si::velocity<units::si::metre_per_second>
|
||||
fixed_double_si_avg_speed(units::si::length<units::si::metre> d,
|
||||
units::si::time<units::si::second> t)
|
||||
{
|
||||
return d / t;
|
||||
}
|
||||
|
||||
template<typename U1, typename R1, typename U2, typename R2>
|
||||
constexpr units::Velocity AUTO si_avg_speed(units::si::length<U1, R1> d,
|
||||
units::si::time<U2, R2> t)
|
||||
{
|
||||
return d / t;
|
||||
}
|
||||
|
||||
constexpr units::Velocity AUTO avg_speed(units::Length AUTO d, units::Time AUTO t)
|
||||
{
|
||||
return d / t;
|
||||
}
|
||||
|
||||
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);
|
||||
std::cout << "Average speed of a car that makes " << distance << " in "
|
||||
<< duration << " is " << result_in_kmph << ".\n";
|
||||
}
|
||||
|
||||
void example()
|
||||
{
|
||||
using namespace units;
|
||||
using namespace units::si::literals;
|
||||
|
||||
// SI (int)
|
||||
{
|
||||
constexpr Length AUTO distance = 220km; // constructed from a UDL
|
||||
constexpr si::time<si::hour, int> duration(2); // constructed from a value
|
||||
|
||||
std::cout << "SI units with 'int' as representation\n";
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
// SI (double)
|
||||
{
|
||||
constexpr Length AUTO distance = 220.km; // constructed from a UDL
|
||||
constexpr si::time<si::hour> duration(2); // constructed from a value
|
||||
|
||||
std::cout << "\nSI 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
|
||||
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<int>(distance), quantity_cast<int>(duration)));
|
||||
|
||||
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
|
||||
print_result(distance, duration, si_avg_speed(distance, duration));
|
||||
print_result(distance, duration, avg_speed(distance, duration));
|
||||
}
|
||||
|
||||
// Customary Units (int)
|
||||
{
|
||||
constexpr Length AUTO distance = 140mi; // constructed from a UDL
|
||||
constexpr si::time<si::hour, int> duration(2); // constructed from a value
|
||||
|
||||
std::cout << "\nUS Customary Units with 'int' as representation\n";
|
||||
|
||||
// it is not possible to make a lossless conversion of miles 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));
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
// Customary Units (double)
|
||||
{
|
||||
constexpr Length AUTO distance = 140.mi; // constructed from a UDL
|
||||
constexpr si::time<si::hour> duration(2); // constructed from a value
|
||||
|
||||
std::cout << "\nUS Customary 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
|
||||
// also it is not possible to make a lossless conversion of miles to meters on an integral type
|
||||
// (explicit cast needed)
|
||||
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<si::metre, int>(distance), quantity_cast<int>(duration)));
|
||||
|
||||
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
|
||||
print_result(distance, duration, si_avg_speed(distance, duration));
|
||||
print_result(distance, duration, avg_speed(distance, duration));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
try {
|
||||
example();
|
||||
}
|
||||
catch (const std::exception& ex) {
|
||||
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
|
||||
}
|
||||
catch (...) {
|
||||
std::cerr << "Unhandled unknown exception caught\n";
|
||||
}
|
||||
}
|
||||
+27
-16
@@ -20,14 +20,11 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include <units/dimensions/acceleration.h>
|
||||
#include <units/physical/si/acceleration.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
||||
template<typename T, template<typename> typename Trait>
|
||||
concept Satisfies = Trait<T>::value;
|
||||
|
||||
// root sum of squares
|
||||
template<typename T>
|
||||
T rss(const T& v1, const T& v2)
|
||||
@@ -118,24 +115,38 @@ namespace {
|
||||
value_type uncertainty_{};
|
||||
};
|
||||
|
||||
template<units::Unit U>
|
||||
using m_quantity = units::quantity<U, measurement<double>>;
|
||||
|
||||
} // namespace
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool units::treat_as_floating_point<measurement<T>> = std::is_floating_point_v<T>;
|
||||
|
||||
namespace {
|
||||
|
||||
void example()
|
||||
{
|
||||
using namespace units;
|
||||
|
||||
const auto a = si::acceleration<si::metre_per_second_sq, measurement<double>>(measurement(9.8, 0.1));
|
||||
const auto t = si::time<si::second, measurement<double>>(measurement(1.2, 0.1));
|
||||
|
||||
const Velocity AUTO v1 = a * t;
|
||||
std::cout << a << " * " << t << " = " << v1 << " = " << quantity_cast<si::kilometre_per_hour>(v1) << '\n';
|
||||
|
||||
si::length<si::metre, measurement<double>> length(measurement(123., 1.));
|
||||
std::cout << "10 * " << length << " = " << 10 * length << '\n';
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto a = m_quantity<units::metre_per_second_sq>(measurement(9.8, 0.1));
|
||||
const auto t = m_quantity<units::second>(measurement(1.2, 0.1));
|
||||
|
||||
units::Velocity AUTO v1 = a * t;
|
||||
m_quantity<units::kilometre_per_hour> v2(v1);
|
||||
std::cout << a << " * " << t << " = " << v1 << " = " << v2 << '\n';
|
||||
|
||||
m_quantity<units::metre> length(measurement(123., 1.));
|
||||
std::cout << "10 * " << length << " = " << 10 * length << '\n';
|
||||
try {
|
||||
example();
|
||||
}
|
||||
catch (const std::exception& ex) {
|
||||
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
|
||||
}
|
||||
catch (...) {
|
||||
std::cerr << "Unhandled unknown exception caught\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,43 +20,43 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include <units/dimensions/velocity.h>
|
||||
#include <units/physical/si/velocity.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace units::literals;
|
||||
|
||||
template<units::Length D, units::Time T>
|
||||
constexpr units::Velocity AUTO avg_speed(D d, T t)
|
||||
{
|
||||
return d / t;
|
||||
}
|
||||
|
||||
template<units::Velocity V, units::Time T>
|
||||
void example_1(V v, T t)
|
||||
void example()
|
||||
{
|
||||
const units::Length AUTO distance = v * t;
|
||||
std::cout << "A car driving " << v << " in a time of " << t << " will pass "
|
||||
<< units::quantity_cast<units::quantity<units::metre, double>>(distance) << ".\n";
|
||||
using namespace units::si::literals;
|
||||
|
||||
units::Length AUTO d1 = 123m;
|
||||
units::Time AUTO t1 = 10s;
|
||||
units::Velocity AUTO v1 = avg_speed(d1, t1);
|
||||
|
||||
auto temp1 = v1 * 50m; // produces intermediate unknown dimension with 'unknown_unit' as its 'coherent_unit'
|
||||
units::Velocity AUTO v2 = temp1 / 100m; // back to known dimensions again
|
||||
units::Length AUTO d2 = v2 * 60s;
|
||||
|
||||
std::cout << "d1 = " << d1 << '\n';
|
||||
std::cout << "t1 = " << t1 << '\n';
|
||||
std::cout << "v1 = " << v1 << '\n';
|
||||
std::cout << "temp1 = " << temp1 << '\n';
|
||||
std::cout << "v2 = " << v2 << '\n';
|
||||
std::cout << "d2 = " << d2 << '\n';
|
||||
}
|
||||
|
||||
void example_2(double distance_v, double duration_v)
|
||||
{
|
||||
units::quantity<units::kilometre> distance(distance_v);
|
||||
units::quantity<units::hour> duration(duration_v);
|
||||
const auto kmph = quantity_cast<units::kilometre_per_hour>(avg_speed(distance, duration));
|
||||
std::cout << "Average speed of a car that makes " << distance << " in "
|
||||
<< duration << " is " << kmph << ".\n";
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
try {
|
||||
example_1(60kmph, 10.0min);
|
||||
example_2(220, 2);
|
||||
example();
|
||||
}
|
||||
catch (const std::exception& ex) {
|
||||
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
|
||||
Reference in New Issue
Block a user