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 1/5] 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). From bce97306449c86c4407b276c5eec928c8f15d2ae Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Thu, 30 Apr 2020 19:03:37 +0100 Subject: [PATCH 2/5] Test log2 function --- test/spec/ol/math.test.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/spec/ol/math.test.js b/test/spec/ol/math.test.js index 0da366e902..77988f89ac 100644 --- a/test/spec/ol/math.test.js +++ b/test/spec/ol/math.test.js @@ -2,6 +2,7 @@ import { clamp, cosh, lerp, + log2, modulo, solveLinearSystem, toDegrees, @@ -52,6 +53,32 @@ describe('ol.math.cosh', function () { }); }); +describe('ol.math.log2', function () { + it('returns the correct value at Infinity', function () { + expect(log2(Infinity)).to.eql(Infinity); + }); + + it('returns the correct value at 3', function () { + expect(log2(3)).to.roughlyEqual(1.584962500721156, 1e-9); + }); + + it('returns the correct value at 2', function () { + expect(log2(2)).to.eql(1); + }); + + it('returns the correct value at 1', function () { + expect(log2(1)).to.eql(0); + }); + + it('returns the correct value at 0', function () { + expect(log2(0)).to.eql(-Infinity); + }); + + it('returns the correct value at -1', function () { + expect(log2(0)).to.eql(NaN); + }); +}); + describe('ol.math.solveLinearSystem', function () { it('calculates correctly', function () { const result = solveLinearSystem([ From d3507e5c5e7ba1d76cdaed1f8c32099372af43f0 Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Thu, 30 Apr 2020 19:07:20 +0100 Subject: [PATCH 3/5] replace Math.log2 with log2 from ol/math --- src/ol/reproj/Triangulation.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ol/reproj/Triangulation.js b/src/ol/reproj/Triangulation.js index b8b1b7edf9..da7f70ead7 100644 --- a/src/ol/reproj/Triangulation.js +++ b/src/ol/reproj/Triangulation.js @@ -14,7 +14,7 @@ import { intersects, } from '../extent.js'; import {getTransform} from '../proj.js'; -import {modulo} from '../math.js'; +import {log2, modulo} from '../math.js'; /** * Single triangle; consists of 3 source points and 3 target points. @@ -169,7 +169,7 @@ class Triangulation { ? Math.max( 0, Math.ceil( - Math.log2( + log2( getArea(targetExtent) / (opt_destinationResolution * opt_destinationResolution * From f91e51c28ba01d5af73b1b83ae99875075cf5225 Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Thu, 30 Apr 2020 19:11:41 +0100 Subject: [PATCH 4/5] Test log2 function fix typo --- test/spec/ol/math.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/spec/ol/math.test.js b/test/spec/ol/math.test.js index 77988f89ac..60f0ba9d14 100644 --- a/test/spec/ol/math.test.js +++ b/test/spec/ol/math.test.js @@ -75,7 +75,7 @@ describe('ol.math.log2', function () { }); it('returns the correct value at -1', function () { - expect(log2(0)).to.eql(NaN); + expect(log2(-1)).to.eql(NaN); }); }); From 8415627731658ebec1d5729f1687ab2d733df3bc Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Thu, 30 Apr 2020 19:39:33 +0100 Subject: [PATCH 5/5] Test log2 function use string to test NaN --- test/spec/ol/math.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/spec/ol/math.test.js b/test/spec/ol/math.test.js index 60f0ba9d14..96a722f8bd 100644 --- a/test/spec/ol/math.test.js +++ b/test/spec/ol/math.test.js @@ -75,7 +75,7 @@ describe('ol.math.log2', function () { }); it('returns the correct value at -1', function () { - expect(log2(-1)).to.eql(NaN); + expect(log2(-1).toString()).to.eql('NaN'); }); });