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)
6 #ifndef BOOST_MATH_TOOLS_SIGN_HPP
7 #define BOOST_MATH_TOOLS_SIGN_HPP
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>
17 namespace boost{ namespace math{
21 #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
23 inline int signbit_impl(T x, native_tag const&)
25 return (std::signbit)(x);
30 inline int signbit_impl(T x, generic_tag<true> const&)
36 inline int signbit_impl(T x, generic_tag<false> const&)
42 inline int signbit_impl(T x, ieee_copy_all_bits_tag const&)
44 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
46 BOOST_DEDUCED_TYPENAME traits::bits a;
47 traits::get_bits(x,a);
48 return a & traits::sign ? 1 : 0;
52 inline int signbit_impl(T x, ieee_copy_leading_bits_tag const&)
54 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
56 BOOST_DEDUCED_TYPENAME traits::bits a;
57 traits::get_bits(x,a);
59 return a & traits::sign ? 1 : 0;
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());
72 inline int sign BOOST_NO_MACRO_EXPAND(const T& z)
74 return (z == 0) ? 0 : (boost::math::signbit)(z) ? -1 : 1;
78 inline T copysign BOOST_NO_MACRO_EXPAND(const T& x, const T& y)
81 return fabs(x) * ((boost::math::signbit)(y) ? -1 : 1);
88 #endif // BOOST_MATH_TOOLS_SIGN_HPP