measurement example updated with a starship operator

This commit is contained in:
Mateusz Pusz
2019-12-17 09:12:11 +01:00
parent d5f7de8ecb
commit 1b2d27a64c

View File

@@ -26,22 +26,24 @@
namespace { namespace {
// root sum of squares // root sum of squares
template<typename T> template<typename T>
T rss(const T& v1, const T& v2) T rss(const T& v1, const T& v2)
{ {
return std::sqrt(std::pow(v1, 2) + std::pow(v2, 2)); return std::sqrt(std::pow(v1, 2) + std::pow(v2, 2));
} }
template<class T> template<class T>
class measurement { class measurement {
public: public:
using value_type = T; using value_type = T;
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,15 +112,19 @@ 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();
} }
private: private:
value_type value_{}; value_type value_{};
value_type uncertainty_{}; value_type uncertainty_{};
}; };
static_assert(units::Scalar<measurement<double>>);
} // namespace } // namespace
@@ -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";
} }
} }