1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #ifndef BOOST_MATH_TOOLS_AGM_HPP
- #define BOOST_MATH_TOOLS_AGM_HPP
- #include <limits>
- #include <cmath>
- namespace boost { namespace math { namespace tools {
- template<typename Real>
- Real agm(Real a, Real g)
- {
- using std::sqrt;
-
- if (a < g)
- {
-
- return agm(g, a);
- }
-
- if (a <= 0 || g <= 0) {
- if (a < 0 || g < 0) {
- return std::numeric_limits<Real>::quiet_NaN();
- }
- return Real(0);
- }
-
-
- const Real scale = sqrt(std::numeric_limits<Real>::epsilon())/512;
- while (a-g > scale*g)
- {
- Real anp1 = (a + g)/2;
- g = sqrt(a*g);
- a = anp1;
- }
-
- return (a + g)/2;
- }
- }}}
- #endif
|