How can I specify the base for Math.log() in JavaScript?
Asked Answered
W

10

227

I need a log function for JavaScript, but it needs to be base 10. I can't see any listing for this, so I'm assuming it's not possible. Are there any math wizards out there who know a solution for this?

Wasp answered 10/6, 2010 at 23:30 Comment(1)
There is a Math.log10() method ever since ECMAScript 2015 for those who come here later on.Newkirk
B
356

"Change of Base" Formula / Identity

The numerical value for logarithm to the base 10 can be calculated with the following identity.

Logarithm for base 10


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);
}
Beffrey answered 10/6, 2010 at 23:33 Comment(9)
In fact, any base can be used, not just e or 2, as long as both logarithms use the same base.Buonaparte
Added an image with the formula and linked to Wikipedia if you don't mind.Hoahoactzin
Wasteful to calculate Math.log(10) each time. Unnecessary to precalculate and store since Math defines this constant already w3schools.com/jsref/jsref_ln10.aspKanazawa
For bases other than 10, scroll down to CMS's answer. If scrolling down and reading an answer sounds like hard work, basic principle is return Math.log(n) / Math.log(base);Domain
I've noticed some potential room for error here, probably having to do with floating point math. I just tried the function above with a value of 1000 in node, and got a result of 2.9999999999999996. (Although other numbers I tried, such as 10, 100, and even 10000, came out with correct values.)Kerri
It's now present in all popular browsers except IE: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Dasha
Might be useful to add Number.EPSILON to the result to curb rounding errors. So that Math.floor(Math.log(1000) / Math.log(10) + Number.EPSILON) === 3 is true.Lynching
While mathematically correct, since JS uses IEEE754 standard for math, there are rounding errors with floating point arithmetics. Therefore, log base mathematics won't work reliably in JS (or any language that uses IEEE574 for that matter). Math.log(9) / Math.log(3) won't be an integer. And adding Number.EPSILON as suggested in the previous comment doesn't solve all cases.Batholomew
@MichaelKariv "Wasteful" yet inconsequential in many applications. I think it's a mistake that Math.LN10 is included in Javascript, b/c that's just one more thing for a programmer to remember. It's trivial to calculate it yourself: const lg10 = Math.log(10), and at least here you don't have to remember more stuffBainbrudge
D
71

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);
Dopester answered 10/6, 2010 at 23:35 Comment(0)
C
56

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);
Clear answered 10/6, 2010 at 23:38 Comment(0)
R
31
const logBase = (n, base) => Math.log(n) / Math.log(base);

https://en.wikipedia.org/wiki/Logarithm#Change_of_base

Roxanaroxane answered 23/1, 2014 at 10:44 Comment(0)
C
18

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
Churchyard answered 1/6, 2013 at 3:20 Comment(1)
Adjust precision with selective rounding: (Math.round(Math.log(1000) / Math.LN10 * 1e6) / 1e6)Wastrel
C
16
Math.log10 = function(n) {
    return (Math.log(n)) / (Math.log(10));
}

Then you can do

Math.log10(your_number);

NOTE: Initially I thought to do Math.prototype.log10 = ... to do this, but user CMS pointed out that Math doesn't work this way, so I edited out the .prototype part.

Carborundum answered 10/6, 2010 at 23:37 Comment(3)
Math is an object, not a constructor function, therefore it doesn't have a prototype property.Athelstan
thanks CMS. Proves one should test things before one feels "inspired." I'll go back to the drawing pad.Carborundum
Just remove the .prototype part ;)Athelstan
B
9

FF 25+ supports a Math.log10 method. You may to use polyfill:

if (!Math.log10) Math.log10 = function(t){ return Math.log(t)/Math.LN10; };

MDN lists the supported browsers.

Desktop Browsers

Chrome    Firefox (Gecko) Internet Explorer   Opera   Safari
38        25 (25)         Not supported       25      7.1

Mobile Browsers

Android         Chrome for Android    Firefox Mobile (Gecko)  IE Mobile      Opera Mobile    Safari Mobile
Not supported   Not supported         25.0 (25)               Not supported  Not supported   iOS 8
Bertine answered 1/8, 2014 at 10:48 Comment(0)
M
3

Math.log10(x)

The top answer is fine for an arbitrary base, but the question is regarding log base 10, and Math.log10(x) has been standard across all browsers since 2015.*

*Except IE, if that's important to you for some reason.

Mothball answered 3/6, 2019 at 14:1 Comment(0)
T
0

If you have a number x, then use of Math.log(x) would essentially be lnx.

To convert it to a base other than e, you can use the following function :

function(x){ return Math.log(x)/Math.log(10); }
Translator answered 24/6, 2016 at 3:41 Comment(0)
D
0

For base 10 use Math.log10().

See docs at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10

Damage answered 19/5, 2020 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.