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)
6 #ifndef BOOST_MATH_CCMATH_TRUNC_HPP
7 #define BOOST_MATH_CCMATH_TRUNC_HPP
10 #include <type_traits>
11 #include <boost/math/tools/is_constant_evaluated.hpp>
12 #include <boost/math/ccmath/abs.hpp>
13 #include <boost/math/ccmath/isinf.hpp>
14 #include <boost/math/ccmath/isnan.hpp>
15 #include <boost/math/ccmath/floor.hpp>
16 #include <boost/math/ccmath/ceil.hpp>
18 namespace boost::math::ccmath {
23 inline constexpr T trunc_impl(T arg) noexcept
25 return (arg > 0) ? boost::math::ccmath::floor(arg) : boost::math::ccmath::ceil(arg);
30 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
31 inline constexpr Real trunc(Real arg) noexcept
33 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
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::trunc_impl(arg);
47 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
48 inline constexpr double trunc(Z arg) noexcept
50 return boost::math::ccmath::trunc(static_cast<double>(arg));
53 inline constexpr float truncf(float arg) noexcept
55 return boost::math::ccmath::trunc(arg);
58 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
59 inline constexpr long double truncl(long double arg) noexcept
61 return boost::math::ccmath::trunc(arg);
67 #endif // BOOST_MATH_CCMATH_TRUNC_HPP