hypot.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // (C) Copyright John Maddock 2005-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_HYPOT_INCLUDED
  6. #define BOOST_MATH_HYPOT_INCLUDED
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/tools/config.hpp>
  11. #include <boost/math/tools/precision.hpp>
  12. #include <boost/math/tools/numeric_limits.hpp>
  13. #include <boost/math/tools/type_traits.hpp>
  14. #include <boost/math/policies/error_handling.hpp>
  15. #include <boost/math/special_functions/math_fwd.hpp>
  16. #include <boost/math/tools/utility.hpp>
  17. #include <boost/math/tools/numeric_limits.hpp>
  18. namespace boost{ namespace math{ namespace detail{
  19. template <class T, class Policy>
  20. BOOST_MATH_GPU_ENABLED T hypot_imp(T x, T y, const Policy& pol)
  21. {
  22. //
  23. // Normalize x and y, so that both are positive and x >= y:
  24. //
  25. BOOST_MATH_STD_USING
  26. x = fabs(x);
  27. y = fabs(y);
  28. #ifdef _MSC_VER
  29. #pragma warning(push)
  30. #pragma warning(disable: 4127)
  31. #endif
  32. // special case, see C99 Annex F:
  33. if(boost::math::numeric_limits<T>::has_infinity
  34. && ((x == boost::math::numeric_limits<T>::infinity())
  35. || (y == boost::math::numeric_limits<T>::infinity())))
  36. return policies::raise_overflow_error<T>("boost::math::hypot<%1%>(%1%,%1%)", nullptr, pol);
  37. #ifdef _MSC_VER
  38. #pragma warning(pop)
  39. #endif
  40. if(y > x)
  41. BOOST_MATH_GPU_SAFE_SWAP(x, y);
  42. if(x * tools::epsilon<T>() >= y)
  43. return x;
  44. T rat = y / x;
  45. return x * sqrt(1 + rat*rat);
  46. } // template <class T> T hypot(T x, T y)
  47. template <class T, class Policy>
  48. BOOST_MATH_GPU_ENABLED T hypot_imp(T x, T y, T z, const Policy& pol)
  49. {
  50. BOOST_MATH_STD_USING
  51. x = fabs(x);
  52. y = fabs(y);
  53. z = fabs(z);
  54. #ifdef _MSC_VER
  55. #pragma warning(push)
  56. #pragma warning(disable: 4127)
  57. #endif
  58. // special case, see C99 Annex F:
  59. BOOST_MATH_IF_CONSTEXPR (boost::math::numeric_limits<T>::has_infinity)
  60. {
  61. if(((x == boost::math::numeric_limits<T>::infinity())
  62. || (y == boost::math::numeric_limits<T>::infinity())
  63. || (z == boost::math::numeric_limits<T>::infinity())))
  64. return policies::raise_overflow_error<T>("boost::math::hypot<%1%>(%1%,%1%,%1%)", nullptr, pol);
  65. }
  66. #ifdef _MSC_VER
  67. #pragma warning(pop)
  68. #endif
  69. const T a {(max)((max)(x, y), z)};
  70. if (a == T(0))
  71. {
  72. return a;
  73. }
  74. const T x_div_a {x / a};
  75. const T y_div_a {y / a};
  76. const T z_div_a {z / a};
  77. return a * sqrt(x_div_a * x_div_a
  78. + y_div_a * y_div_a
  79. + z_div_a * z_div_a);
  80. }
  81. }
  82. template <class T1, class T2>
  83. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type
  84. hypot(T1 x, T2 y)
  85. {
  86. typedef typename tools::promote_args<T1, T2>::type result_type;
  87. return detail::hypot_imp(
  88. static_cast<result_type>(x), static_cast<result_type>(y), policies::policy<>());
  89. }
  90. template <class T1, class T2, class Policy, boost::math::enable_if_t<policies::is_policy_v<Policy>, bool>>
  91. BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type
  92. hypot(T1 x, T2 y, const Policy& pol)
  93. {
  94. typedef typename tools::promote_args<T1, T2>::type result_type;
  95. return detail::hypot_imp(
  96. static_cast<result_type>(x), static_cast<result_type>(y), pol);
  97. }
  98. template <class T1, class T2, class T3, boost::math::enable_if_t<!policies::is_policy_v<T3>, bool>>
  99. BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T1, T2, T3>
  100. hypot(T1 x, T2 y, T3 z)
  101. {
  102. using result_type = tools::promote_args_t<T1, T2, T3>;
  103. return detail::hypot_imp(static_cast<result_type>(x),
  104. static_cast<result_type>(y),
  105. static_cast<result_type>(z),
  106. policies::policy<>());
  107. }
  108. template <class T1, class T2, class T3, class Policy>
  109. BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T1, T2, T3>
  110. hypot(T1 x, T2 y, T3 z, const Policy& pol)
  111. {
  112. using result_type = tools::promote_args_t<T1, T2, T3>;
  113. return detail::hypot_imp(static_cast<result_type>(x),
  114. static_cast<result_type>(y),
  115. static_cast<result_type>(z),
  116. pol);
  117. }
  118. } // namespace math
  119. } // namespace boost
  120. #endif // BOOST_MATH_HYPOT_INCLUDED