Saturday, October 6, 2012

Javascript: calculating logarithm of a number in any base

Given that the javascript log() method returns the natural logarithm of the given number. You have to use the logarithms property using the natural logarithms. For example to calculate the log of x with 10 base or 2 base:


 So,


function log(val, x) {
  return Math.log(val) / Math.log(X);
}

where x is the base, and val is the number you want to calculate the logarithm.

Resources:
http://www.w3schools.com/jsref/jsref_log.asp
http://stackoverflow.com/questions/3019278/any-way-to-specify-the-base-of-math-log-in-javascript

No comments: