2019-11-16 18:30:44 +01:00
|
|
|
// 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.
|
|
|
|
|
2023-02-03 16:57:48 +01:00
|
|
|
#include <mp_units/iostream.h>
|
2022-12-29 20:18:48 +01:00
|
|
|
#include <mp_units/systems/isq/space_and_time.h>
|
|
|
|
#include <mp_units/systems/si/unit_symbols.h>
|
|
|
|
#include <mp_units/systems/si/units.h>
|
2019-12-12 13:17:31 +01:00
|
|
|
#include <cmath>
|
2021-03-30 13:21:05 +02:00
|
|
|
#include <exception>
|
2020-09-08 13:09:34 +02:00
|
|
|
#include <iostream>
|
2020-09-04 23:00:57 +02:00
|
|
|
|
2019-11-16 18:30:44 +01:00
|
|
|
namespace {
|
|
|
|
|
2019-12-17 09:12:11 +01:00
|
|
|
template<class T>
|
|
|
|
class measurement {
|
|
|
|
public:
|
|
|
|
using value_type = T;
|
|
|
|
|
|
|
|
measurement() = default;
|
|
|
|
|
2022-04-02 21:36:42 +02:00
|
|
|
constexpr explicit measurement(const value_type& val, const value_type& err = {}) : value_(val)
|
2019-12-17 09:12:11 +01:00
|
|
|
{
|
2021-04-15 14:05:36 +02:00
|
|
|
// it sucks that using declaration cannot be provided for a constructor initializer list
|
|
|
|
using namespace std;
|
|
|
|
uncertainty_ = abs(err);
|
2019-12-17 09:12:11 +01:00
|
|
|
}
|
|
|
|
|
2022-11-11 10:33:24 -10:00
|
|
|
[[nodiscard]] constexpr const value_type& value() const { return value_; }
|
|
|
|
[[nodiscard]] constexpr const value_type& uncertainty() const { return uncertainty_; }
|
2019-12-17 09:12:11 +01:00
|
|
|
|
2022-11-11 10:33:24 -10:00
|
|
|
[[nodiscard]] constexpr value_type relative_uncertainty() const { return uncertainty() / value(); }
|
|
|
|
[[nodiscard]] constexpr value_type lower_bound() const { return value() - uncertainty(); }
|
|
|
|
[[nodiscard]] constexpr value_type upper_bound() const { return value() + uncertainty(); }
|
2019-12-17 09:12:11 +01:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr measurement operator-() const { return measurement(-value(), uncertainty()); }
|
|
|
|
|
|
|
|
[[nodiscard]] friend constexpr measurement operator+(const measurement& lhs, const measurement& rhs)
|
|
|
|
{
|
2021-04-15 14:05:36 +02:00
|
|
|
using namespace std;
|
|
|
|
return measurement(lhs.value() + rhs.value(), hypot(lhs.uncertainty(), rhs.uncertainty()));
|
2019-12-17 09:12:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] friend constexpr measurement operator-(const measurement& lhs, const measurement& rhs)
|
|
|
|
{
|
2021-04-15 14:05:36 +02:00
|
|
|
using namespace std;
|
|
|
|
return measurement(lhs.value() - rhs.value(), hypot(lhs.uncertainty(), rhs.uncertainty()));
|
2019-12-17 09:12:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] friend constexpr measurement operator*(const measurement& lhs, const measurement& rhs)
|
|
|
|
{
|
|
|
|
const auto val = lhs.value() * rhs.value();
|
2021-04-15 14:05:36 +02:00
|
|
|
using namespace std;
|
|
|
|
return measurement(val, val * hypot(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
|
2019-12-17 09:12:11 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 16:30:08 +01:00
|
|
|
[[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());
|
|
|
|
}
|
|
|
|
|
2019-12-17 09:12:11 +01:00
|
|
|
[[nodiscard]] friend constexpr measurement operator/(const measurement& lhs, const measurement& rhs)
|
|
|
|
{
|
|
|
|
const auto val = lhs.value() / rhs.value();
|
2021-04-15 14:05:36 +02:00
|
|
|
using namespace std;
|
|
|
|
return measurement(val, val * hypot(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
|
2019-12-17 09:12:11 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 16:30:08 +01:00
|
|
|
[[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());
|
|
|
|
}
|
|
|
|
|
2020-09-04 23:00:57 +02:00
|
|
|
[[nodiscard]] constexpr auto operator<=>(const measurement&) const = default;
|
2019-12-17 09:12:11 +01:00
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& os, const measurement& v)
|
2019-11-16 18:30:44 +01:00
|
|
|
{
|
2019-12-17 09:12:11 +01:00
|
|
|
return os << v.value() << " ± " << v.uncertainty();
|
2019-11-16 18:30:44 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 09:12:11 +01:00
|
|
|
private:
|
|
|
|
value_type value_{};
|
|
|
|
value_type uncertainty_{};
|
|
|
|
};
|
|
|
|
|
2019-11-16 18:30:44 +01:00
|
|
|
} // namespace
|
|
|
|
|
2022-12-20 17:26:55 +01:00
|
|
|
template<class T>
|
2022-12-29 20:18:48 +01:00
|
|
|
inline constexpr bool mp_units::is_scalar<measurement<T>> = true;
|
2022-12-20 17:26:55 +01:00
|
|
|
template<class T>
|
2022-12-29 20:18:48 +01:00
|
|
|
inline constexpr bool mp_units::is_vector<measurement<T>> = true;
|
2019-11-16 18:30:44 +01:00
|
|
|
|
2022-12-29 20:18:48 +01:00
|
|
|
static_assert(mp_units::RepresentationOf<measurement<double>, mp_units::quantity_character::scalar>);
|
2019-11-16 18:30:44 +01:00
|
|
|
|
2022-12-20 17:26:55 +01:00
|
|
|
namespace {
|
2020-10-06 18:17:52 +02:00
|
|
|
|
2019-12-06 12:18:39 +01:00
|
|
|
void example()
|
2019-11-16 18:30:44 +01:00
|
|
|
{
|
2022-12-29 20:18:48 +01:00
|
|
|
using namespace mp_units;
|
|
|
|
using namespace mp_units::si::unit_symbols;
|
2019-11-16 18:30:44 +01:00
|
|
|
|
2023-02-08 21:47:48 -08:00
|
|
|
const auto a = isq::acceleration(measurement{9.8, 0.1} * (m / s2));
|
2023-02-14 12:58:54 +01:00
|
|
|
const auto t = measurement{1.2, 0.1} * s;
|
2019-11-16 18:30:44 +01:00
|
|
|
|
2023-02-08 21:47:48 -08:00
|
|
|
const QuantityOf<isq::velocity> auto v = a * t;
|
2022-11-11 10:33:24 -10:00
|
|
|
std::cout << a << " * " << t << " = " << v << " = " << v[km / h] << '\n';
|
2019-12-06 12:18:39 +01:00
|
|
|
|
2023-02-14 12:58:54 +01:00
|
|
|
const auto length = measurement{123., 1.} * m;
|
2019-11-16 18:30:44 +01:00
|
|
|
std::cout << "10 * " << length << " = " << 10 * length << '\n';
|
|
|
|
}
|
2019-12-06 12:18:39 +01:00
|
|
|
|
2019-12-17 09:12:11 +01:00
|
|
|
} // namespace
|
2019-12-06 12:18:39 +01:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
example();
|
2019-12-17 09:12:11 +01:00
|
|
|
} catch (const std::exception& ex) {
|
2019-12-06 12:18:39 +01:00
|
|
|
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
|
2019-12-17 09:12:11 +01:00
|
|
|
} catch (...) {
|
2019-12-06 12:18:39 +01:00
|
|
|
std::cerr << "Unhandled unknown exception caught\n";
|
|
|
|
}
|
|
|
|
}
|