Chris@16
|
1 // Copyright John Maddock 2007.
|
Chris@16
|
2 // Use, modification and distribution are subject to the
|
Chris@16
|
3 // Boost Software License, Version 1.0. (See accompanying file
|
Chris@16
|
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 #ifndef BOOST_MATH_TRUNC_HPP
|
Chris@16
|
7 #define BOOST_MATH_TRUNC_HPP
|
Chris@16
|
8
|
Chris@16
|
9 #ifdef _MSC_VER
|
Chris@16
|
10 #pragma once
|
Chris@16
|
11 #endif
|
Chris@16
|
12
|
Chris@101
|
13 #include <boost/math/special_functions/math_fwd.hpp>
|
Chris@16
|
14 #include <boost/math/tools/config.hpp>
|
Chris@16
|
15 #include <boost/math/policies/error_handling.hpp>
|
Chris@16
|
16 #include <boost/math/special_functions/fpclassify.hpp>
|
Chris@16
|
17
|
Chris@101
|
18 namespace boost{ namespace math{ namespace detail{
|
Chris@16
|
19
|
Chris@16
|
20 template <class T, class Policy>
|
Chris@101
|
21 inline typename tools::promote_args<T>::type trunc(const T& v, const Policy& pol, const mpl::false_&)
|
Chris@16
|
22 {
|
Chris@16
|
23 BOOST_MATH_STD_USING
|
Chris@16
|
24 typedef typename tools::promote_args<T>::type result_type;
|
Chris@16
|
25 if(!(boost::math::isfinite)(v))
|
Chris@16
|
26 return policies::raise_rounding_error("boost::math::trunc<%1%>(%1%)", 0, static_cast<result_type>(v), static_cast<result_type>(v), pol);
|
Chris@16
|
27 return (v >= 0) ? static_cast<result_type>(floor(v)) : static_cast<result_type>(ceil(v));
|
Chris@16
|
28 }
|
Chris@101
|
29
|
Chris@101
|
30 template <class T, class Policy>
|
Chris@101
|
31 inline typename tools::promote_args<T>::type trunc(const T& v, const Policy&, const mpl::true_&)
|
Chris@101
|
32 {
|
Chris@101
|
33 return v;
|
Chris@101
|
34 }
|
Chris@101
|
35
|
Chris@101
|
36 }
|
Chris@101
|
37
|
Chris@101
|
38 template <class T, class Policy>
|
Chris@101
|
39 inline typename tools::promote_args<T>::type trunc(const T& v, const Policy& pol)
|
Chris@101
|
40 {
|
Chris@101
|
41 return detail::trunc(v, pol, mpl::bool_<detail::is_integer_for_rounding<T>::value>());
|
Chris@101
|
42 }
|
Chris@16
|
43 template <class T>
|
Chris@16
|
44 inline typename tools::promote_args<T>::type trunc(const T& v)
|
Chris@16
|
45 {
|
Chris@16
|
46 return trunc(v, policies::policy<>());
|
Chris@16
|
47 }
|
Chris@16
|
48 //
|
Chris@16
|
49 // The following functions will not compile unless T has an
|
Chris@16
|
50 // implicit convertion to the integer types. For user-defined
|
Chris@16
|
51 // number types this will likely not be the case. In that case
|
Chris@16
|
52 // these functions should either be specialized for the UDT in
|
Chris@16
|
53 // question, or else overloads should be placed in the same
|
Chris@16
|
54 // namespace as the UDT: these will then be found via argument
|
Chris@16
|
55 // dependent lookup. See our concept archetypes for examples.
|
Chris@16
|
56 //
|
Chris@16
|
57 template <class T, class Policy>
|
Chris@16
|
58 inline int itrunc(const T& v, const Policy& pol)
|
Chris@16
|
59 {
|
Chris@16
|
60 BOOST_MATH_STD_USING
|
Chris@16
|
61 typedef typename tools::promote_args<T>::type result_type;
|
Chris@16
|
62 result_type r = boost::math::trunc(v, pol);
|
Chris@16
|
63 if((r > (std::numeric_limits<int>::max)()) || (r < (std::numeric_limits<int>::min)()))
|
Chris@16
|
64 return static_cast<int>(policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)", 0, static_cast<result_type>(v), 0, pol));
|
Chris@16
|
65 return static_cast<int>(r);
|
Chris@16
|
66 }
|
Chris@16
|
67 template <class T>
|
Chris@16
|
68 inline int itrunc(const T& v)
|
Chris@16
|
69 {
|
Chris@16
|
70 return itrunc(v, policies::policy<>());
|
Chris@16
|
71 }
|
Chris@16
|
72
|
Chris@16
|
73 template <class T, class Policy>
|
Chris@16
|
74 inline long ltrunc(const T& v, const Policy& pol)
|
Chris@16
|
75 {
|
Chris@16
|
76 BOOST_MATH_STD_USING
|
Chris@16
|
77 typedef typename tools::promote_args<T>::type result_type;
|
Chris@16
|
78 result_type r = boost::math::trunc(v, pol);
|
Chris@16
|
79 if((r > (std::numeric_limits<long>::max)()) || (r < (std::numeric_limits<long>::min)()))
|
Chris@16
|
80 return static_cast<long>(policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)", 0, static_cast<result_type>(v), 0L, pol));
|
Chris@16
|
81 return static_cast<long>(r);
|
Chris@16
|
82 }
|
Chris@16
|
83 template <class T>
|
Chris@16
|
84 inline long ltrunc(const T& v)
|
Chris@16
|
85 {
|
Chris@16
|
86 return ltrunc(v, policies::policy<>());
|
Chris@16
|
87 }
|
Chris@16
|
88
|
Chris@16
|
89 #ifdef BOOST_HAS_LONG_LONG
|
Chris@16
|
90
|
Chris@16
|
91 template <class T, class Policy>
|
Chris@16
|
92 inline boost::long_long_type lltrunc(const T& v, const Policy& pol)
|
Chris@16
|
93 {
|
Chris@16
|
94 BOOST_MATH_STD_USING
|
Chris@16
|
95 typedef typename tools::promote_args<T>::type result_type;
|
Chris@16
|
96 result_type r = boost::math::trunc(v, pol);
|
Chris@16
|
97 if((r > (std::numeric_limits<boost::long_long_type>::max)()) || (r < (std::numeric_limits<boost::long_long_type>::min)()))
|
Chris@16
|
98 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));
|
Chris@16
|
99 return static_cast<boost::long_long_type>(r);
|
Chris@16
|
100 }
|
Chris@16
|
101 template <class T>
|
Chris@16
|
102 inline boost::long_long_type lltrunc(const T& v)
|
Chris@16
|
103 {
|
Chris@16
|
104 return lltrunc(v, policies::policy<>());
|
Chris@16
|
105 }
|
Chris@16
|
106
|
Chris@16
|
107 #endif
|
Chris@16
|
108
|
Chris@16
|
109 }} // namespaces
|
Chris@16
|
110
|
Chris@16
|
111 #endif // BOOST_MATH_TRUNC_HPP
|