how is log(x) calculated?


mm2ps
01-07-2004, 05:27 AM
Hi,

Could someone give me some suggestions of numerical methods suitable for calculating ln(x), for small x (below 1.0). I want to be able to stop when the result is 'good' enough.

The built in C libarary function log(x) is too acurate, and slow, for my requirements (how does it calculate it?).

Some thoughts I have had:-

1. ln(x)=int_{1}^{x} frac{1}{t} dt

2. A ploynomial approximation of ln(x)

NB. The usual series for ln(x+1) is not suitable.

Thanks for any help.

Douglas

jaje
01-09-2004, 08:38 AM
First, a comment. You said:

...I want to be able to stop when the result is 'good' enough.

By good enough, do you mean that you want a result to machine accuracy, or do you want it to only a small number of decimal places?

Here are a few strategies you can try:

1. Why not try a (m,n) Padé approximant? You can use the pade routine to generate the coefficients for a good rational approximation.

2. Richard P. Brent discusses the Salamin method for computing the logarithm. You can find a short description in his paper.
(download it from http://web.comlab.ox.ac.uk/oucl/work/richard.brent/pd/rpb028.pdf)

3. Or, you can check this site out:

http://www.dspguru.com/comp.dsp/tricks/alg/quicklog.htm

(quite clever, IMHO.)

Jan M. (^_^)

P.S. How is the logarithm usually calculated? Most computers calculate the natural logarithm by power series economization.
§ 5.11 of Numerical Recipes discusses this method.

mm2ps
01-09-2004, 09:17 AM
Jan,

Thankyou for your help.

When I said 'good enough', I did mean to a specific number (small, I think, but what is a small number of decimal places? ) of decimall places. However, though not relevent directly to my investigation, could someone tell me how machine accuracy is defined.

Douglas

jaje
01-10-2004, 03:56 AM
Informally, I consider "small" in the context of significant figures/decimal places as being less than half the number of digits in the mantissa.

Now to answer your question. Machine accuracy, traditionally denoted by the Greek letter å, is formally defined as the smallest number in your machine that when added to 1.0, gives a number that is not equal to 1.0 . If you're interested in determining this parameter for your computer, you can use the machar routine from chapter 20.

When one says "computed to machine accuracy", however, the usual meaning attached to this phrase is that the number computed is accurate to the number of digits in the mantissa. I was using this sense. But I guess the more precise way to say it is "computed to machine precision" (pardon the pun).

Hope this clears things up.

Jan M. (~_^)