diff --git a/test/objects.js b/test/objects.js index 702e5682b..6d432033a 100644 --- a/test/objects.js +++ b/test/objects.js @@ -106,6 +106,8 @@ $(document).ready(function() { ok(_.isEqual(new Number(75), new Number(75)), "Number objects with identical primitive values are equal"); ok(_.isEqual(75, new Number(75)), "Number primitives and their corresponding object wrappers are equal"); ok(_.isEqual(new Number(75), 75), "Commutative equality is implemented for number objects and primitives"); + ok(!_.isEqual(new Number(0), -0), "`new Number(0)` and `-0` are not equal"); + ok(!_.isEqual(0, new Number(-0)), "Commutative equality is implemented for `new Number(0)` and `-0`"); ok(!_.isEqual(new Number(75), new Number(63)), "Number objects with different primitive values are not equal"); ok(!_.isEqual(new Number(63), {valueOf: function(){ return 63; }}), "Number objects and objects with a `valueOf` method are not equal"); diff --git a/underscore.js b/underscore.js index bac024592..934f3f83e 100644 --- a/underscore.js +++ b/underscore.js @@ -684,14 +684,16 @@ // equivalent to `new String("5")`. return String(a) == String(b); case '[object Number]': - case '[object Boolean]': - // Coerce numbers, dates, and booleans to numeric primitive values. a = +a; b = +b; - // `NaN`s are equivalent, but non-reflexive. - return a != a ? b != b : a == b; + // `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for + // other numeric values. + return a != a ? b != b : (a == 0 ? 1 / a == 1 / b : a == b); case '[object Date]': - // Compare dates by their millisecond representations. Invalid dates are not equivalent. + case '[object Boolean]': + // Coerce dates and booleans to numeric primitive values. Dates are compared by their + // millisecond representations. Note that invalid dates with millisecond representations + // of `NaN` are not equivalent. return +a == +b; // RegExps are compared by their source patterns and flags. case '[object RegExp]':