diff --git a/es6-shim.js b/es6-shim.js index 527fb9d6..b0cf3cd4 100644 --- a/es6-shim.js +++ b/es6-shim.js @@ -1957,8 +1957,9 @@ tanh: function tanh(value) { var x = Number(value); if (Number.isNaN(x) || x === 0) { return x; } - if (x === Infinity) { return 1; } - if (x === -Infinity) { return -1; } + // can exit early at +-20 as JS loses precision for true value at this integer + if (x >= 20) { return 1; } + if (x <= -20) { return -1; } var a = Math.expm1(x); var b = Math.expm1(-x); if (a === Infinity) { return 1; } diff --git a/test/math.js b/test/math.js index 494d7c5a..f81218f1 100644 --- a/test/math.js +++ b/test/math.js @@ -605,7 +605,10 @@ describe('Math', function () { expect(isNegativeZero(Math.tanh(-0))).to.equal(true); expect(Math.tanh(Infinity)).to.equal(1); expect(Math.tanh(-Infinity)).to.equal(-1); - expect(Math.tanh(90)).to.almostEqual(1); + expect(Math.tanh(19)).to.almostEqual(1); + expect(Math.tanh(-19)).to.almostEqual(-1); + expect(Math.tanh(20)).to.equal(1); // JS loses precision for true value at this integer + expect(Math.tanh(-20)).to.equal(-1); // JS loses precision for true value at this integer expect(Math.tanh(10)).to.almostEqual(0.9999999958776927); expect(Math.tanh(-2e-17)).to.equal(-2e-17); });