diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 6bac2236..dd350611 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -1445,10 +1445,7 @@ jasmine.Matchers.prototype.toBeCloseTo = function(expected, precision) { if (!(precision === 0)) { precision = precision || 2; } - var multiplier = Math.pow(10, precision); - var actual = Math.round(this.actual * multiplier); - expected = Math.round(expected * multiplier); - return expected == actual; + return Math.abs(expected - this.actual) < (Math.pow(10, -precision) / 2); }; /** @@ -2537,5 +2534,5 @@ jasmine.version_= { "major": 1, "minor": 2, "build": 0, - "revision": 1337006083 + "revision": 1343710612 }; diff --git a/spec/core/MatchersSpec.js b/spec/core/MatchersSpec.js index 7be2b814..963a557f 100644 --- a/spec/core/MatchersSpec.js +++ b/spec/core/MatchersSpec.js @@ -510,6 +510,11 @@ describe("jasmine.Matchers", function() { expect(-1.23).not.toBeCloseTo(-1.24); }); + it("expects close numbers to 'be close' and further numbers not to", function() { + expect(1.225).not.toBeCloseTo(1.234); // difference is 0.009 + expect(1.225).toBeCloseTo(1.224); // difference is 0.001 + }); + it("accepts an optional precision argument", function() { expect(1).toBeCloseTo(1.1, 0); expect(1.2).toBeCloseTo(1.23, 1); diff --git a/src/core/Matchers.js b/src/core/Matchers.js index 7303facb..ad025638 100644 --- a/src/core/Matchers.js +++ b/src/core/Matchers.js @@ -302,10 +302,7 @@ jasmine.Matchers.prototype.toBeCloseTo = function(expected, precision) { if (!(precision === 0)) { precision = precision || 2; } - var multiplier = Math.pow(10, precision); - var actual = Math.round(this.actual * multiplier); - expected = Math.round(expected * multiplier); - return expected == actual; + return Math.abs(expected - this.actual) < (Math.pow(10, -precision) / 2); }; /** diff --git a/src/version.js b/src/version.js index e728798a..0ed57491 100644 --- a/src/version.js +++ b/src/version.js @@ -3,5 +3,5 @@ jasmine.version_= { "major": 1, "minor": 2, "build": 0, - "revision": 1337006083 + "revision": 1343710612 };