5

Possible Duplicate:
What's the opposite of JavaScript's Math.pow?

2^x=i

Given i, how can we calculate x using Javascript?

Community
  • 1
  • 1

2 Answers2

7

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

Shad
  • 15,134
  • 2
  • 22
  • 34
Dan
  • 10,531
  • 2
  • 36
  • 55
0

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

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

Reference to the javascript Math object.

In the more general case we calculate 2^x = i this way:

var i; // Some number
var x = Math.log(i) / Math.log(2);
Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100