mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-01 03:14:29 +02:00
refactor: measurement
example moved to a root examples directory
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
measurement
|
||||
===========
|
||||
|
||||
.. literalinclude:: ../../example/references/measurement.cpp
|
||||
.. literalinclude:: ../../example/measurement.cpp
|
||||
:caption: measurement.cpp
|
||||
:start-at: #include
|
||||
:linenos:
|
||||
|
@@ -33,6 +33,7 @@ endfunction()
|
||||
add_example(conversion_factor mp-units::core-fmt mp-units::core-io mp-units::si)
|
||||
add_example(custom_systems mp-units::core-io mp-units::si)
|
||||
add_example(hello_units mp-units::core-fmt mp-units::core-io mp-units::si mp-units::si-international)
|
||||
add_example(measurement mp-units::core-io mp-units::si)
|
||||
|
||||
if(NOT UNITS_LIBCXX)
|
||||
add_subdirectory(glide_computer)
|
||||
|
@@ -37,7 +37,6 @@ add_example(capacitor_time_curve mp-units::core-io mp-units::si)
|
||||
add_example(clcpp_response mp-units::core-fmt mp-units::core-io mp-units::si mp-units::si-iau mp-units::si-imperial mp-units::si-international mp-units::si-typographic mp-units::si-us)
|
||||
add_example(experimental_angle mp-units::core-fmt mp-units::core-io mp-units::si)
|
||||
add_example(foot_pound_second mp-units::core-fmt mp-units::si-fps)
|
||||
add_example(measurement mp-units::core-io mp-units::si)
|
||||
add_example(total_energy mp-units::core-io mp-units::si mp-units::isq-natural)
|
||||
add_example(unknown_dimension mp-units::core-io mp-units::si)
|
||||
|
||||
|
@@ -31,13 +31,6 @@
|
||||
|
||||
namespace {
|
||||
|
||||
// root sum of squares
|
||||
template<typename T>
|
||||
T rss(const T& v1, const T& v2)
|
||||
{
|
||||
return std::sqrt(std::pow(v1, 2) + std::pow(v2, 2));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
class measurement {
|
||||
public:
|
||||
@@ -46,9 +39,11 @@ public:
|
||||
measurement() = default;
|
||||
|
||||
constexpr explicit measurement(const value_type& val, const value_type& err = {}) :
|
||||
value_(val),
|
||||
uncertainty_(std::abs(err))
|
||||
value_(val)
|
||||
{
|
||||
// it sucks that using declaration cannot be provided for a constructor initializer list
|
||||
using namespace std;
|
||||
uncertainty_ = abs(err);
|
||||
}
|
||||
|
||||
constexpr const value_type& value() const { return value_; }
|
||||
@@ -62,18 +57,21 @@ public:
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator+(const measurement& lhs, const measurement& rhs)
|
||||
{
|
||||
return measurement(lhs.value() + rhs.value(), rss(lhs.uncertainty(), rhs.uncertainty()));
|
||||
using namespace std;
|
||||
return measurement(lhs.value() + rhs.value(), hypot(lhs.uncertainty(), rhs.uncertainty()));
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator-(const measurement& lhs, const measurement& rhs)
|
||||
{
|
||||
return measurement(lhs.value() - rhs.value(), rss(lhs.uncertainty(), rhs.uncertainty()));
|
||||
using namespace std;
|
||||
return measurement(lhs.value() - rhs.value(), hypot(lhs.uncertainty(), rhs.uncertainty()));
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator*(const measurement& lhs, const measurement& rhs)
|
||||
{
|
||||
const auto val = lhs.value() * rhs.value();
|
||||
return measurement(val, val * rss(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
|
||||
using namespace std;
|
||||
return measurement(val, val * hypot(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator*(const measurement& lhs, const value_type& value)
|
||||
@@ -91,7 +89,8 @@ public:
|
||||
[[nodiscard]] friend constexpr measurement operator/(const measurement& lhs, const measurement& rhs)
|
||||
{
|
||||
const auto val = lhs.value() / rhs.value();
|
||||
return measurement(val, val * rss(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
|
||||
using namespace std;
|
||||
return measurement(val, val * hypot(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator/(const measurement& lhs, const value_type& value)
|
@@ -37,7 +37,6 @@ add_example(capacitor_time_curve mp-units::core-io mp-units::si)
|
||||
add_example(clcpp_response mp-units::core-fmt mp-units::core-io mp-units::si mp-units::si-iau mp-units::si-imperial mp-units::si-international mp-units::si-typographic mp-units::si-us)
|
||||
add_example(experimental_angle mp-units::core-fmt mp-units::core-io mp-units::si)
|
||||
add_example(foot_pound_second mp-units::core-fmt mp-units::si-fps)
|
||||
add_example(measurement mp-units::core-io mp-units::si)
|
||||
add_example(total_energy mp-units::core-io mp-units::si mp-units::isq-natural)
|
||||
add_example(unknown_dimension mp-units::core-io mp-units::si)
|
||||
|
||||
|
@@ -1,157 +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.
|
||||
|
||||
#include <units/isq/si/acceleration.h>
|
||||
#include <units/isq/si/length.h>
|
||||
#include <units/isq/si/speed.h>
|
||||
#include <units/isq/si/time.h>
|
||||
#include <units/quantity_io.h>
|
||||
#include <cmath>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
||||
// root sum of squares
|
||||
template<typename T>
|
||||
T rss(const T& v1, const T& v2)
|
||||
{
|
||||
return std::sqrt(std::pow(v1, 2) + std::pow(v2, 2));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
class measurement {
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
measurement() = default;
|
||||
|
||||
constexpr explicit measurement(const value_type& val, const value_type& err = {}) :
|
||||
value_(val),
|
||||
uncertainty_(std::abs(err))
|
||||
{
|
||||
}
|
||||
|
||||
constexpr const value_type& value() const { return value_; }
|
||||
constexpr const value_type& uncertainty() const { return uncertainty_; }
|
||||
|
||||
constexpr value_type relative_uncertainty() const { return uncertainty() / value(); }
|
||||
constexpr value_type lower_bound() const { return value() - uncertainty(); }
|
||||
constexpr value_type upper_bound() const { return value() + uncertainty(); }
|
||||
|
||||
[[nodiscard]] constexpr measurement operator-() const { return measurement(-value(), uncertainty()); }
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator+(const measurement& lhs, const measurement& rhs)
|
||||
{
|
||||
return measurement(lhs.value() + rhs.value(), rss(lhs.uncertainty(), rhs.uncertainty()));
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator-(const measurement& lhs, const measurement& rhs)
|
||||
{
|
||||
return measurement(lhs.value() - rhs.value(), rss(lhs.uncertainty(), rhs.uncertainty()));
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator*(const measurement& lhs, const measurement& rhs)
|
||||
{
|
||||
const auto val = lhs.value() * rhs.value();
|
||||
return measurement(val, val * rss(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator*(const measurement& lhs, const value_type& value)
|
||||
{
|
||||
const auto val = lhs.value() * value;
|
||||
return measurement(val, val * lhs.relative_uncertainty());
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator*(const value_type& value, const measurement& rhs)
|
||||
{
|
||||
const auto val = rhs.value() * value;
|
||||
return measurement(val, val * rhs.relative_uncertainty());
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator/(const measurement& lhs, const measurement& rhs)
|
||||
{
|
||||
const auto val = lhs.value() / rhs.value();
|
||||
return measurement(val, val * rss(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator/(const measurement& lhs, const value_type& value)
|
||||
{
|
||||
const auto val = lhs.value() / value;
|
||||
return measurement(val, val * lhs.relative_uncertainty());
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr measurement operator/(const value_type& value, const measurement& rhs)
|
||||
{
|
||||
const auto val = value / rhs.value();
|
||||
return measurement(val, val * rhs.relative_uncertainty());
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr auto operator<=>(const measurement&) const = default;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const measurement& v)
|
||||
{
|
||||
return os << v.value() << " ± " << v.uncertainty();
|
||||
}
|
||||
|
||||
private:
|
||||
value_type value_{};
|
||||
value_type uncertainty_{};
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
static_assert(units::Representation<measurement<double>>);
|
||||
|
||||
void example()
|
||||
{
|
||||
using namespace units::isq;
|
||||
|
||||
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 Speed auto v1 = a * t;
|
||||
#if UNITS_DOWNCAST_MODE == 0
|
||||
std::cout << a << " * " << t << " = " << v1 << " = " << quantity_cast<si::dim_speed, si::kilometre_per_hour>(v1) << '\n';
|
||||
#else
|
||||
std::cout << a << " * " << t << " = " << v1 << " = " << quantity_cast<si::kilometre_per_hour>(v1) << '\n';
|
||||
#endif
|
||||
|
||||
si::length<si::metre, measurement<double>> length(measurement(123., 1.));
|
||||
std::cout << "10 * " << length << " = " << 10 * length << '\n';
|
||||
}
|
||||
|
||||
} // 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";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user