How to get x from 2 ^ x = 8000?

Possible duplicate:
What is the opposite of Math.pow JavaScript?

2^x=i

Given i, how can we calculate x using Javascript?

+5
source share
2 answers

You want to take the logarithm of 8000. JS has a function Math.logthat uses base e, you want base 2so you can write Math.log(8000) / Math.log(2)to get the logarithm of 8000 base 2, which is equal to x.

+7
source

You need the logarithm from the Math object. It does not provide a 2 log base, so do the conversion:

var x = Math.log(8000) / Math.log(2);

Math javascript object reference.

In a more general case, we calculate 2 ^ x = i as follows:

var i; // Some number
var x = Math.log(i) / Math.log(2);
0
source

All Articles