From 75e9b1831e252610f808810eaf094501e1b9bef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com> Date: Sat, 31 Jul 2021 15:38:12 +0200 Subject: [PATCH] Add NaNHelper utility The aim of this helper is to allow to print signaling NaN. This should help to debug uninitialized matrices of vectors values while printing them. --- src/utils/NaNHelper.hpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/utils/NaNHelper.hpp diff --git a/src/utils/NaNHelper.hpp b/src/utils/NaNHelper.hpp new file mode 100644 index 000000000..d77d1d9f1 --- /dev/null +++ b/src/utils/NaNHelper.hpp @@ -0,0 +1,38 @@ +#ifndef NAN_HELPER_HPP +#define NAN_HELPER_HPP + +#include <cmath> +#include <iostream> +#include <type_traits> + +template <typename T> +class NaNHelper +{ + private: + const T& m_t; + + public: + friend std::ostream& + operator<<(std::ostream& os, const NaNHelper<T>& helper) + { + if constexpr (std::is_arithmetic_v<T>) { + if (std::isnan(helper.m_t)) { + os << "nan"; + } else { + os << helper.m_t; + } + } else { + os << helper.m_t; + } + return os; + } + NaNHelper(const T& t) : m_t{t} {} + + NaNHelper(NaNHelper&&) = delete; + NaNHelper(const NaNHelper&) = delete; + + NaNHelper() = delete; + ~NaNHelper() = default; +}; + +#endif // NAN_HELPER_HPP -- GitLab