RE: logs and antilogs
- Posted by Andy Serpa <ac at onehorseshy.com> Oct 26, 2002
- 482 views
Kat wrote: > I have been looking all over the net for a formula to calculate logs and > > antilogs for any given number, but turned up zip. Can anyone suggest a > formula? > > Kat > > Found this on web, and translated to Euphoria: -- natural logarithm function ln(atom x) atom precision,l,m,n,k,val if x <= 0.0 then -- ERROR: can't take a log of zero or negative -- return some appropriate error here (like a sequence) -- or kill program end if precision = 1e-15 m = 1.0 n = 0.0 while (x/m >= 10.0) do m *= 10.0 n += 1.0 end while if x < 1.0 then n = 0.0 while (x/m <= 0.1) do m *= 0.1 n -= 1.0 end while end if x /= m x = (x-1)/(x+1) l = x val = x k = 3.0 l *= x*x m = l/k val += m if m < 0.0 then m = -m end if while (m >= precision) do k += 2.0 l *= x*x m = l/k val += m if m < 0.0 then m = -m end if end while val = (val * 2) + (n*2.30258509298749) return val -- / 2.30258509298749 (uncomment to get log10) end function -- returns base-based logarithm of x function logbase(atom x, atom base) return ln(x)/ln(base) end function