Why does this algorithm work for calculating ln(x)?

In summary: So if we want the error to be at most ##10^{-2}##, i.e. 0.01, then we should choose ##n## so that ##\frac{x^{1/n}}{2n} \ln(x)^2 < 10^{-2}##, or equivalently, ##\frac{x^{1/n}}{n} < 20##.
  • #1
Arnoldjavs3
191
3

Homework Statement


I found this algorithm online for computing ln(x). I use the babylonians method for computing square root if it is relevant.

Code:
fun naturalLog(desired: Double): Double {
    var naturalLog = desired // desired = x
    for(number in 0..9) {
        naturalLog = squareRootx(naturalLog) // squareroot 10 times
    }
    naturalLog -= 1
    naturalLog *= 1024
    return naturalLog
}
(This code is in Kotlin).

Homework Equations

The Attempt at a Solution


I just really don't understand. I've verified that it has accuracy of up to 2 decimal points.

When I compute ##ln(9)## I get ##2.19958358##, so after the 2nd decimal point it loses accuracy. It might be relevant to my squareroot algorithm, but I doubt it. What is this 1024 number? Is there some coincidence between a KiB = 1024 bytes? Googling around for ln(x) algorithms doesn't help.
 
  • Like
Likes I like Serena
Physics news on Phys.org
  • #2
As best as I can figure it is roughly approximating the following limits.

## \lim_{n \to \infty} (x^{\frac{1}{n}} - 1) * n = \lim_{n \to \infty} \frac{x^{\frac{1}{n}} - 1}{\frac{1}{n}} = \lim_{n \to \infty} \frac{x^{\frac{1}{n}} * \ln{x} * \frac{-1}{n^2}}{\frac{-1}{n^2}} = \ln{x} ##

It mis-matches the root taking with the multiplier, but otherwise seems pretty close. Hard to determine at first because use of 1024 as the multiplier is suggestive of clever computing tricks such as register shifts and whatnot, but none of that is in play here that I can see.

Quick edit: If you want to improve accuracy, approximate larger nth roots, and have the multiplier at the end be based on the n.
 
  • Like
Likes Arnoldjavs3 and I like Serena
  • #3
I concur.
Taking the square root 10 times means that we have n=210=1024, which is where the multiplier comes from.
The KiB finds its origin in the fact that this number is so close to 1000, but that won't be related to the algorithm. We can pick a higher n after all.
 
  • Like
Likes Zerox5f3759df and Arnoldjavs3
  • #4
Good catch on where 1024 is coming from. I thought I saw the loop going from 1-9, not 0-9.
 
  • Like
Likes I like Serena
  • #5
Zerox5f3759df said:
As best as I can figure it is roughly approximating the following limits.

## \lim_{n \to \infty} (x^{\frac{1}{n}} - 1) * n = \lim_{n \to \infty} \frac{x^{\frac{1}{n}} - 1}{\frac{1}{n}} = \lim_{n \to \infty} \frac{x^{\frac{1}{n}} * \ln{x} * \frac{-1}{n^2}}{\frac{-1}{n^2}} = \ln{x} ##

It mis-matches the root taking with the multiplier, but otherwise seems pretty close. Hard to determine at first because use of 1024 as the multiplier is suggestive of clever computing tricks such as register shifts and whatnot, but none of that is in play here that I can see.

Quick edit: If you want to improve accuracy, approximate larger nth roots, and have the multiplier at the end be based on the n.
In fact, for finite ##n## we have
$$n \left( x^{1/n} - 1 \right) = \ln(x) + \frac{1}{2n} \ln(x)^2 + \frac{1}{6 n^2} \ln(x)^3 + \cdots + \frac{1}{k! \, n^k} \ln(x)^{k+1} + \cdots $$
 
  • #6
Ray Vickson said:
In fact, for finite ##n## we have
$$n \left( x^{1/n} - 1 \right) = \ln(x) + \frac{1}{2n} \ln(x)^2 + \frac{1}{6 n^2} \ln(x)^3 + \cdots + \frac{1}{k! \, n^k} \ln(x)^{k+1} + \cdots $$
More specifically, we can calculate the error:
$$n \left( x^{1/n} - 1 \right) = \ln(x) + \frac{\xi^{1/n}}{2n} \ln(x)^2$$
where ##\xi## is between 1 and x.
That is, if ##x\ge 1## then the error is at most ##\frac{x^{1/n}}{2n} \ln(x)^2##.
 

1. Why is the natural logarithm important?

The natural logarithm, ln(x), is an important mathematical function used in various scientific and mathematical applications. It is especially useful in representing exponential growth and decay, and is commonly used in fields such as physics, chemistry, and economics.

2. How does the algorithm for calculating ln(x) work?

The algorithm for calculating ln(x) is based on the concept of the inverse of the exponential function. It involves using a series of approximations and iterations to find the value of ln(x) for a given input x. This algorithm is derived from the Taylor series expansion of the natural logarithm function.

3. Why is the Taylor series expansion used in the algorithm for ln(x)?

The Taylor series expansion is used in the algorithm for ln(x) because it allows for the approximation of the natural logarithm function at a specific input x. This is achieved by using a polynomial with an infinite number of terms to approximate the function at that point, resulting in a more accurate value of ln(x).

4. Are there any limitations to this algorithm for calculating ln(x)?

While the algorithm for calculating ln(x) is generally accurate, it does have its limitations. It may not produce accurate results for extremely large or small values of x, and can also be affected by rounding errors and machine precision. Additionally, the algorithm may not be suitable for complex or imaginary numbers.

5. How can this algorithm be improved or optimized?

There are various ways in which the algorithm for calculating ln(x) can be improved or optimized. One approach is to use more efficient methods for computing the exponential function, such as the CORDIC algorithm. Additionally, using more accurate approximations and increasing the number of iterations can also improve the overall accuracy of the algorithm.

Similar threads

  • Calculus and Beyond Homework Help
Replies
14
Views
2K
  • Calculus and Beyond Homework Help
Replies
10
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
646
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
5K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
997
Replies
1
Views
1K
Replies
1
Views
2K
Back
Top