71 lines · plain
1// (C) Copyright Matt Borland 2021.2// Use, modification and distribution are subject to the3// Boost Software License, Version 1.0. (See accompanying file4// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)5 6#ifndef BOOST_MATH_CCMATH_TRUNC_HPP7#define BOOST_MATH_CCMATH_TRUNC_HPP8 9#include <boost/math/ccmath/detail/config.hpp>10 11#ifdef BOOST_MATH_NO_CCMATH12#error "The header <boost/math/trunc.hpp> can only be used in C++17 and later."13#endif14 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 21namespace boost::math::ccmath {22 23namespace detail {24 25template <typename T>26inline constexpr T trunc_impl(T arg) noexcept27{28 return (arg > 0) ? boost::math::ccmath::floor(arg) : boost::math::ccmath::ceil(arg);29}30 31} // Namespace detail32 33template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>34inline constexpr Real trunc(Real arg) noexcept35{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 else44 {45 using std::trunc;46 return trunc(arg);47 }48}49 50template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>51inline constexpr double trunc(Z arg) noexcept52{53 return boost::math::ccmath::trunc(static_cast<double>(arg));54}55 56inline constexpr float truncf(float arg) noexcept57{58 return boost::math::ccmath::trunc(arg);59}60 61#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS62inline constexpr long double truncl(long double arg) noexcept63{64 return boost::math::ccmath::trunc(arg);65}66#endif67 68} // Namespaces69 70#endif // BOOST_MATH_CCMATH_TRUNC_HPP71