1 // (C) Copyright Nick Thompson 2020.
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)
6 #ifndef BOOST_MATH_TOOLS_AGM_HPP
7 #define BOOST_MATH_TOOLS_AGM_HPP
11 namespace boost { namespace math { namespace tools {
13 template<typename Real>
14 Real agm(Real a, Real g)
20 // Mathematica, mpfr, and mpmath are all symmetric functions:
23 // Use: M(rx, ry) = rM(x,y)
24 if (a <= 0 || g <= 0) {
26 return std::numeric_limits<Real>::quiet_NaN();
31 // The number of correct digits doubles on each iteration.
32 // Divide by 512 for some leeway:
33 const Real scale = sqrt(std::numeric_limits<Real>::epsilon())/512;
36 Real anp1 = (a + g)/2;
41 // Final cleanup iteration recovers down to ~2ULPs: