Use Math.log2

This commit is contained in:
Tim Schaub
2022-07-27 15:11:14 -06:00
parent f32517a77f
commit fab4e83745
4 changed files with 3 additions and 56 deletions

View File

@@ -14,31 +14,6 @@ export function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
/**
* Return the base 2 logarithm of a given number. The method will use the
* native `Math.log2` function if it is available, otherwise the base 2
* logarithm will be calculated via the reference implementation of the
* Mozilla developer network.
*
* @param {number} x X.
* @return {number} Base 2 logarithm of x.
*/
export const log2 = (function () {
// Wrapped in a iife, to save the overhead of checking for the native
// implementation on every invocation.
let log2;
if ('log2' in Math) {
// The environment supports the native Math.log2 function, use it…
log2 = Math.log2;
} else {
// … else, use the reference implementation of MDN:
log2 = function (x) {
return Math.log(x) * Math.LOG2E;
};
}
return log2;
})();
/**
* Returns the square of the closest distance between the point (x, y) and the
* line segment (x1, y1) to (x2, y2).