1///////////////////////////////////////////////////////////////////////////////
2// Copyright 2022 Matt Borland. Distributed under the Boost
3// 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_MP_DETAIL_FUNCTIONS_TRUNC_HPP
7#define BOOST_MP_DETAIL_FUNCTIONS_TRUNC_HPP
8
9#include <cmath>
10#include <limits>
11#include <stdexcept>
12#include <boost/multiprecision/detail/standalone_config.hpp>
13#include <boost/multiprecision/detail/no_exceptions_support.hpp>
14
15#ifdef BOOST_MP_MATH_AVAILABLE
16#include <boost/math/special_functions/trunc.hpp>
17#endif
18
19namespace boost { namespace multiprecision { namespace detail {
20
21namespace impl {
22
23template <typename T>
24inline T trunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
25{
26 if (arg > 0)
27 {
28 using std::floor;
29
30 return floor(arg);
31 }
32
33 using std::ceil;
34
35 return ceil(arg);}
36} // namespace impl
37
38#ifdef BOOST_MP_MATH_AVAILABLE
39
40template <typename T>
41inline long long lltrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
42{
43 return boost::math::lltrunc(arg);
44}
45
46template <typename T>
47inline int itrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
48{
49 return boost::math::itrunc(arg);
50}
51
52#else
53
54template <typename T>
55inline long long lltrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
56{
57 T t = boost::multiprecision::detail::impl::trunc(arg);
58 if (t > LLONG_MAX)
59 {
60 BOOST_MP_THROW_EXCEPTION(std::domain_error("arg cannot be converted into a long long"));
61 }
62
63 return static_cast<long long>(t);
64}
65
66template <typename T>
67inline int itrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
68{
69 T t = boost::multiprecision::detail::impl::trunc(arg);
70 if (t > static_cast<T>(INT_MAX))
71 {
72 BOOST_MP_THROW_EXCEPTION(std::domain_error("arg cannot be converted into an int"));
73 }
74
75 return static_cast<int>(t);
76}
77
78#endif
79
80}}} // Namespaces
81
82#endif // BOOST_MP_DETAIL_FUNCTIONS_TRUNC_HPP
83

source code of boost/libs/multiprecision/include/boost/multiprecision/detail/functions/trunc.hpp