fpclassify.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 <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/ccmath/isfinite.hpp>
  15. namespace boost::math::ccmath {
  16. template <typename T, std::enable_if_t<!std::is_integral_v<T>, bool> = true>
  17. inline constexpr int fpclassify(T x)
  18. {
  19. if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
  20. {
  21. return boost::math::ccmath::isnan(x) ? FP_NAN :
  22. boost::math::ccmath::isinf(x) ? FP_INFINITE :
  23. boost::math::ccmath::abs(x) == T(0) ? FP_ZERO :
  24. boost::math::ccmath::abs(x) > 0 && boost::math::ccmath::abs(x) < (std::numeric_limits<T>::min)() ? FP_SUBNORMAL : FP_NORMAL;
  25. }
  26. else
  27. {
  28. using std::fpclassify;
  29. return fpclassify(x);
  30. }
  31. }
  32. template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
  33. inline constexpr int fpclassify(Z x)
  34. {
  35. return boost::math::ccmath::fpclassify(static_cast<double>(x));
  36. }
  37. }
  38. #endif // BOOST_MATH_CCMATH_FPCLASSIFY