ceil.hpp 1.8 KB

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