diff --git a/decimal.js b/decimal.js index 8eabf43..13fe9ef 100644 --- a/decimal.js +++ b/decimal.js @@ -1222,7 +1222,7 @@ * P.max = function () { Array.prototype.push.call(arguments, this); - return maxOrMin(this.constructor, arguments, 'lt'); + return maxOrMin(this.constructor, arguments, -1); }; */ @@ -1234,7 +1234,7 @@ * P.min = function () { Array.prototype.push.call(arguments, this); - return maxOrMin(this.constructor, arguments, 'gt'); + return maxOrMin(this.constructor, arguments, 1); }; */ @@ -3245,19 +3245,25 @@ /* - * Handle `max` and `min`. `ltgt` is 'lt' or 'gt'. + * Handle `max` (`n` is -1) and `min` (`n` is 1). */ - function maxOrMin(Ctor, args, ltgt) { - var y, + function maxOrMin(Ctor, args, n) { + var k, y, x = new Ctor(args[0]), i = 0; for (; ++i < args.length;) { y = new Ctor(args[i]); + + // NaN? if (!y.s) { x = y; break; - } else if (x[ltgt](y)) { + } + + k = x.cmp(y); + + if (k === n || k === 0 && x.s === n) { x = y; } } @@ -4292,7 +4298,6 @@ // which points to Object. x.constructor = Decimal; - // Duplicate. if (isDecimalInstance(v)) { x.s = v.s; @@ -4593,7 +4598,7 @@ * */ function max() { - return maxOrMin(this, arguments, 'lt'); + return maxOrMin(this, arguments, -1); } @@ -4604,7 +4609,7 @@ * */ function min() { - return maxOrMin(this, arguments, 'gt'); + return maxOrMin(this, arguments, 1); } diff --git a/test/modules/minAndMax.js b/test/modules/minAndMax.js index 54e0d97..262d1b5 100644 --- a/test/modules/minAndMax.js +++ b/test/modules/minAndMax.js @@ -28,18 +28,23 @@ T('min and max', function () { t(NaN, NaN, [Infinity, -2, NaN, 0, -1, -Infinity]); t(0, 0, [0, 0, 0]); - t(-2, Infinity, [-2, 0, -1, Infinity]); + t(-0, 0, [-0, 0, 0]); + t(-0, 0, [0, -0, 0]); + t(-0, 0, [0, 0, -0]); + t(-0, 1, [1, 0, -0]); + t(-2, 0, [0, -1, -0, -2]); + t(-2, Infinity, [-2, -1, -0, 0, Infinity]); t(-Infinity, 0, [-2, 0, -1, -Infinity]); t(-Infinity, Infinity, [-Infinity, -2, 0, -1, Infinity]); t(-Infinity, Infinity, [Infinity, -2, 0, -1, -Infinity]); t(-Infinity, Infinity, [-Infinity, -2, 0, new Decimal(Infinity)]); t(-2, 0, [-2, 0, -1]); - t(-2, 0, [-2, -1, 0]); + t(-2, 0, [-2, -1, -0, 0]); t(-2, 0, [0, -2, -1]); t(-2, 0, [0, -1, -2]); t(-2, 0, [-1, -2, 0]); - t(-2, 0, [-1, 0, -2]); + t(-2, 0, [-1, 0, -0, -2]); t(-1, 1, [-1, 0, 1]); t(-1, 1, [-1, 1, 0]);