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