fpclassify.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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_CCMATH_FPCLASSIFY
  6. #define BOOST_MATH_CCMATH_FPCLASSIFY
  7. #include <boost/math/ccmath/detail/config.hpp>
  8. #ifdef BOOST_MATH_NO_CCMATH
  9. #error "The header <boost/math/fpclassify.hpp> can only be used in C++17 and later."
  10. #endif
  11. #include <boost/math/special_functions/fpclassify.hpp>
  12. #include <boost/math/ccmath/abs.hpp>
  13. #include <boost/math/ccmath/isinf.hpp>
  14. #include <boost/math/ccmath/isnan.hpp>
  15. #include <boost/math/ccmath/isfinite.hpp>
  16. namespace boost::math::ccmath {
  17. template <typename T, std::enable_if_t<!std::is_integral_v<T>, bool> = true>
  18. inline constexpr int fpclassify BOOST_MATH_PREVENT_MACRO_SUBSTITUTION(T x)
  19. {
  20. if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
  21. {
  22. return (boost::math::ccmath::isnan)(x) ? FP_NAN :
  23. (boost::math::ccmath::isinf)(x) ? FP_INFINITE :
  24. boost::math::ccmath::abs(x) == T(0) ? FP_ZERO :
  25. boost::math::ccmath::abs(x) > 0 && boost::math::ccmath::abs(x) < (std::numeric_limits<T>::min)() ? FP_SUBNORMAL : FP_NORMAL;
  26. }
  27. else
  28. {
  29. using boost::math::fpclassify;
  30. return (fpclassify)(x);
  31. }
  32. }
  33. template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
  34. inline constexpr int fpclassify(Z x)
  35. {
  36. return boost::math::ccmath::fpclassify(static_cast<double>(x));
  37. }
  38. }
  39. #endif // BOOST_MATH_CCMATH_FPCLASSIFY