From 06f75b2b3072b3e7a8e6cb6755208deb9c603d63 Mon Sep 17 00:00:00 2001 From: Francesco Biscani Date: Wed, 13 Sep 2023 09:33:44 +0200 Subject: [PATCH] Add several noexcept decorations to the numbers API. --- include/heyoka/number.hpp | 18 +++++++++--------- src/number.cpp | 23 ++++++++++++++--------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/include/heyoka/number.hpp b/include/heyoka/number.hpp index 8c3d9d17f..eef551436 100644 --- a/include/heyoka/number.hpp +++ b/include/heyoka/number.hpp @@ -70,12 +70,12 @@ class HEYOKA_DLL_PUBLIC number } public: - number(); - explicit number(float); - explicit number(double); - explicit number(long double); + number() noexcept; + explicit number(float) noexcept; + explicit number(double) noexcept; + explicit number(long double) noexcept; #if defined(HEYOKA_HAVE_REAL128) - explicit number(mppp::real128); + explicit number(mppp::real128) noexcept; #endif #if defined(HEYOKA_HAVE_REAL) explicit number(mppp::real); @@ -87,7 +87,7 @@ class HEYOKA_DLL_PUBLIC number number &operator=(const number &); number &operator=(number &&) noexcept; - [[nodiscard]] const value_type &value() const; + [[nodiscard]] const value_type &value() const noexcept; }; namespace detail @@ -113,9 +113,9 @@ HEYOKA_DLL_PUBLIC number operator-(const number &, const number &); HEYOKA_DLL_PUBLIC number operator*(const number &, const number &); HEYOKA_DLL_PUBLIC number operator/(const number &, const number &); -HEYOKA_DLL_PUBLIC bool operator==(const number &, const number &); -HEYOKA_DLL_PUBLIC bool operator!=(const number &, const number &); -HEYOKA_DLL_PUBLIC bool operator<(const number &, const number &); +HEYOKA_DLL_PUBLIC bool operator==(const number &, const number &) noexcept; +HEYOKA_DLL_PUBLIC bool operator!=(const number &, const number &) noexcept; +HEYOKA_DLL_PUBLIC bool operator<(const number &, const number &) noexcept; HEYOKA_DLL_PUBLIC number exp(const number &); HEYOKA_DLL_PUBLIC number binomial(const number &, const number &); diff --git a/src/number.cpp b/src/number.cpp index 8e1036390..e29b6610a 100644 --- a/src/number.cpp +++ b/src/number.cpp @@ -72,17 +72,17 @@ HEYOKA_BEGIN_NAMESPACE -number::number() : number(0.) {} +number::number() noexcept : number(0.) {} -number::number(float x) : m_value(x) {} +number::number(float x) noexcept : m_value(x) {} -number::number(double x) : m_value(x) {} +number::number(double x) noexcept : m_value(x) {} -number::number(long double x) : m_value(x) {} +number::number(long double x) noexcept : m_value(x) {} #if defined(HEYOKA_HAVE_REAL128) -number::number(mppp::real128 x) : m_value(x) {} +number::number(mppp::real128 x) noexcept : m_value(x) {} #endif @@ -126,7 +126,7 @@ number &number::operator=(number &&other) noexcept return *this; } -const number::value_type &number::value() const +const number::value_type &number::value() const noexcept { return m_value; } @@ -253,6 +253,11 @@ bool is_integer(const number &n) n.value()); } +number operator+(number n) +{ + return n; +} + number operator-(const number &n) { return std::visit([](const auto &arg) { return number{-arg}; }, n.value()); @@ -377,7 +382,7 @@ number operator/(const number &n1, const number &n2) // verifying decompositions: when the original expression is // reconstructed from the subexpressions and we compare, the // check would fail due to NaN != NaN. -bool operator==(const number &n1, const number &n2) +bool operator==(const number &n1, const number &n2) noexcept { return std::visit( [](const auto &v1, const auto &v2) -> bool { @@ -396,7 +401,7 @@ bool operator==(const number &n1, const number &n2) n1.value(), n2.value()); } -bool operator!=(const number &n1, const number &n2) +bool operator!=(const number &n1, const number &n2) noexcept { return !(n1 == n2); } @@ -405,7 +410,7 @@ bool operator!=(const number &n1, const number &n2) // same type in order to be considered equivalent. Also, NaNs are considered // greater than any other value, so that they will be placed at the // end of a sorted range. -bool operator<(const number &n1, const number &n2) +bool operator<(const number &n1, const number &n2) noexcept { return std::visit( [&](const auto &v1, const auto &v2) -> bool {