diff --git a/src/ol/math.js b/src/ol/math.js new file mode 100644 index 0000000000..b1fa7f9024 --- /dev/null +++ b/src/ol/math.js @@ -0,0 +1,57 @@ +goog.provide('ol.math'); + + +/** + * @param {number} x X. + * @return {number} Hyperbolic cosine of x. + */ +ol.math.cosh = function(x) { + return (Math.exp(x) + Math.exp(-x)) / 2; +}; + + +/** + * @param {number} x X. + * @return {number} Hyperbolic cotangent of x. + */ +ol.math.coth = function(x) { + var expMinusTwoX = Math.exp(-2 * x); + return (1 + expMinusTwoX) / (1 - expMinusTwoX); +}; + + +/** + * @param {number} x X. + * @return {number} Hyperbolic cosecant of x. + */ +ol.math.csch = function(x) { + return 2 / (Math.exp(x) - Math.exp(-x)); +}; + + +/** + * @param {number} x X. + * @return {number} Hyperbolic secant of x. + */ +ol.math.sech = function(x) { + return 2 / (Math.exp(x) + Math.exp(-x)); +}; + + +/** + * @param {number} x X. + * @return {number} Hyperbolic sine of x. + */ +ol.math.sinh = function(x) { + return (Math.exp(x) - Math.exp(-x)) / 2; +}; + + +/** + * @param {number} x X. + * @return {number} Hyperbolic tangent of x. + */ +ol.math.tanh = function(x) { + var expMinusTwoX = Math.exp(-2 * x); + return (1 - expMinusTwoX) / (1 + expMinusTwoX); +}; diff --git a/test/spec/ol/math.test.js b/test/spec/ol/math.test.js new file mode 100644 index 0000000000..0daab68820 --- /dev/null +++ b/test/spec/ol/math.test.js @@ -0,0 +1,134 @@ +goog.provide('ol.test.math'); + + +describe('ol.math.cosh', function() { + + it('returns the correct value at -Infinity', function() { + expect(ol.math.cosh(-Infinity)).toEqual(Infinity); + }); + + it('returns the correct value at -1', function() { + expect(ol.math.cosh(-1)).toRoughlyEqual(1.5430806348152437, 1e-9); + }); + + it('returns the correct value at 0', function() { + expect(ol.math.cosh(0)).toEqual(1); + }); + + it('returns the correct value at 1', function() { + expect(ol.math.cosh(1)).toRoughlyEqual(1.5430806348152437, 1e-9); + }); + + it('returns the correct value at Infinity', function() { + expect(ol.math.cosh(Infinity)).toEqual(Infinity); + }); + +}); + + +describe('ol.math.coth', function() { + + it('returns the correct value at -1', function() { + expect(ol.math.coth(-1)).toRoughlyEqual(-1.3130352854993312, 1e-9); + }); + + it('returns the correct value at 1', function() { + expect(ol.math.coth(1)).toRoughlyEqual(1.3130352854993312, 1e-9); + }); + + it('returns the correct value at Infinity', function() { + expect(ol.math.coth(Infinity)).toEqual(1); + }); + +}); + + +describe('ol.math.csch', function() { + + it('returns the correct value at -Infinity', function() { + expect(ol.math.csch(-Infinity)).toEqual(0); + }); + + it('returns the correct value at -1', function() { + expect(ol.math.csch(-1)).toRoughlyEqual(-0.85091812823932156, 1e-9); + }); + + it('returns the correct value at 1', function() { + expect(ol.math.csch(1)).toRoughlyEqual(0.85091812823932156, 1e-9); + }); + + it('returns the correct value at Infinity', function() { + expect(ol.math.csch(Infinity)).toEqual(0); + }); + +}); + + +describe('ol.math.sech', function() { + + it('returns the correct value at -Infinity', function() { + expect(ol.math.sech(-Infinity)).toEqual(0); + }); + + it('returns the correct value at -1', function() { + expect(ol.math.sech(-1)).toRoughlyEqual(0.64805427366388535, 1e-9); + }); + + it('returns the correct value at 0', function() { + expect(ol.math.sech(0)).toEqual(1); + }); + + it('returns the correct value at 1', function() { + expect(ol.math.sech(1)).toRoughlyEqual(0.64805427366388535, 1e-9); + }); + + it('returns the correct value at Infinity', function() { + expect(ol.math.sech(Infinity)).toEqual(0); + }); + +}); + + +describe('ol.math.sinh', function() { + + it('returns the correct value at -Infinity', function() { + expect(ol.math.sinh(-Infinity)).toEqual(-Infinity); + }); + + it('returns the correct value at -1', function() { + expect(ol.math.sinh(-1)).toRoughlyEqual(-1.1752011936438014, 1e-9); + }); + + it('returns the correct value at 0', function() { + expect(ol.math.sinh(0)).toEqual(0); + }); + + it('returns the correct value at 1', function() { + expect(ol.math.sinh(1)).toRoughlyEqual(1.1752011936438014, 1e-9); + }); + + it('returns the correct value at Infinity', function() { + expect(ol.math.cosh(Infinity)).toEqual(Infinity); + }); + +}); + + +describe('ol.math.tanh', function() { + + it('returns the correct value at -1', function() { + expect(ol.math.tanh(-1)).toRoughlyEqual(-0.76159415595576485, 1e-9); + }); + + it('returns the correct value at 0', function() { + expect(ol.math.tanh(0)).toEqual(0); + }); + + it('returns the correct value at 1', function() { + expect(ol.math.tanh(1)).toRoughlyEqual(0.76159415595576485, 1e-9); + }); + +}); + + +goog.require('ol.math');