Skip to content

Commit cb60444

Browse files
committed
#241 Match Math.max and Math.min negative zero handling
1 parent 08e5a38 commit cb60444

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

decimal.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@
12221222
*
12231223
P.max = function () {
12241224
Array.prototype.push.call(arguments, this);
1225-
return maxOrMin(this.constructor, arguments, 'lt');
1225+
return maxOrMin(this.constructor, arguments, -1);
12261226
};
12271227
*/
12281228

@@ -1234,7 +1234,7 @@
12341234
*
12351235
P.min = function () {
12361236
Array.prototype.push.call(arguments, this);
1237-
return maxOrMin(this.constructor, arguments, 'gt');
1237+
return maxOrMin(this.constructor, arguments, 1);
12381238
};
12391239
*/
12401240

@@ -3245,19 +3245,25 @@
32453245

32463246

32473247
/*
3248-
* Handle `max` and `min`. `ltgt` is 'lt' or 'gt'.
3248+
* Handle `max` (`n` is -1) and `min` (`n` is 1).
32493249
*/
3250-
function maxOrMin(Ctor, args, ltgt) {
3251-
var y,
3250+
function maxOrMin(Ctor, args, n) {
3251+
var k, y,
32523252
x = new Ctor(args[0]),
32533253
i = 0;
32543254

32553255
for (; ++i < args.length;) {
32563256
y = new Ctor(args[i]);
3257+
3258+
// NaN?
32573259
if (!y.s) {
32583260
x = y;
32593261
break;
3260-
} else if (x[ltgt](y)) {
3262+
}
3263+
3264+
k = x.cmp(y);
3265+
3266+
if (k === n || k === 0 && x.s === n) {
32613267
x = y;
32623268
}
32633269
}
@@ -4292,7 +4298,6 @@
42924298
// which points to Object.
42934299
x.constructor = Decimal;
42944300

4295-
// Duplicate.
42964301
if (isDecimalInstance(v)) {
42974302
x.s = v.s;
42984303

@@ -4593,7 +4598,7 @@
45934598
*
45944599
*/
45954600
function max() {
4596-
return maxOrMin(this, arguments, 'lt');
4601+
return maxOrMin(this, arguments, -1);
45974602
}
45984603

45994604

@@ -4604,7 +4609,7 @@
46044609
*
46054610
*/
46064611
function min() {
4607-
return maxOrMin(this, arguments, 'gt');
4612+
return maxOrMin(this, arguments, 1);
46084613
}
46094614

46104615

test/modules/minAndMax.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,23 @@ T('min and max', function () {
2828
t(NaN, NaN, [Infinity, -2, NaN, 0, -1, -Infinity]);
2929

3030
t(0, 0, [0, 0, 0]);
31-
t(-2, Infinity, [-2, 0, -1, Infinity]);
31+
t(-0, 0, [-0, 0, 0]);
32+
t(-0, 0, [0, -0, 0]);
33+
t(-0, 0, [0, 0, -0]);
34+
t(-0, 1, [1, 0, -0]);
35+
t(-2, 0, [0, -1, -0, -2]);
36+
t(-2, Infinity, [-2, -1, -0, 0, Infinity]);
3237
t(-Infinity, 0, [-2, 0, -1, -Infinity]);
3338
t(-Infinity, Infinity, [-Infinity, -2, 0, -1, Infinity]);
3439
t(-Infinity, Infinity, [Infinity, -2, 0, -1, -Infinity]);
3540
t(-Infinity, Infinity, [-Infinity, -2, 0, new Decimal(Infinity)]);
3641

3742
t(-2, 0, [-2, 0, -1]);
38-
t(-2, 0, [-2, -1, 0]);
43+
t(-2, 0, [-2, -1, -0, 0]);
3944
t(-2, 0, [0, -2, -1]);
4045
t(-2, 0, [0, -1, -2]);
4146
t(-2, 0, [-1, -2, 0]);
42-
t(-2, 0, [-1, 0, -2]);
47+
t(-2, 0, [-1, 0, -0, -2]);
4348

4449
t(-1, 1, [-1, 0, 1]);
4550
t(-1, 1, [-1, 1, 0]);

0 commit comments

Comments
 (0)