A question was recently asked about computing log as there is no intrinsic function in MEL. I suggested using a Taylor series. The following is based on a derivation of the Taylor series, hyperbolic tangent function (don't ask). The form to drive this code consists of a variable field, a result field, and a button to calculate the Ln. I made it a little more complicated as I made the input number <=10 by series of divisions by 10 then calculated the result as ln(z)=ln(zdiv10 value)+(num of div10)*ln(10). This allows the value to rapidly converge. The VFE is as follows:
/*Compute Natural Log*/
{ fn Calc_Ln(strZ)
{
//note where z is input number, n10 is number of divisions by 10
//ln(z)=ln(zdiv10)+n10*ln(10)
sumZ=0
strLnZ=""
presumZ=0
strSumZ=""
z=val(strZ)
n=1
n10=0
zdiv10=z
//divide by powers of 10 such than z/10^n<10
while zdiv10>10 do
zdiv10=zdiv10/10
n10=n10+1
endwhile
z=zdiv10
//calculate ln(z) where z now<=10
presumZ=2*((z-1)/(z+1))
sumZ=presumZ+(2/3)*((z-1)/(z+1))^3
while ((sumZ-presumZ)/sumZ)>.000001 do
presumZ=sumZ
n=n+1
sumZ=sumZ+(2/(2*n+1))*((z-1)/(z+1))^(2*n+1)
endwhile
//ln(z)=ln(zdiv10)+n10*ln(10)
sumZ=sumZ+n10*2.30258509
strAns="iterations: "+str(n)+" num div 10: "+str(N10)+" lnz "+str(round(SumZ,6))
document.Ln=strAns
}
}
Computing natural log of 400, 11 interations through the series, result accurate to 6 places, 5.991464. To any mathematicians out there, I apologize in advance if I have butchered the approach, but this seems to be fast and accurate.
I know this is an old post, but you are truly the gift the keeps on giving. I haven't tried this yet, but I think you just saved my life!