Use Math.cosh

This commit is contained in:
Tim Schaub
2022-07-27 15:09:43 -06:00
parent f2d989b299
commit f32517a77f
3 changed files with 1 additions and 51 deletions

View File

@@ -14,32 +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 * 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 * native `Math.log2` function if it is available, otherwise the base 2

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

@@ -2,7 +2,6 @@ import expect from '../expect.js';
import { import {
ceil, ceil,
clamp, clamp,
cosh,
floor, floor,
lerp, lerp,
log2, 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 () { describe('log2', function () {
it('returns the correct value at Infinity', function () { it('returns the correct value at Infinity', function () {
expect(log2(Infinity)).to.eql(Infinity); expect(log2(Infinity)).to.eql(Infinity);