File indexing completed on 2025-01-18 09:40:39
0001
0002
0003
0004
0005
0006 #ifndef BOOST_MATH_TOOLS_AGM_HPP
0007 #define BOOST_MATH_TOOLS_AGM_HPP
0008 #include <limits>
0009 #include <cmath>
0010
0011 namespace boost { namespace math { namespace tools {
0012
0013 template<typename Real>
0014 Real agm(Real a, Real g)
0015 {
0016 using std::sqrt;
0017
0018 if (a < g)
0019 {
0020
0021 return agm(g, a);
0022 }
0023
0024 if (a <= 0 || g <= 0) {
0025 if (a < 0 || g < 0) {
0026 return std::numeric_limits<Real>::quiet_NaN();
0027 }
0028 return Real(0);
0029 }
0030
0031
0032
0033 const Real scale = sqrt(std::numeric_limits<Real>::epsilon())/512;
0034 while (a-g > scale*g)
0035 {
0036 Real anp1 = (a + g)/2;
0037 g = sqrt(a*g);
0038 a = anp1;
0039 }
0040
0041
0042 return (a + g)/2;
0043 }
0044
0045
0046 }}}
0047 #endif