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