C/C++ Computing ln(x) with C++ Iterations

  • Thread starter Thread starter AndersHermansson
  • Start date Start date
  • Tags Tags
    C++ Computing
Click For Summary
The discussion centers on a C++ algorithm for calculating the natural logarithm of a number, ln(x). The algorithm uses a loop to sum fractions from 1/10^n to 1/x*10^(n+1), where n is a variable that improves the approximation with larger values. The method effectively demonstrates the relationship between integration and the natural logarithm, specifically highlighting that the integral of 1/x is ln(x). The conversation concludes with acknowledgment of the discovery.
AndersHermansson
Messages
61
Reaction score
0
I was fiddling around with C++ and iterations and I found an algorithm for computing ln(x).


n = any number, the higher, the better the approximation
ln_x = 0;
divisor = 10^n;

while ( divisor <= x*10^n ) // Where x is the ln(x) you want to find.
{

ln_x += 1 / divisor;
divisor++;

}

What it does is add all integer fractions from 1/10^n to 1/x*10^(n+1).
 
Technology news on Phys.org
You've discovered the fact that [inte](1/x) dx is ln(x). Congratulations!

- Warren
 
Thanks :)
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
9
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
1
Views
2K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K