mm1975
mm1975

Reputation: 1655

Calculate quotient with Logarithm log2

I have to calculate a simple quotient:

log2(val1) / log2(val2)

I´ve tried to this with:

 var valueOne = Math.log2(val1);
 var valueTwo = Math.log2(val2);
 var quotient = valueOne / valueTwo;

Unfortunately,it do not work.As I found out, there are obviously browser compatibility problems with Math.log2

In Chrome, I get *'Uncaught TypeError: undefined is not a function'*

How can now calculate the quotient?

Upvotes: 1

Views: 144

Answers (2)

Pointy
Pointy

Reputation: 413737

The Math.log2(x) function can be computed as Math.log(x) / Math.LN2;. That can be derived from the nature of logarithms.

The .log2() function is a newcomer to the Math constructor, and not supported by all browsers.

Upvotes: 1

karan3112
karan3112

Reputation: 1867

Use this custom function

function log2(val) {
 return Math.log(val) / Math.LN2;
}

var valueOne = log2(val1);

DEMO

Upvotes: 3

Related Questions