ceil.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_CEIL_HPP
  6. #define BOOST_MATH_CCMATH_CEIL_HPP
  7. #include <boost/math/ccmath/detail/config.hpp>
  8. #ifdef BOOST_MATH_NO_CCMATH
  9. #error "The header <boost/math/ceil.hpp> can only be used in C++17 and later."
  10. #endif
  11. #include <boost/math/ccmath/floor.hpp>
  12. #include <boost/math/ccmath/abs.hpp>
  13. #include <boost/math/ccmath/isinf.hpp>
  14. #include <boost/math/ccmath/isnan.hpp>
  15. namespace boost::math::ccmath {
  16. namespace detail {
  17. template <typename T>
  18. inline constexpr T ceil_impl(T arg) noexcept
  19. {
  20. T result = boost::math::ccmath::floor(arg);
  21. if(result == arg)
  22. {
  23. return result;
  24. }
  25. else
  26. {
  27. return result + 1;
  28. }
  29. }
  30. } // Namespace detail
  31. template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
  32. inline constexpr Real ceil(Real arg) noexcept
  33. {
  34. if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
  35. {
  36. return boost::math::ccmath::abs(arg) == Real(0) ? arg :
  37. boost::math::ccmath::isinf(arg) ? arg :
  38. boost::math::ccmath::isnan(arg) ? arg :
  39. boost::math::ccmath::detail::ceil_impl(arg);
  40. }
  41. else
  42. {
  43. using std::ceil;
  44. return ceil(arg);
  45. }
  46. }
  47. template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
  48. inline constexpr double ceil(Z arg) noexcept
  49. {
  50. return boost::math::ccmath::ceil(static_cast<double>(arg));
  51. }
  52. inline constexpr float ceilf(float arg) noexcept
  53. {
  54. return boost::math::ccmath::ceil(arg);
  55. }
  56. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  57. inline constexpr long double ceill(long double arg) noexcept
  58. {
  59. return boost::math::ccmath::ceil(arg);
  60. }
  61. #endif
  62. } // Namespaces
  63. #endif // BOOST_MATH_CCMATH_CEIL_HPP