The logarithm base 10 (that is b = 10) is called the decimal or common logarithm and is commonly used in science and engineering. The natural logarithm has the number e (that is b ≈ 2.718) as its base; its use is widespread in mathematics and physics, because of its simpler integral and derivative.
Open the document and place the cursor at the point where you want to insert the logarithm. Type "log," followed by the subscript icon given under the "Font" category of the "Home" tab. Type the base of the logarithm in subscript; for instance, "2." Press the subscript icon again to revert to normal font.
The log2() function returns the base-2 logarithm of a number. If the number is 0, the log2() function will return -Infinity. If the number is a negative value, the log2() function will return NaN.
"Change of Base" Formula / Identity
The numerical value for logarithm to the base 10 can be calculated with the following identity.
Since Math.log(x)
in JavaScript returns the natural logarithm of x
(same as ln(x)), for base 10 you can divide by Math.log(10)
(same as ln(10)):
function log10(val) {
return Math.log(val) / Math.LN10;
}
Math.LN10
is a built-in precomputed constant for Math.log(10)
, so this function is essentially identical to:
function log10(val) {
return Math.log(val) / Math.log(10);
}
Easy, just change the base by dividing by the log(10). There is even a constant to help you
Math.log(num) / Math.LN10;
which is the same as:
Math.log(num) / Math.log(10);
You can simply divide the logarithm of your value, and the logarithm of the desired base, also you could override the Math.log
method to accept an optional base argument:
Math.log = (function() {
var log = Math.log;
return function(n, base) {
return log(n)/(base ? log(base) : 1);
};
})();
Math.log(5, 10);
the answer here would cause obvious precision problem and is not reliable in some use cases
> Math.log(10)/Math.LN10
1
> Math.log(100)/Math.LN10
2
> Math.log(1000)/Math.LN10
2.9999999999999996
> Math.log(10000)/Math.LN10
4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With