Prefer native Math.log2(), but support IE through internalized polyfill.

This commit is contained in:
Jürg Lehni 2016-07-09 12:54:17 +02:00
parent 7e3d18f5d4
commit 2532f205a7

View file

@ -60,6 +60,10 @@ var Numerical = new function() {
var abs = Math.abs, var abs = Math.abs,
sqrt = Math.sqrt, sqrt = Math.sqrt,
pow = Math.pow, pow = Math.pow,
// Fallback to polyfill:
log2 = Math.log2 || function(x) {
return Math.log(x) * Math.LOG2E;
},
// Constants // Constants
EPSILON = 1e-12, EPSILON = 1e-12,
MACHINE_EPSILON = 1.12e-16; MACHINE_EPSILON = 1.12e-16;
@ -97,8 +101,7 @@ var Numerical = new function() {
function getNormalizationFactor(x) { function getNormalizationFactor(x) {
// Normalization is done by scaling coefficients with a power of 2, so // Normalization is done by scaling coefficients with a power of 2, so
// that all the bits in the mantissa remain unchanged. // that all the bits in the mantissa remain unchanged.
return pow(2, -Math.floor( return pow(2, -Math.round(log2(x || MACHINE_EPSILON)));
Math.log(x || MACHINE_EPSILON) * Math.LOG2E + 0.5));
} }
return /** @lends Numerical */{ return /** @lends Numerical */{