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