1 // (C) Copyright John Maddock 2006.
2 // (C) Copyright Johan Rade 2006.
3 // (C) Copyright Paul A. Bristow 2011 (added changesign).
5 // Use, modification and distribution are subject to the
6 // Boost Software License, Version 1.0. (See accompanying file
7 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_MATH_TOOLS_SIGN_HPP
10 #define BOOST_MATH_TOOLS_SIGN_HPP
16 #include <boost/math/tools/config.hpp>
17 #include <boost/math/special_functions/math_fwd.hpp>
18 #include <boost/math/special_functions/detail/fp_traits.hpp>
20 namespace boost{ namespace math{
26 #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
28 inline int signbit_impl(T x, native_tag const&)
30 return (std::signbit)(x);
35 inline int signbit_impl(T x, generic_tag<true> const&)
41 inline int signbit_impl(T x, generic_tag<false> const&)
47 inline int signbit_impl(T x, ieee_copy_all_bits_tag const&)
49 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
51 BOOST_DEDUCED_TYPENAME traits::bits a;
52 traits::get_bits(x,a);
53 return a & traits::sign ? 1 : 0;
57 inline int signbit_impl(T x, ieee_copy_leading_bits_tag const&)
59 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
61 BOOST_DEDUCED_TYPENAME traits::bits a;
62 traits::get_bits(x,a);
64 return a & traits::sign ? 1 : 0;
70 inline T (changesign_impl)(T x, generic_tag<true> const&)
76 inline T (changesign_impl)(T x, generic_tag<false> const&)
83 inline T changesign_impl(T x, ieee_copy_all_bits_tag const&)
85 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::sign_change_type traits;
87 BOOST_DEDUCED_TYPENAME traits::bits a;
88 traits::get_bits(x,a);
90 traits::set_bits(x,a);
95 inline T (changesign_impl)(T x, ieee_copy_leading_bits_tag const&)
97 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::sign_change_type traits;
99 BOOST_DEDUCED_TYPENAME traits::bits a;
100 traits::get_bits(x,a);
102 traits::set_bits(x,a);
107 } // namespace detail
109 template<class T> int (signbit)(T x)
111 typedef typename detail::fp_traits<T>::type traits;
112 typedef typename traits::method method;
113 // typedef typename boost::is_floating_point<T>::type fp_tag;
114 typedef typename tools::promote_args_permissive<T>::type result_type;
115 return detail::signbit_impl(static_cast<result_type>(x), method());
119 inline int sign BOOST_NO_MACRO_EXPAND(const T& z)
121 return (z == 0) ? 0 : (boost::math::signbit)(z) ? -1 : 1;
124 template <class T> typename tools::promote_args_permissive<T>::type (changesign)(const T& x)
125 { //!< \brief return unchanged binary pattern of x, except for change of sign bit.
126 typedef typename detail::fp_traits<T>::sign_change_type traits;
127 typedef typename traits::method method;
128 // typedef typename boost::is_floating_point<T>::type fp_tag;
129 typedef typename tools::promote_args_permissive<T>::type result_type;
131 return detail::changesign_impl(static_cast<result_type>(x), method());
134 template <class T, class U>
135 inline typename tools::promote_args_permissive<T, U>::type
136 copysign BOOST_NO_MACRO_EXPAND(const T& x, const U& y)
139 typedef typename tools::promote_args_permissive<T, U>::type result_type;
140 return (boost::math::signbit)(static_cast<result_type>(x)) != (boost::math::signbit)(static_cast<result_type>(y))
141 ? (boost::math::changesign)(static_cast<result_type>(x)) : static_cast<result_type>(x);
148 #endif // BOOST_MATH_TOOLS_SIGN_HPP