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_SQRT1PM1
7 #define BOOST_MATH_SQRT1PM1
13 #include <boost/math/special_functions/log1p.hpp>
14 #include <boost/math/special_functions/expm1.hpp>
15 #include <boost/math/special_functions/math_fwd.hpp>
18 // This algorithm computes sqrt(1+x)-1 for small x:
21 namespace boost{ namespace math{
23 template <class T, class Policy>
24 inline typename tools::promote_args<T>::type sqrt1pm1(const T& val, const Policy& pol)
26 typedef typename tools::promote_args<T>::type result_type;
29 if(fabs(result_type(val)) > 0.75)
30 return sqrt(1 + result_type(val)) - 1;
31 return boost::math::expm1(boost::math::log1p(val, pol) / 2, pol);
35 inline typename tools::promote_args<T>::type sqrt1pm1(const T& val)
37 return sqrt1pm1(val, policies::policy<>());
43 #endif // BOOST_MATH_SQRT1PM1