Skip to content

Commit c74437a

Browse files
committed
add isNan constexpr function
1 parent 4864f14 commit c74437a

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

include/units/units.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,13 @@ NEW_UNIT(Luminosity, candela, 0, 0, 0, 0, 0, 0, 1, 0);
413413
NEW_UNIT(Moles, mol, 0, 0, 0, 0, 0, 0, 0, 1);
414414

415415
namespace units {
416+
constexpr bool isNan(double lhs) {
417+
// Use the fact that NaN is not equal to itself
418+
return lhs != lhs;
419+
}
420+
421+
template <isQuantity Q> constexpr bool isNan(const Q& lhs) { return isNan(lhs.internal()); }
422+
416423
template <isQuantity Q> constexpr Q abs(const Q& lhs) { return Q(std::abs(lhs.internal())); }
417424

418425
template <isQuantity Q, isQuantity R> constexpr Q max(const Q& lhs, const R& rhs)

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ constexpr void sgnTests() {
4444
static_assert(units::sgn(15_stDeg) == 1);
4545
static_assert(units::sgn(-15_m) == -1);
4646
static_assert(units::sgn(0_kg) == 0);
47-
static_assert(std::isnan(units::sgn(std::numeric_limits<double>::quiet_NaN())));
47+
static_assert(units::isNan(units::sgn(std::numeric_limits<double>::quiet_NaN())));
4848
static_assert(units::sgn(std::numeric_limits<double>::infinity()) == 1);
4949
static_assert(units::sgn(-std::numeric_limits<double>::infinity()) == -1);
5050
}

0 commit comments

Comments
 (0)