最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to calculate for the ln of a given number? - Stack Overflow

programmeradmin0浏览0评论

Good day all.

The google scientific calculator lets you calculate the ln of a given number.

examples:

ln(1) = 0

ln(2) = 0.69314718056

I've been trying to figure out the equation used by it to arrive at the answer. Any leads would be wele.

I'm bad at math as you can tell. :(

Good day all.

The google scientific calculator lets you calculate the ln of a given number.

examples:

ln(1) = 0

ln(2) = 0.69314718056

I've been trying to figure out the equation used by it to arrive at the answer. Any leads would be wele.

I'm bad at math as you can tell. :(

Share Improve this question edited Feb 14, 2014 at 2:47 Josh Engelsma 2,64615 silver badges17 bronze badges asked Feb 14, 2014 at 2:45 KamaKama 1931 gold badge3 silver badges13 bronze badges 2
  • 1 That's a "logarithm", which is the power to which you have to raise some number (called the base) to get the original number. The base-2 logarithm of 8 is 3, because 2^3 = 8. "ln" refers to the "natural" logarithms, whose base is Euler's number e - an irrational number that shows up in all kinds of exponential growth in nature. Fractional logarithms are defined for numbers that aren't exact powers of the base, but there's no simple equation for calculating them; you can only approximate them by summing a number of terms from an infinite series. – Mark Reed Commented Feb 14, 2014 at 2:51
  • @Kama, I'm confused. Are you asking what "ln" stands for, or how to use the Math.log() function, or what the algorith is for calculating the natural log of a number? – Jonathan M Commented Feb 14, 2014 at 2:57
Add a ment  | 

2 Answers 2

Reset to default 4

If you want to verify the value for yourself, as some kind of programming exercise, the classical formula for the natural or Neperian (Napier's) logarithm is

ln(a)=limit(n -> inf) n*(root(n,a)-1),

so start with

n=1, a=2 

and loop

n=n*2, a=sqrt(a),
output n*(a-1)

until some kind of convergence is reached. This will break down at some point due to the limits of floating point numbers, the repeated square root converges very fast towards 1.


The traditional definition without using the exponential function is via the integral

ln(a) = integral( 1/x, x=1..a)

where you can use the trapezoid or Simpson method of numerical integration to get increasingly more accurate results.


From the integral formula one gets via the geometric series the power series of the logarithm. A series based formula that will converge a little faster than the direct power series starts with the identity

ln(2)=ln(4/3)-ln(2/3)=ln(1+1/3)-ln(1-1/3)

from

a = (1+x)/(1-x)  <==>  x = (a-1)/(a+1).

Using

ln(1+x)=x-x^2/2+x^3/3-x^4/4+x^5/5-+...

the even powers in the above difference cancel, and

ln(1+x)-ln(1-x)=2*x*(1+x^2/3+x^4/5+...),

So for the putation of ln(2) initialize

x=1/3, xx=x*x, n=1, xpow=1, sum=0

and loop

sum+=xpow/n, xpow *= xx, n+=2
output 2*x*sum

again until some kind of convergence is reached.

ln x gives you the natural logarithm of x (or the value of y that makes the equation e^y = x true, where e is Euler's number)

Math.log(2);

The result will be:

0.6931471805599453

The log() method returns the natural logarithm (base E) of a number.

Note: If the parameter x is negative, NaN is returned.

Note: If the parameter x is 0, -Infinity is returned.

发布评论

评论列表(0)

  1. 暂无评论