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@16
|
13 #include <boost/math/tools/config.hpp>
|
Chris@16
|
14 #include <boost/math/policies/error_handling.hpp>
|
Chris@16
|
15 #include <boost/math/special_functions/fpclassify.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 namespace boost{ namespace math{
|
Chris@16
|
18
|
Chris@16
|
19 template <class T, class Policy>
|
Chris@16
|
20 inline typename tools::promote_args<T>::type trunc(const T& v, const Policy& pol)
|
Chris@16
|
21 {
|
Chris@16
|
22 BOOST_MATH_STD_USING
|
Chris@16
|
23 typedef typename tools::promote_args<T>::type result_type;
|
Chris@16
|
24 if(!(boost::math::isfinite)(v))
|
Chris@16
|
25 return policies::raise_rounding_error("boost::math::trunc<%1%>(%1%)", 0, static_cast<result_type>(v), static_cast<result_type>(v), pol);
|
Chris@16
|
26 return (v >= 0) ? static_cast<result_type>(floor(v)) : static_cast<result_type>(ceil(v));
|
Chris@16
|
27 }
|
Chris@16
|
28 template <class T>
|
Chris@16
|
29 inline typename tools::promote_args<T>::type trunc(const T& v)
|
Chris@16
|
30 {
|
Chris@16
|
31 return trunc(v, policies::policy<>());
|
Chris@16
|
32 }
|
Chris@16
|
33 //
|
Chris@16
|
34 // The following functions will not compile unless T has an
|
Chris@16
|
35 // implicit convertion to the integer types. For user-defined
|
Chris@16
|
36 // number types this will likely not be the case. In that case
|
Chris@16
|
37 // these functions should either be specialized for the UDT in
|
Chris@16
|
38 // question, or else overloads should be placed in the same
|
Chris@16
|
39 // namespace as the UDT: these will then be found via argument
|
Chris@16
|
40 // dependent lookup. See our concept archetypes for examples.
|
Chris@16
|
41 //
|
Chris@16
|
42 template <class T, class Policy>
|
Chris@16
|
43 inline int itrunc(const T& v, const Policy& pol)
|
Chris@16
|
44 {
|
Chris@16
|
45 BOOST_MATH_STD_USING
|
Chris@16
|
46 typedef typename tools::promote_args<T>::type result_type;
|
Chris@16
|
47 result_type r = boost::math::trunc(v, pol);
|
Chris@16
|
48 if((r > (std::numeric_limits<int>::max)()) || (r < (std::numeric_limits<int>::min)()))
|
Chris@16
|
49 return static_cast<int>(policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)", 0, static_cast<result_type>(v), 0, pol));
|
Chris@16
|
50 return static_cast<int>(r);
|
Chris@16
|
51 }
|
Chris@16
|
52 template <class T>
|
Chris@16
|
53 inline int itrunc(const T& v)
|
Chris@16
|
54 {
|
Chris@16
|
55 return itrunc(v, policies::policy<>());
|
Chris@16
|
56 }
|
Chris@16
|
57
|
Chris@16
|
58 template <class T, class Policy>
|
Chris@16
|
59 inline long ltrunc(const T& v, const Policy& pol)
|
Chris@16
|
60 {
|
Chris@16
|
61 BOOST_MATH_STD_USING
|
Chris@16
|
62 typedef typename tools::promote_args<T>::type result_type;
|
Chris@16
|
63 result_type r = boost::math::trunc(v, pol);
|
Chris@16
|
64 if((r > (std::numeric_limits<long>::max)()) || (r < (std::numeric_limits<long>::min)()))
|
Chris@16
|
65 return static_cast<long>(policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)", 0, static_cast<result_type>(v), 0L, pol));
|
Chris@16
|
66 return static_cast<long>(r);
|
Chris@16
|
67 }
|
Chris@16
|
68 template <class T>
|
Chris@16
|
69 inline long ltrunc(const T& v)
|
Chris@16
|
70 {
|
Chris@16
|
71 return ltrunc(v, policies::policy<>());
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 #ifdef BOOST_HAS_LONG_LONG
|
Chris@16
|
75
|
Chris@16
|
76 template <class T, class Policy>
|
Chris@16
|
77 inline boost::long_long_type lltrunc(const T& v, const Policy& pol)
|
Chris@16
|
78 {
|
Chris@16
|
79 BOOST_MATH_STD_USING
|
Chris@16
|
80 typedef typename tools::promote_args<T>::type result_type;
|
Chris@16
|
81 result_type r = boost::math::trunc(v, pol);
|
Chris@16
|
82 if((r > (std::numeric_limits<boost::long_long_type>::max)()) || (r < (std::numeric_limits<boost::long_long_type>::min)()))
|
Chris@16
|
83 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
|
84 return static_cast<boost::long_long_type>(r);
|
Chris@16
|
85 }
|
Chris@16
|
86 template <class T>
|
Chris@16
|
87 inline boost::long_long_type lltrunc(const T& v)
|
Chris@16
|
88 {
|
Chris@16
|
89 return lltrunc(v, policies::policy<>());
|
Chris@16
|
90 }
|
Chris@16
|
91
|
Chris@16
|
92 #endif
|
Chris@16
|
93
|
Chris@16
|
94 }} // namespaces
|
Chris@16
|
95
|
Chris@16
|
96 #endif // BOOST_MATH_TRUNC_HPP
|