Comparing version 0.3.1 to 0.4.0
@@ -0,1 +1,6 @@ | ||
## es6-shim 0.4.0 (February 8, 2012) | ||
* Added Math.log10, Math.log2, Math.log1p, Math.expm1, Math.cosh, | ||
Math.sinh, Math.tanh, Math.acosh, Math.asinh, Math.atanh, Math.hypot, | ||
Math.trunc. | ||
## es6-shim 0.3.1 (January 30, 2012) | ||
@@ -2,0 +7,0 @@ * Added IE8 support. |
@@ -9,2 +9,9 @@ ({define: (typeof define === 'function') | ||
var global_isFinite = globall.isFinite; | ||
var factorial = function(value) { | ||
var result = 1; | ||
for (var i = 2; i <= value; i++) { | ||
result *= i; | ||
} | ||
return result; | ||
}; | ||
@@ -149,5 +156,61 @@ var defineProperty = function(object, name, method) { | ||
} | ||
}); | ||
}); | ||
defineProperties(Math, { | ||
acosh: function(value) { | ||
return Math.log(value + Math.sqrt(value * value - 1)); | ||
}, | ||
asinh: function(value) { | ||
return Math.log(value + Math.sqrt(value * value + 1)); | ||
}, | ||
atanh: function(value) { | ||
return 0.5 * Math.log((1 + value) / (1 - value)); | ||
}, | ||
cosh: function(value) { | ||
if (value < 0) value = -value; | ||
if (value > 21) return Math.exp(value) / 2; | ||
return (Math.exp(value) + Math.exp(-value)) / 2; | ||
}, | ||
expm1: function(value) { | ||
var result = 0; | ||
var n = 50; | ||
for (var i = 1; i < n; i++) { | ||
result += Math.pow(value, i) / factorial(i); | ||
} | ||
return result; | ||
}, | ||
hypot: function(x, y) { | ||
return Math.sqrt(x * x + y * y) || 0; | ||
}, | ||
log2: function(value) { | ||
return Math.log(value) * (1 / Math.LN2); | ||
}, | ||
log10: function(value) { | ||
return Math.log(value) * (1 / Math.LN10); | ||
}, | ||
log1p: function(value) { | ||
var result = 0; | ||
var n = 50; | ||
if (value <= -1) return -Infinity; | ||
if (value < 0 || value > 1) return Math.log(1 + value); | ||
for (var i = 1; i < n; i++) { | ||
if ((i % 2) === 0) { | ||
result -= Math.pow(value, i) / i; | ||
} else { | ||
result += Math.pow(value, i) / i; | ||
} | ||
} | ||
return result; | ||
}, | ||
sign: function(value) { | ||
@@ -157,2 +220,14 @@ var number = +value; | ||
return (number < 0) ? -1 : 1; | ||
}, | ||
sinh: function(value) { | ||
return (Math.exp(value) - Math.exp(-value)) / 2; | ||
}, | ||
tanh: function(value) { | ||
return (Math.exp(value) - Math.exp(-value)) / (Math.exp(value) + Math.exp(-value)); | ||
}, | ||
trunc: function(value) { | ||
return ~~value; | ||
} | ||
@@ -159,0 +234,0 @@ }); |
@@ -5,3 +5,3 @@ { | ||
"description": "ECMAScript 6 (Harmony) compatibility shims for legacy JavaScript engines", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"homepage": "https://github.com/paulmillr/es6-shim/", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -21,3 +21,5 @@ # ES6 Shim | ||
Object.getPropertyNames, Object.is, Object.isnt | ||
* Math.sign | ||
* Math.sign, Math.log10, Math.log2, Math.log1p, Math.expm1, Math.cosh, | ||
Math.sinh, Math.tanh, Math.acosh, Math.asinh, Math.atanh, Math.hypot, | ||
Math.trunc | ||
@@ -24,0 +26,0 @@ ## IE8 support |
@@ -1,4 +0,1 @@ | ||
var expect = require('expect.js'); | ||
require('../'); | ||
describe('Array', function() { | ||
@@ -5,0 +2,0 @@ describe('Array.from()', function() { |
@@ -1,4 +0,1 @@ | ||
var expect = require('expect.js'); | ||
require('../'); | ||
// Big thanks to V8 folks for test ideas. | ||
@@ -5,0 +2,0 @@ // v8/test/mjsunit/harmony/collections.js |
109
test/math.js
@@ -1,26 +0,111 @@ | ||
var expect = require('expect.js'); | ||
require('../'); | ||
var Assertion = expect().constructor; | ||
Assertion.prototype.almostEqual = function(obj) { | ||
var allowedDiff = 1e-11; | ||
return this.within(obj - allowedDiff, obj + allowedDiff); | ||
} | ||
describe('Math', function() { | ||
describe('Math.sign()', function() { | ||
it('should be 1 on positive values', function() { | ||
describe('#acosh()', function() { | ||
it('should be correct', function() { | ||
expect(Math.acosh(1234)).to.almostEqual(7.811163220849231); | ||
expect(Math.acosh(8.88)).to.almostEqual(2.8737631531629235); | ||
}); | ||
}); | ||
describe('#asinh()', function() { | ||
it('should be correct', function() { | ||
expect(Math.asinh(1234)).to.almostEqual(7.811163549201245); | ||
expect(Math.asinh(9.99)).to.almostEqual(2.997227420191335); | ||
}); | ||
}); | ||
describe('#atanh()', function() { | ||
it('should be correct', function() { | ||
expect(Math.atanh(0.5)).to.almostEqual(0.5493061443340549); | ||
expect(Math.atanh(-0.5)).to.almostEqual(-0.5493061443340549); | ||
expect(Math.atanh(-0.5)).to.almostEqual(-0.5493061443340549); | ||
expect(Math.atanh(0.444)).to.almostEqual(0.47720201260109457); | ||
}); | ||
}); | ||
describe('#cosh()', function() { | ||
it('should be correct', function() { | ||
expect(Math.cosh(12)).to.almostEqual(81377.39571257407); | ||
expect(Math.cosh(0)).to.almostEqual(1); | ||
expect(Math.cosh(-10)).to.almostEqual(11013.232920103323); | ||
}); | ||
}); | ||
describe('#expm1()', function() { | ||
it('should be correct', function() { | ||
expect(Math.expm1(10)).to.almostEqual(22025.465794806718); | ||
expect(Math.expm1(-10)).to.almostEqual(-0.9999546000702375); | ||
}); | ||
}); | ||
describe('#hypot()', function() { | ||
it('should be correct', function() { | ||
expect(Math.hypot(66, 66)).to.almostEqual(93.33809511662427); | ||
expect(Math.hypot(0.1, 100)).to.almostEqual(100.0000499999875); | ||
}); | ||
}); | ||
describe('#log2()', function() { | ||
it('should be correct', function() { | ||
expect(Math.log2(5)).to.almostEqual(2.321928094887362); | ||
expect(Math.log2(32)).to.almostEqual(5); | ||
}); | ||
}); | ||
describe('#log10', function() { | ||
it('should be correct', function() { | ||
expect(Math.log10(5)).to.almostEqual(0.6989700043360189); | ||
expect(Math.log10(50)).to.almostEqual(1.6989700043360187); | ||
}); | ||
}); | ||
describe('#log1p', function() { | ||
it('should be correct', function() { | ||
expect(Math.log1p(5)).to.almostEqual(1.791759469228055); | ||
expect(Math.log1p(50)).to.almostEqual(3.9318256327243257); | ||
}); | ||
}); | ||
describe('#sign()', function() { | ||
it('should be correct', function() { | ||
[Infinity, 1].forEach(function(value) { | ||
expect(Math.sign(value)).to.equal(1); | ||
}); | ||
}); | ||
it('should be -1 on negative values', function() { | ||
[-Infinity, -1].forEach(function(value) { | ||
expect(Math.sign(value)).to.equal(-1); | ||
}); | ||
}); | ||
it('should be 0 on 0', function() { | ||
expect(Math.sign(0)).to.equal(0); | ||
expect(Number.isNaN(Math.sign(NaN))).to.be.ok(); | ||
}); | ||
it('should be NaN on NaN', function() { | ||
expect(isNaN(Math.sign(NaN))).to.be.ok(); | ||
}); | ||
describe('#sinh()', function() { | ||
it('should be correct', function() { | ||
expect(Math.sinh(-5)).to.almostEqual(-74.20321057778875); | ||
expect(Math.sinh(2)).to.almostEqual(3.6268604078470186); | ||
}); | ||
}); | ||
describe('#tanh()', function() { | ||
it('should be correct', function() { | ||
expect(Math.tanh(90)).to.almostEqual(1); | ||
expect(Math.tanh(10)).to.almostEqual(0.9999999958776927); | ||
}); | ||
}); | ||
describe('#trunc()', function() { | ||
it('should be correct', function() { | ||
expect(Math.trunc(1.01)).to.equal(1); | ||
expect(Math.trunc(1.99)).to.equal(1); | ||
expect(Math.trunc(-555.555)).to.equal(-555); | ||
expect(Math.trunc(-1.99)).to.equal(-1); | ||
}); | ||
}); | ||
}); |
@@ -1,4 +0,1 @@ | ||
var expect = require('expect.js'); | ||
require('../'); | ||
describe('Number', function() { | ||
@@ -5,0 +2,0 @@ var integers = [5295, -5295, -9007199254740991, 9007199254740991, 0, -0]; |
@@ -1,4 +0,1 @@ | ||
var expect = require('expect.js'); | ||
require('../'); | ||
describe('Object', function() { | ||
@@ -5,0 +2,0 @@ describe('Object.is()', function() { |
@@ -1,4 +0,1 @@ | ||
var expect = require('expect.js'); | ||
require('../'); | ||
describe('String', function() { | ||
@@ -5,0 +2,0 @@ describe('#repeat()', function() { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
28869
15
704
55