]> git.donarmstrong.com Git - rsem.git/blob - boost/math/special_functions/sign.hpp
88db77729a8d1485f740bf982f334f8e8103cb3d
[rsem.git] / boost / math / special_functions / sign.hpp
1 //  (C) Copyright John Maddock 2006.
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_TOOLS_SIGN_HPP
7 #define BOOST_MATH_TOOLS_SIGN_HPP
8
9 #ifdef _MSC_VER
10 #pragma once
11 #endif
12
13 #include <boost/math/tools/config.hpp>
14 #include <boost/math/special_functions/math_fwd.hpp>
15 #include <boost/math/special_functions/detail/fp_traits.hpp>
16
17 namespace boost{ namespace math{ 
18
19 namespace detail {
20
21 #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
22     template<class T> 
23     inline int signbit_impl(T x, native_tag const&)
24     {
25         return (std::signbit)(x);
26     }
27 #endif
28
29     template<class T> 
30     inline int signbit_impl(T x, generic_tag<true> const&)
31     {
32         return x < 0;
33     }
34
35     template<class T> 
36     inline int signbit_impl(T x, generic_tag<false> const&)
37     {
38         return x < 0;
39     }
40
41     template<class T> 
42     inline int signbit_impl(T x, ieee_copy_all_bits_tag const&)
43     {
44         typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
45
46         BOOST_DEDUCED_TYPENAME traits::bits a;
47         traits::get_bits(x,a);
48         return a & traits::sign ? 1 : 0;
49     }
50
51     template<class T> 
52     inline int signbit_impl(T x, ieee_copy_leading_bits_tag const&)
53     {
54         typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
55
56         BOOST_DEDUCED_TYPENAME traits::bits a;
57         traits::get_bits(x,a);
58
59         return a & traits::sign ? 1 : 0;
60     }
61 }   // namespace detail
62
63 template<class T> int (signbit)(T x)
64 { //!< \brief return true if floating-point type t is NaN (Not A Number).
65    typedef typename detail::fp_traits<T>::type traits;
66    typedef typename traits::method method;
67    typedef typename boost::is_floating_point<T>::type fp_tag;
68    return detail::signbit_impl(x, method());
69 }
70
71 template <class T>
72 inline int sign BOOST_NO_MACRO_EXPAND(const T& z)
73 {
74    return (z == 0) ? 0 : (boost::math::signbit)(z) ? -1 : 1;
75 }
76
77 template <class T>
78 inline T copysign BOOST_NO_MACRO_EXPAND(const T& x, const T& y)
79 {
80    BOOST_MATH_STD_USING
81       return fabs(x) * ((boost::math::signbit)(y) ? -1 : 1);
82 }
83
84 } // namespace math
85 } // namespace boost
86
87
88 #endif // BOOST_MATH_TOOLS_SIGN_HPP
89
90