Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for Math.log2

So I have this script that use Javascript's Math.log2() function. Tested it in IE 9 today and found out that IE does not support log2. It only supports log.

Does anyone know of a way that I can get the same result as log base 2? An example of my code is below:

var number = 16,
    exponent = Math.log2(number);

//Will return 4
return exponent;
like image 840
Max Baldwin Avatar asked Jan 09 '15 03:01

Max Baldwin


1 Answers

Expression Math.log(number) / Math.log(2) is equivalent to Math.log2(number)

http://www.mathwords.com/c/change_of_base_formula.htm

like image 151
MBo Avatar answered Sep 20 '22 14:09

MBo