MetaGuru
MetaGuru

Reputation: 43813

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

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?

Upvotes: 230

Views: 87976

Answers (10)

Yukulélé
Yukulélé

Reputation: 17062

const logBase = (n, base = Math.E) => Math.log(n) / Math.log(base)

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

Upvotes: 32

Phoenix Vizva'i
Phoenix Vizva'i

Reputation: 53

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.

Upvotes: 3

Peter
Peter

Reputation: 132217

"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);
}

Upvotes: 359

Kartik Singh
Kartik Singh

Reputation: 1

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); }

Upvotes: 0

Ivan Black
Ivan Black

Reputation: 5103

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

Upvotes: 9

Royce Chao
Royce Chao

Reputation: 759

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

Upvotes: 18

artlung
artlung

Reputation: 34013

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.

Upvotes: 16

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827366

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

Upvotes: 56

bramp
bramp

Reputation: 9741

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

Upvotes: 71

Related Questions