sqrt1pm1.hpp 1.1 KB

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