| 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_SQRT1PM1 |
| 7 | #define BOOST_MATH_SQRT1PM1 |
| 8 | |
| 9 | #ifdef _MSC_VER |
| 10 | #pragma once |
| 11 | #endif |
| 12 | |
| 13 | #include <boost/math/special_functions/math_fwd.hpp> |
| 14 | #include <boost/math/special_functions/log1p.hpp> |
| 15 | #include <boost/math/special_functions/expm1.hpp> |
| 16 | |
| 17 | // |
| 18 | // This algorithm computes sqrt(1+x)-1 for small x: |
| 19 | // |
| 20 | |
| 21 | namespace boost{ namespace math{ |
| 22 | |
| 23 | template <class T, class Policy> |
| 24 | inline typename tools::promote_args<T>::type sqrt1pm1(const T& val, const Policy& pol) |
| 25 | { |
| 26 | typedef typename tools::promote_args<T>::type result_type; |
| 27 | BOOST_MATH_STD_USING |
| 28 | |
| 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); |
| 32 | } |
| 33 | |
| 34 | template <class T> |
| 35 | inline typename tools::promote_args<T>::type sqrt1pm1(const T& val) |
| 36 | { |
| 37 | return sqrt1pm1(val, policies::policy<>()); |
| 38 | } |
| 39 | |
| 40 | } // namespace math |
| 41 | } // namespace boost |
| 42 | |
| 43 | #endif // BOOST_MATH_SQRT1PM1 |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 |
