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