Skip to content

Commit

Permalink
[BUGFIX] #344 minumum(0, -0) should be -0
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMcl committed Apr 23, 2023
1 parent 2603bfd commit 981a8a9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
24 changes: 10 additions & 14 deletions bignumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@
* arguments {number|string|BigNumber}
*/
BigNumber.maximum = BigNumber.max = function () {
return maxOrMin(arguments, P.lt);
return maxOrMin(arguments, -1);
};


Expand All @@ -647,7 +647,7 @@
* arguments {number|string|BigNumber}
*/
BigNumber.minimum = BigNumber.min = function () {
return maxOrMin(arguments, P.gt);
return maxOrMin(arguments, 1);
};


Expand Down Expand Up @@ -1291,24 +1291,20 @@


// Handle BigNumber.max and BigNumber.min.
function maxOrMin(args, method) {
var n,
// If any number is NaN, return NaN.
function maxOrMin(args, n) {
var k, y,
i = 1,
m = new BigNumber(args[0]);
x = new BigNumber(args[0]);

for (; i < args.length; i++) {
n = new BigNumber(args[i]);

// If any number is NaN, return NaN.
if (!n.s) {
m = n;
break;
} else if (method.call(m, n)) {
m = n;
y = new BigNumber(args[i]);
if (!y.s || (k = compare(x, y)) === n || k === 0 && x.s === n) {
x = y;
}
}

return m;
return x;
}


Expand Down
24 changes: 10 additions & 14 deletions bignumber.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ function clone(configObject) {
* arguments {number|string|BigNumber}
*/
BigNumber.maximum = BigNumber.max = function () {
return maxOrMin(arguments, P.lt);
return maxOrMin(arguments, -1);
};


Expand All @@ -644,7 +644,7 @@ function clone(configObject) {
* arguments {number|string|BigNumber}
*/
BigNumber.minimum = BigNumber.min = function () {
return maxOrMin(arguments, P.gt);
return maxOrMin(arguments, 1);
};


Expand Down Expand Up @@ -1288,24 +1288,20 @@ function clone(configObject) {


// Handle BigNumber.max and BigNumber.min.
function maxOrMin(args, method) {
var n,
// If any number is NaN, return NaN.
function maxOrMin(args, n) {
var k, y,
i = 1,
m = new BigNumber(args[0]);
x = new BigNumber(args[0]);

for (; i < args.length; i++) {
n = new BigNumber(args[i]);

// If any number is NaN, return NaN.
if (!n.s) {
m = n;
break;
} else if (method.call(m, n)) {
m = n;
y = new BigNumber(args[i]);
if (!y.s || (k = compare(x, y)) === n || k === 0 && x.s === n) {
x = y;
}
}

return m;
return x;
}


Expand Down
24 changes: 24 additions & 0 deletions test/methods/minmax.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ Test('minimum and maximum', function () {
Test.areEqual(BigNumber.maximum, BigNumber.max);
Test.areEqual(BigNumber.minimum, BigNumber.min);

function isMinusZero(x) {
return x.isZero() && x.isNegative();
}

t(isMinusZero(BigNumber.min(-0, 0)));
t(isMinusZero(BigNumber.min(0, -0)));
t(isMinusZero(BigNumber.min('-0', '0')));
t(isMinusZero(BigNumber.min('0', '-0')));
t(isMinusZero(BigNumber.min(-0, 1, 0, 2)));
t(isMinusZero(BigNumber.min(0, 1, 2, -0)));
t(isMinusZero(BigNumber.min(2, 1, -0, 0)));

function isPositiveZero(x) {
return x.isZero() && x.isPositive();
}

t(isPositiveZero(BigNumber.max(-0, 0)));
t(isPositiveZero(BigNumber.max(0, -0)));
t(isPositiveZero(BigNumber.max('-0', '0')));
t(isPositiveZero(BigNumber.max('0', '-0')));
t(isPositiveZero(BigNumber.max(0, -1, -0, -2)));
t(isPositiveZero(BigNumber.max(0, -1, 0, -2, -0)));
t(isPositiveZero(BigNumber.max(-0, -1, -2, -0, 0)));

t(!BigNumber.min(0, 0, 0).isNaN());
t(BigNumber.min(NaN, -2, 0, -1).isNaN());
t(BigNumber.max(NaN, -2, 0, -1).isNaN());
Expand Down

0 comments on commit 981a8a9

Please sign in to comment.