Skip to content

Commit

Permalink
Add several noexcept decorations to the numbers API.
Browse files Browse the repository at this point in the history
  • Loading branch information
bluescarni committed Sep 13, 2023
1 parent 475489b commit 06f75b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
18 changes: 9 additions & 9 deletions include/heyoka/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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 &);
Expand Down
23 changes: 14 additions & 9 deletions src/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
Expand All @@ -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 {
Expand Down

0 comments on commit 06f75b2

Please sign in to comment.