mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-04 20:54:28 +02:00
measurement example updated with a starship operator
This commit is contained in:
@@ -40,8 +40,10 @@ namespace {
|
|||||||
|
|
||||||
measurement() = default;
|
measurement() = default;
|
||||||
|
|
||||||
constexpr /* explicit */ measurement(const value_type& val, const value_type& err = {}): // cannot be explicit as `magma` concept requires implicit conversions :-(
|
constexpr /* explicit */ measurement(const value_type& val, const value_type& err = {}) :
|
||||||
value_(val), uncertainty_(std::abs(err))
|
// cannot be explicit as `magma` concept requires implicit conversions :-(
|
||||||
|
value_(val),
|
||||||
|
uncertainty_(std::abs(err))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,6 +78,13 @@ namespace {
|
|||||||
return measurement(val, val * rss(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
|
return measurement(val, val * rss(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if __GNUC__ >= 10
|
||||||
|
|
||||||
|
[[nodiscard]] friend constexpr auto operator<=>(const measurement& lhs, const measurement& rhs) = default;
|
||||||
|
[[nodiscard]] friend constexpr bool operator==(const measurement& lhs, const measurement& rhs) = default;
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
[[nodiscard]] friend constexpr bool operator==(const measurement& lhs, const measurement& rhs)
|
[[nodiscard]] friend constexpr bool operator==(const measurement& lhs, const measurement& rhs)
|
||||||
{
|
{
|
||||||
return lhs.value() == rhs.value() && lhs.uncertainty() == rhs.uncertainty();
|
return lhs.value() == rhs.value() && lhs.uncertainty() == rhs.uncertainty();
|
||||||
@@ -91,10 +100,7 @@ namespace {
|
|||||||
return lhs.value() == rhs.value() ? lhs.uncertainty() < rhs.uncertainty() : lhs.value() < rhs.value();
|
return lhs.value() == rhs.value() ? lhs.uncertainty() < rhs.uncertainty() : lhs.value() < rhs.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] friend constexpr bool operator>(const measurement& lhs, const measurement& rhs)
|
[[nodiscard]] friend constexpr bool operator>(const measurement& lhs, const measurement& rhs) { return rhs < lhs; }
|
||||||
{
|
|
||||||
return rhs < lhs;
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] friend constexpr bool operator<=(const measurement& lhs, const measurement& rhs)
|
[[nodiscard]] friend constexpr bool operator<=(const measurement& lhs, const measurement& rhs)
|
||||||
{
|
{
|
||||||
@@ -106,6 +112,8 @@ namespace {
|
|||||||
return !(lhs < rhs);
|
return !(lhs < rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream& os, const measurement& v)
|
friend std::ostream& operator<<(std::ostream& os, const measurement& v)
|
||||||
{
|
{
|
||||||
return os << v.value() << " ± " << v.uncertainty();
|
return os << v.value() << " ± " << v.uncertainty();
|
||||||
@@ -116,6 +124,8 @@ namespace {
|
|||||||
value_type uncertainty_{};
|
value_type uncertainty_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static_assert(units::Scalar<measurement<double>>);
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -143,11 +153,9 @@ int main()
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
example();
|
example();
|
||||||
}
|
} catch (const std::exception& ex) {
|
||||||
catch (const std::exception& ex) {
|
|
||||||
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
|
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
|
||||||
}
|
} catch (...) {
|
||||||
catch (...) {
|
|
||||||
std::cerr << "Unhandled unknown exception caught\n";
|
std::cerr << "Unhandled unknown exception caught\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user