Computing ln(x) with C++ Iterations

  • Context: C/C++ 
  • Thread starter Thread starter AndersHermansson
  • Start date Start date
  • Tags Tags
    C++ Computing
Click For Summary
SUMMARY

The forum discussion presents an algorithm for computing the natural logarithm ln(x) using C++ iterations. The algorithm involves setting a divisor to 10 raised to the power of n, where n is a user-defined number that enhances the approximation accuracy. The loop iterates, adding the reciprocal of the divisor to ln_x until the divisor exceeds x multiplied by 10 raised to the power of n. This method effectively approximates ln(x) by summing integer fractions from 1/10^n to 1/x*10^(n+1).

PREREQUISITES
  • C++ programming fundamentals
  • Understanding of loops and iterations in programming
  • Basic knowledge of mathematical functions, specifically logarithms
  • Familiarity with numerical approximation techniques
NEXT STEPS
  • Explore C++ numerical methods for improved logarithmic calculations
  • Learn about convergence and error analysis in iterative algorithms
  • Investigate alternative algorithms for computing logarithms, such as Taylor series
  • Study performance optimization techniques in C++ for iterative processes
USEFUL FOR

C++ developers, mathematicians, and anyone interested in numerical methods for logarithmic computations.

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 :)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · 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
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 23 ·
Replies
23
Views
3K