From 8076a5126ded4a2b360bcfeb35c80d792ad80ff7 Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Thu, 30 Apr 2020 18:44:49 +0100 Subject: [PATCH] Add log2 function --- src/ol/math.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/ol/math.js b/src/ol/math.js index 2baa07c8e4..8de0517fe6 100644 --- a/src/ol/math.js +++ b/src/ol/math.js @@ -40,6 +40,31 @@ export const cosh = (function () { return cosh; })(); +/** + * 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).