isnormal.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // (C) Copyright Matt Borland 2021.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_ISNORMAL_HPP
  6. #define BOOST_MATH_ISNORMAL_HPP
  7. #include <cmath>
  8. #include <limits>
  9. #include <type_traits>
  10. #include <boost/math/tools/is_constant_evaluated.hpp>
  11. #include <boost/math/ccmath/abs.hpp>
  12. #include <boost/math/ccmath/isinf.hpp>
  13. #include <boost/math/ccmath/isnan.hpp>
  14. #include <boost/math/tools/is_standalone.hpp>
  15. #ifndef BOOST_MATH_STANDALONE
  16. #include <boost/config.hpp>
  17. #ifdef BOOST_NO_CXX17_IF_CONSTEXPR
  18. #error "The header <boost/math/norms.hpp> can only be used in C++17 and later."
  19. #endif
  20. #endif
  21. namespace boost::math::ccmath {
  22. template <typename T>
  23. inline constexpr bool isnormal(T x)
  24. {
  25. if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
  26. {
  27. return x == T(0) ? false :
  28. boost::math::ccmath::isinf(x) ? false :
  29. boost::math::ccmath::isnan(x) ? false :
  30. boost::math::ccmath::abs(x) < (std::numeric_limits<T>::min)() ? false : true;
  31. }
  32. else
  33. {
  34. using std::isnormal;
  35. if constexpr (!std::is_integral_v<T>)
  36. {
  37. return isnormal(x);
  38. }
  39. else
  40. {
  41. return isnormal(static_cast<double>(x));
  42. }
  43. }
  44. }
  45. }
  46. #endif // BOOST_MATH_ISNORMAL_HPP