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

  • Thread starter Thread starter Arnoldjavs3
  • Start date Start date
  • Tags Tags
    Algorithm Work
Click For Summary
The algorithm for calculating ln(x) involves repeatedly taking the square root of the input value, which approximates the natural logarithm through a limit process. The multiplier of 1024 arises from taking the square root ten times, effectively relating to the concept of binary representation in computing. While the algorithm achieves accuracy up to two decimal points, it loses precision beyond that, suggesting the need for larger nth roots for improved accuracy. The connection to 1024 is noted, but it is not directly related to the algorithm's effectiveness. Overall, the discussion highlights the algorithm's approximation method and potential areas for enhancement.
Arnoldjavs3
Messages
191
Reaction score
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
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
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
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
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 $$
 
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##.
 
Question: A clock's minute hand has length 4 and its hour hand has length 3. What is the distance between the tips at the moment when it is increasing most rapidly?(Putnam Exam Question) Answer: Making assumption that both the hands moves at constant angular velocities, the answer is ## \sqrt{7} .## But don't you think this assumption is somewhat doubtful and wrong?

Similar threads

  • · Replies 14 ·
Replies
14
Views
2K
Replies
10
Views
3K
Replies
2
Views
1K
  • · Replies 14 ·
Replies
14
Views
2K
Replies
6
Views
2K
Replies
3
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K