1// Copyright John Maddock 2007.
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_TRUNC_HPP
7#define BOOST_MATH_TRUNC_HPP
8
9#ifdef _MSC_VER
10#pragma once
11#endif
12
13#include <boost/math/special_functions/math_fwd.hpp>
14#include <boost/math/tools/config.hpp>
15#include <boost/math/policies/error_handling.hpp>
16#include <boost/math/special_functions/fpclassify.hpp>
17#include <boost/type_traits/is_constructible.hpp>
18#include <boost/core/enable_if.hpp>
19
20namespace boost{ namespace math{ namespace detail{
21
22template <class T, class Policy>
23inline typename tools::promote_args<T>::type trunc(const T& v, const Policy& pol, const boost::false_type&)
24{
25 BOOST_MATH_STD_USING
26 typedef typename tools::promote_args<T>::type result_type;
27 if(!(boost::math::isfinite)(v))
28 return policies::raise_rounding_error("boost::math::trunc<%1%>(%1%)", 0, static_cast<result_type>(v), static_cast<result_type>(v), pol);
29 return (v >= 0) ? static_cast<result_type>(floor(v)) : static_cast<result_type>(ceil(v));
30}
31
32template <class T, class Policy>
33inline typename tools::promote_args<T>::type trunc(const T& v, const Policy&, const boost::true_type&)
34{
35 return v;
36}
37
38}
39
40template <class T, class Policy>
41inline typename tools::promote_args<T>::type trunc(const T& v, const Policy& pol)
42{
43 return detail::trunc(v, pol, boost::integral_constant<bool, detail::is_integer_for_rounding<T>::value>());
44}
45template <class T>
46inline typename tools::promote_args<T>::type trunc(const T& v)
47{
48 return trunc(v, policies::policy<>());
49}
50//
51// The following functions will not compile unless T has an
52// implicit conversion to the integer types. For user-defined
53// number types this will likely not be the case. In that case
54// these functions should either be specialized for the UDT in
55// question, or else overloads should be placed in the same
56// namespace as the UDT: these will then be found via argument
57// dependent lookup. See our concept archetypes for examples.
58//
59template <class T, class Policy>
60inline int itrunc(const T& v, const Policy& pol)
61{
62 BOOST_MATH_STD_USING
63 typedef typename tools::promote_args<T>::type result_type;
64 result_type r = boost::math::trunc(v, pol);
65 if((r > (std::numeric_limits<int>::max)()) || (r < (std::numeric_limits<int>::min)()))
66 return static_cast<int>(policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)", 0, static_cast<result_type>(v), 0, pol));
67 return static_cast<int>(r);
68}
69template <class T>
70inline int itrunc(const T& v)
71{
72 return itrunc(v, policies::policy<>());
73}
74
75template <class T, class Policy>
76inline long ltrunc(const T& v, const Policy& pol)
77{
78 BOOST_MATH_STD_USING
79 typedef typename tools::promote_args<T>::type result_type;
80 result_type r = boost::math::trunc(v, pol);
81 if((r > (std::numeric_limits<long>::max)()) || (r < (std::numeric_limits<long>::min)()))
82 return static_cast<long>(policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)", 0, static_cast<result_type>(v), 0L, pol));
83 return static_cast<long>(r);
84}
85template <class T>
86inline long ltrunc(const T& v)
87{
88 return ltrunc(v, policies::policy<>());
89}
90
91#ifdef BOOST_HAS_LONG_LONG
92
93template <class T, class Policy>
94inline boost::long_long_type lltrunc(const T& v, const Policy& pol)
95{
96 BOOST_MATH_STD_USING
97 typedef typename tools::promote_args<T>::type result_type;
98 result_type r = boost::math::trunc(v, pol);
99 if((r > (std::numeric_limits<boost::long_long_type>::max)()) || (r < (std::numeric_limits<boost::long_long_type>::min)()))
100 return static_cast<boost::long_long_type>(policies::raise_rounding_error("boost::math::lltrunc<%1%>(%1%)", 0, v, static_cast<boost::long_long_type>(0), pol));
101 return static_cast<boost::long_long_type>(r);
102}
103template <class T>
104inline boost::long_long_type lltrunc(const T& v)
105{
106 return lltrunc(v, policies::policy<>());
107}
108
109#endif
110
111template <class T, class Policy>
112inline typename boost::enable_if_c<boost::is_constructible<int, T>::value, int>::type
113 iconvert(const T& v, const Policy&)
114{
115 return static_cast<int>(v);
116}
117
118template <class T, class Policy>
119inline typename boost::disable_if_c<boost::is_constructible<int, T>::value, int>::type
120 iconvert(const T& v, const Policy& pol)
121{
122 using boost::math::itrunc;
123 return itrunc(v, pol);
124}
125
126template <class T, class Policy>
127inline typename boost::enable_if_c<boost::is_constructible<long, T>::value, long>::type
128 lconvert(const T& v, const Policy&)
129{
130 return static_cast<long>(v);
131}
132
133template <class T, class Policy>
134inline typename boost::disable_if_c<boost::is_constructible<long, T>::value, long>::type
135 lconvert(const T& v, const Policy& pol)
136{
137 using boost::math::ltrunc;
138 return ltrunc(v, pol);
139}
140
141#ifdef BOOST_HAS_LONG_LONG
142
143template <class T, class Policy>
144inline typename boost::enable_if_c<boost::is_constructible<boost::long_long_type, T>::value, boost::long_long_type>::type
145 llconvertert(const T& v, const Policy&)
146{
147 return static_cast<boost::long_long_type>(v);
148}
149
150template <class T, class Policy>
151inline typename boost::disable_if_c<boost::is_constructible<boost::long_long_type, T>::value, boost::long_long_type>::type
152 llconvertert(const T& v, const Policy& pol)
153{
154 using boost::math::lltrunc;
155 return lltrunc(v, pol);
156}
157
158#endif
159
160}} // namespaces
161
162#endif // BOOST_MATH_TRUNC_HPP
163

source code of include/boost/math/special_functions/trunc.hpp