# numerics – Wish to compute ln(x) with millions of digits of precision fast as possible

Computing $$ln(10)$$ to 6 million digits of precision on my 2.5 GHz machine running Mathematica 12.1 takes about 23 seconds using the methods below. Wish to compute $$ln(x)$$ with much higher precision. Is this the fastest I can compute $$ln(10)$$ with 6 million digits on my machine?

1. Use the built-in Log(x) function,
2. Invert the exponential expression $$e^y=x$$ which results in a Newton iteration of the form:
$$y_{n+1}=y_n+2frac{x-e^{y_n}}{x+e^{y_n}}$$
3. Use the arithmetic-geometric expression:
$$log(x)approx frac{pi}{2M(1,4/s)}-mlog(2);quad s=x 2^m>2^{p/2}$$
for $$p$$ bits of precision.

Unfortunately, these all take about the same amount of time. The code below is for 6 million digits and the best time is about 23 seconds:

``````totalD = 6000000;
(*
set up A-G mean parameters
*)
myPi = SetPrecision(Pi, totalD);
myLog2 = SetPrecision(Log(2), totalD);
pFun(x_) := Ceiling(x Log(10)/Log(2));
mFun(p_) := Ceiling(1/Log(2) (p/2 Log(2) - Log(10)));
sFun(m_) := 10 2^m;
(*
check built-in Log:
*)
AbsoluteTiming(
actVal = SetPrecision(Log(10), totalD);
)
(*
check arithmetic-geometric mean approach
*)
AbsoluteTiming(
pVal = pFun(totalD);
mVal = mFun(pVal);
sVal = sFun(mVal);
denom = SetPrecision(ArithmeticGeometricMean(1, 4/sVal), totalD);
myVal = SetPrecision(myPi/(2 denom) - mVal myLog2, totalD);
)
(*
check just the quotient expression of the inverted exp expression
*)
y0 = 23/10;
AbsoluteTiming(
SetPrecision((
10 - Exp(y0))/(10 + Exp(y0)), totalD);
)

{23.911, Null}

{22.7023, Null}

{50.6573, Null}
``````

Posted on Categories Articles