isfinite.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_ISFINITE
  6. #define BOOST_MATH_CCMATH_ISFINITE
  7. #include <cmath>
  8. #include <type_traits>
  9. #include <boost/math/tools/is_constant_evaluated.hpp>
  10. #include <boost/math/ccmath/isinf.hpp>
  11. #include <boost/math/ccmath/isnan.hpp>
  12. #include <boost/math/tools/is_standalone.hpp>
  13. #ifndef BOOST_MATH_STANDALONE
  14. #include <boost/config.hpp>
  15. #ifdef BOOST_NO_CXX17_IF_CONSTEXPR
  16. #error "The header <boost/math/norms.hpp> can only be used in C++17 and later."
  17. #endif
  18. #endif
  19. namespace boost::math::ccmath {
  20. template <typename T>
  21. inline constexpr bool isfinite(T x)
  22. {
  23. if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
  24. {
  25. // bool isfinite (IntegralType arg) is a set of overloads accepting the arg argument of any integral type
  26. // equivalent to casting the integral argument arg to double (e.g. static_cast<double>(arg))
  27. if constexpr (std::is_integral_v<T>)
  28. {
  29. return !boost::math::ccmath::isinf(static_cast<double>(x)) && !boost::math::ccmath::isnan(static_cast<double>(x));
  30. }
  31. else
  32. {
  33. return !boost::math::ccmath::isinf(x) && !boost::math::ccmath::isnan(x);
  34. }
  35. }
  36. else
  37. {
  38. using std::isfinite;
  39. if constexpr (!std::is_integral_v<T>)
  40. {
  41. return isfinite(x);
  42. }
  43. else
  44. {
  45. return isfinite(static_cast<double>(x));
  46. }
  47. }
  48. }
  49. }
  50. #endif // BOOST_MATH_CCMATH_ISFINITE