sqrt1pm1.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. #ifndef BOOST_MATH_SQRT1PM1
  6. #define BOOST_MATH_SQRT1PM1
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/tools/config.hpp>
  11. #include <boost/math/special_functions/math_fwd.hpp>
  12. #include <boost/math/special_functions/log1p.hpp>
  13. #include <boost/math/special_functions/expm1.hpp>
  14. //
  15. // This algorithm computes sqrt(1+x)-1 for small x:
  16. //
  17. namespace boost{ namespace math{
  18. template <class T, class Policy>
  19. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type sqrt1pm1(const T& val, const Policy& pol)
  20. {
  21. typedef typename tools::promote_args<T>::type result_type;
  22. BOOST_MATH_STD_USING
  23. if(fabs(result_type(val)) > result_type(0.75))
  24. return sqrt(1 + result_type(val)) - 1;
  25. return boost::math::expm1(boost::math::log1p(val, pol) / 2, pol);
  26. }
  27. template <class T>
  28. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type sqrt1pm1(const T& val)
  29. {
  30. return sqrt1pm1(val, policies::policy<>());
  31. }
  32. } // namespace math
  33. } // namespace boost
  34. #endif // BOOST_MATH_SQRT1PM1