Skip to content

Commit

Permalink
add isNan constexpr function
Browse files Browse the repository at this point in the history
  • Loading branch information
SizzinSeal committed Jan 7, 2025
1 parent 4864f14 commit c74437a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions include/units/units.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,13 @@ NEW_UNIT(Luminosity, candela, 0, 0, 0, 0, 0, 0, 1, 0);
NEW_UNIT(Moles, mol, 0, 0, 0, 0, 0, 0, 0, 1);

namespace units {
constexpr bool isNan(double lhs) {
// Use the fact that NaN is not equal to itself
return lhs != lhs;
}

template <isQuantity Q> constexpr bool isNan(const Q& lhs) { return isNan(lhs.internal()); }

template <isQuantity Q> constexpr Q abs(const Q& lhs) { return Q(std::abs(lhs.internal())); }

template <isQuantity Q, isQuantity R> constexpr Q max(const Q& lhs, const R& rhs)
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ constexpr void sgnTests() {
static_assert(units::sgn(15_stDeg) == 1);
static_assert(units::sgn(-15_m) == -1);
static_assert(units::sgn(0_kg) == 0);
static_assert(std::isnan(units::sgn(std::numeric_limits<double>::quiet_NaN())));
static_assert(units::isNan(units::sgn(std::numeric_limits<double>::quiet_NaN())));
static_assert(units::sgn(std::numeric_limits<double>::infinity()) == 1);
static_assert(units::sgn(-std::numeric_limits<double>::infinity()) == -1);
}
Expand Down

0 comments on commit c74437a

Please sign in to comment.