From f32517a77f711cc9858079ee0e65b307a7a425d7 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 27 Jul 2022 15:09:43 -0600 Subject: [PATCH 1/2] Use Math.cosh --- src/ol/math.js | 26 -------------------------- src/ol/proj/epsg3857.js | 3 +-- test/node/ol/math.test.js | 23 ----------------------- 3 files changed, 1 insertion(+), 51 deletions(-) diff --git a/src/ol/math.js b/src/ol/math.js index 96087f0d03..cf47a81b4e 100644 --- a/src/ol/math.js +++ b/src/ol/math.js @@ -14,32 +14,6 @@ export function clamp(value, min, max) { return Math.min(Math.max(value, min), max); } -/** - * Return the hyperbolic cosine of a given number. The method will use the - * native `Math.cosh` function if it is available, otherwise the hyperbolic - * cosine will be calculated via the reference implementation of the Mozilla - * developer network. - * - * @param {number} x X. - * @return {number} Hyperbolic cosine of x. - */ -export const cosh = (function () { - // Wrapped in a iife, to save the overhead of checking for the native - // implementation on every invocation. - let cosh; - if ('cosh' in Math) { - // The environment supports the native Math.cosh function, use it… - cosh = Math.cosh; - } else { - // … else, use the reference implementation of MDN: - cosh = function (x) { - const y = /** @type {Math} */ (Math).exp(x); - return (y + 1 / y) / 2; - }; - } - 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 diff --git a/src/ol/proj/epsg3857.js b/src/ol/proj/epsg3857.js index 16a69df67d..1ffcf96d65 100644 --- a/src/ol/proj/epsg3857.js +++ b/src/ol/proj/epsg3857.js @@ -3,7 +3,6 @@ */ import Projection from './Projection.js'; import Units from './Units.js'; -import {cosh} from '../math.js'; /** * Radius of WGS84 sphere @@ -54,7 +53,7 @@ class EPSG3857Projection extends Projection { global: true, worldExtent: WORLD_EXTENT, getPointResolution: function (resolution, point) { - return resolution / cosh(point[1] / RADIUS); + return resolution / Math.cosh(point[1] / RADIUS); }, }); } diff --git a/test/node/ol/math.test.js b/test/node/ol/math.test.js index ab7fc0da1e..505a519b13 100644 --- a/test/node/ol/math.test.js +++ b/test/node/ol/math.test.js @@ -2,7 +2,6 @@ import expect from '../expect.js'; import { ceil, clamp, - cosh, floor, lerp, log2, @@ -37,28 +36,6 @@ describe('ol/math.js', () => { }); }); - describe('cosh', function () { - it('returns the correct value at -Infinity', function () { - expect(cosh(-Infinity)).to.eql(Infinity); - }); - - it('returns the correct value at -1', function () { - expect(cosh(-1)).to.roughlyEqual(1.5430806348152437, 1e-9); - }); - - it('returns the correct value at 0', function () { - expect(cosh(0)).to.eql(1); - }); - - it('returns the correct value at 1', function () { - expect(cosh(1)).to.roughlyEqual(1.5430806348152437, 1e-9); - }); - - it('returns the correct value at Infinity', function () { - expect(cosh(Infinity)).to.eql(Infinity); - }); - }); - describe('log2', function () { it('returns the correct value at Infinity', function () { expect(log2(Infinity)).to.eql(Infinity); From fab4e837452a586c94642d2ad430fa24e62b2f12 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 27 Jul 2022 15:11:14 -0600 Subject: [PATCH 2/2] Use Math.log2 --- src/ol/math.js | 25 ------------------------- src/ol/reproj/Triangulation.js | 4 ++-- src/ol/style/expressions.js | 3 +-- test/node/ol/math.test.js | 27 --------------------------- 4 files changed, 3 insertions(+), 56 deletions(-) diff --git a/src/ol/math.js b/src/ol/math.js index cf47a81b4e..142e6f580f 100644 --- a/src/ol/math.js +++ b/src/ol/math.js @@ -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). diff --git a/src/ol/reproj/Triangulation.js b/src/ol/reproj/Triangulation.js index bd1350a999..1e604671f2 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 {log2, modulo} from '../math.js'; +import {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( - log2( + Math.log2( getArea(targetExtent) / (opt_destinationResolution * opt_destinationResolution * diff --git a/src/ol/style/expressions.js b/src/ol/style/expressions.js index f4aa6f51ea..1c69024020 100644 --- a/src/ol/style/expressions.js +++ b/src/ol/style/expressions.js @@ -6,7 +6,6 @@ import PaletteTexture from '../webgl/PaletteTexture.js'; import {Uniforms} from '../renderer/webgl/TileLayer.js'; import {asArray, fromString, isStringColor} from '../color.js'; -import {log2} from '../math.js'; /** * Base type used for literal style parameters; can be a number literal or the output of an operator, @@ -183,7 +182,7 @@ export function getValueType(value) { * @return {boolean} True if only one type flag is enabled, false if zero or multiple */ export function isTypeUnique(valueType) { - return log2(valueType) % 1 === 0; + return Math.log2(valueType) % 1 === 0; } /** diff --git a/test/node/ol/math.test.js b/test/node/ol/math.test.js index 505a519b13..a279a07cfa 100644 --- a/test/node/ol/math.test.js +++ b/test/node/ol/math.test.js @@ -4,7 +4,6 @@ import { clamp, floor, lerp, - log2, modulo, round, solveLinearSystem, @@ -36,32 +35,6 @@ describe('ol/math.js', () => { }); }); - describe('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(-1).toString()).to.eql('NaN'); - }); - }); - describe('solveLinearSystem', function () { it('calculates correctly', function () { const result = solveLinearSystem([