Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add IEEE float epsilon check

Because of IEEE 754 floating point error, doing a hard equality check
does not always work (as with the case of 0.1 + 0.2). An epsilon check
has been added to allow for these small rounding errors.
  • Loading branch information...
commit 3c0dc3bb05535c2f62338df8370a15114f4902de 1 parent 796ac5d
@trevnorris trevnorris authored
Showing with 9 additions and 0 deletions.
  1. +6 −0 lib/assert/macros.js
  2. +3 −0  test/assert-test.js
View
6 lib/assert/macros.js
@@ -16,6 +16,12 @@ for (var key in messages) {
})(key, assert[key]);
}
+assert.epsilon = function (eps, actual, expected, message) {
+ if (Math.abs(actual - expected) > eps) {
+ assert.fail(actual, fail, message || "expected {expected} +-{eps}, but was {actual}");
+ }
+};
+
assert.ok = (function (callback) {
return function (actual, message) {
callback(actual, message || "expected expression to evaluate to {expected}, but was {actual}");
View
3  test/assert-test.js
@@ -7,6 +7,9 @@ vows.describe('vows/assert').addBatch({
assert.equal("hello world", "hello world");
assert.equal(1, true);
},
+ "`epsilon`": function() {
+ assert.epsilon(1e5, 0.1+0.2, 0.3);
+ },
"`match`": function () {
assert.match("hello world", /^[a-z]+ [a-z]+$/);
},
Please sign in to comment.
Something went wrong with that request. Please try again.