Basically like the opposite of Math.pow()
.
I want a function which can be used as such a logarithm:
var mynum = findpower(36, 6); // 2, because 6 squared is 36
How could such a function be constructed?
Join Stack Overflow to learn, share knowledge, and build your career.
Basically like the opposite of Math.pow()
.
I want a function which can be used as such a logarithm:
var mynum = findpower(36, 6); // 2, because 6 squared is 36
How could such a function be constructed?
It's a logarithm
Math.log(36) / Math.log(6) = 2
in general case
Math.log(number) / Math.log(base)
The logarithm has following property: if a = log(n = number, b = base) (usually denoted as logb(n)) then ba = n. In my code above I used logarithms property: logy(x) = logc(x) / logc(y) where c is any positive number.
Math.log
in examples above is the natural logarithm, i.e. the logarithm with base = e (≈ 2.718281828…, see Math.E). Obviously, if we have the natural logarithm (usually denoted as ln(n) or log(n)), then we have the logarithm with any other base via the equation given above: logy(x) = log(x) / log(y)
javascript
, a function
(or prototype
) could be welcome.
– F. Hauri
Dec 22 '12 at 8:41
Math.log(number) / Math.log(base)
is valid javascript code, that works just fine. Isn't it a prototype?
– Artem Sobolev
Dec 22 '12 at 8:43