Skip to content

Instantly share code, notes, and snippets.

@fmela
Created December 30, 2011 20:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmela/1541295 to your computer and use it in GitHub Desktop.
Save fmela/1541295 to your computer and use it in GitHub Desktop.
A natural logarithm function that uses the arithmetic-geometric mean.
double
agm_log(double x)
{
double a, b, e, e0 = DBL_MAX;
assert(x > 0.0);
for (a = (x + 1.0) * 0.5, b = sqrt(x); (e = fabs(a - b)) < e0; e0 = e) {
a = 0.5 * (a + b);
b = sqrt(a * b);
}
return (x - 1.0) / a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment