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.
|
|
|
|
|
2019-12-06 12:18:39 +01:00
|
|
|
#include <units/physical/si/acceleration.h>
|
2019-12-12 13:17:31 +01:00
|
|
|
#include <cmath>
|
2019-11-16 18:30:44 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-12-17 09:12:11 +01:00
|
|
|
// 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;
|
|
|
|
|
2020-03-25 16:30:08 +01:00
|
|
|
constexpr explicit measurement(const value_type& val, const value_type& err = {}) :
|
2019-12-17 09:12:11 +01:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
return measurement(val, val * rss(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2019-12-17 09:12:11 +01:00
|
|
|
#if __GNUC__ >= 10
|
|
|
|
|
|
|
|
[[nodiscard]] friend constexpr auto operator<=>(const measurement& lhs, const measurement& rhs) = default;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
[[nodiscard]] friend constexpr bool operator==(const measurement& lhs, const measurement& rhs)
|
|
|
|
{
|
|
|
|
return lhs.value() == rhs.value() && lhs.uncertainty() == rhs.uncertainty();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] friend constexpr bool operator!=(const measurement& lhs, const measurement& rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] friend constexpr bool operator<(const measurement& lhs, const measurement& rhs)
|
|
|
|
{
|
|
|
|
return lhs.value() == rhs.value() ? lhs.uncertainty() < rhs.uncertainty() : lhs.value() < rhs.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] friend constexpr bool operator>(const measurement& lhs, const measurement& rhs) { return rhs < lhs; }
|
|
|
|
|
|
|
|
[[nodiscard]] friend constexpr bool operator<=(const measurement& lhs, const measurement& rhs)
|
|
|
|
{
|
|
|
|
return !(rhs < lhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] friend constexpr bool operator>=(const measurement& lhs, const measurement& rhs)
|
|
|
|
{
|
|
|
|
return !(lhs < rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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_{};
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(units::Scalar<measurement<double>>);
|
2019-11-16 18:30:44 +01:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline constexpr bool units::treat_as_floating_point<measurement<T>> = std::is_floating_point_v<T>;
|
|
|
|
|
2019-12-06 12:18:39 +01:00
|
|
|
namespace {
|
2019-11-16 18:30:44 +01:00
|
|
|
|
2019-12-06 12:18:39 +01:00
|
|
|
void example()
|
2019-11-16 18:30:44 +01:00
|
|
|
{
|
2020-05-08 22:39:24 +02:00
|
|
|
using namespace units::physical;
|
2019-11-16 18:30:44 +01:00
|
|
|
|
2019-12-06 12:18:39 +01:00
|
|
|
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));
|
2019-11-16 18:30:44 +01:00
|
|
|
|
2019-12-06 12:18:39 +01:00
|
|
|
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.));
|
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";
|
|
|
|
}
|
|
|
|
}
|