Merge pull request #13890 from tschaub/math

Use Math.cosh and Math.log2
This commit is contained in:
Tim Schaub
2022-07-27 18:09:15 -06:00
committed by GitHub
5 changed files with 4 additions and 107 deletions

View File

@@ -14,57 +14,6 @@ export function clamp(value, min, max) {
return Math.min(Math.max(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
* 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 * Returns the square of the closest distance between the point (x, y) and the
* line segment (x1, y1) to (x2, y2). * line segment (x1, y1) to (x2, y2).

View File

@@ -3,7 +3,6 @@
*/ */
import Projection from './Projection.js'; import Projection from './Projection.js';
import Units from './Units.js'; import Units from './Units.js';
import {cosh} from '../math.js';
/** /**
* Radius of WGS84 sphere * Radius of WGS84 sphere
@@ -54,7 +53,7 @@ class EPSG3857Projection extends Projection {
global: true, global: true,
worldExtent: WORLD_EXTENT, worldExtent: WORLD_EXTENT,
getPointResolution: function (resolution, point) { getPointResolution: function (resolution, point) {
return resolution / cosh(point[1] / RADIUS); return resolution / Math.cosh(point[1] / RADIUS);
}, },
}); });
} }

View File

@@ -14,7 +14,7 @@ import {
intersects, intersects,
} from '../extent.js'; } from '../extent.js';
import {getTransform} from '../proj.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. * Single triangle; consists of 3 source points and 3 target points.
@@ -169,7 +169,7 @@ class Triangulation {
? Math.max( ? Math.max(
0, 0,
Math.ceil( Math.ceil(
log2( Math.log2(
getArea(targetExtent) / getArea(targetExtent) /
(opt_destinationResolution * (opt_destinationResolution *
opt_destinationResolution * opt_destinationResolution *

View File

@@ -6,7 +6,6 @@
import PaletteTexture from '../webgl/PaletteTexture.js'; import PaletteTexture from '../webgl/PaletteTexture.js';
import {Uniforms} from '../renderer/webgl/TileLayer.js'; import {Uniforms} from '../renderer/webgl/TileLayer.js';
import {asArray, fromString, isStringColor} from '../color.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, * 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 * @return {boolean} True if only one type flag is enabled, false if zero or multiple
*/ */
export function isTypeUnique(valueType) { export function isTypeUnique(valueType) {
return log2(valueType) % 1 === 0; return Math.log2(valueType) % 1 === 0;
} }
/** /**

View File

@@ -2,10 +2,8 @@ import expect from '../expect.js';
import { import {
ceil, ceil,
clamp, clamp,
cosh,
floor, floor,
lerp, lerp,
log2,
modulo, modulo,
round, round,
solveLinearSystem, solveLinearSystem,
@@ -37,54 +35,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);
});
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 () { describe('solveLinearSystem', function () {
it('calculates correctly', function () { it('calculates correctly', function () {
const result = solveLinearSystem([ const result = solveLinearSystem([