mirror of
https://github.com/MikeMcl/decimal.js.git
synced 2025-06-06 09:24:21 +00:00
#241 Match Math.max and Math.min negative zero handling
This commit is contained in:
parent
08e5a381d2
commit
cb60444aaf
23
decimal.js
23
decimal.js
@ -1222,7 +1222,7 @@
|
|||||||
*
|
*
|
||||||
P.max = function () {
|
P.max = function () {
|
||||||
Array.prototype.push.call(arguments, this);
|
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 () {
|
P.min = function () {
|
||||||
Array.prototype.push.call(arguments, this);
|
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) {
|
function maxOrMin(Ctor, args, n) {
|
||||||
var y,
|
var k, y,
|
||||||
x = new Ctor(args[0]),
|
x = new Ctor(args[0]),
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
for (; ++i < args.length;) {
|
for (; ++i < args.length;) {
|
||||||
y = new Ctor(args[i]);
|
y = new Ctor(args[i]);
|
||||||
|
|
||||||
|
// NaN?
|
||||||
if (!y.s) {
|
if (!y.s) {
|
||||||
x = y;
|
x = y;
|
||||||
break;
|
break;
|
||||||
} else if (x[ltgt](y)) {
|
}
|
||||||
|
|
||||||
|
k = x.cmp(y);
|
||||||
|
|
||||||
|
if (k === n || k === 0 && x.s === n) {
|
||||||
x = y;
|
x = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -4292,7 +4298,6 @@
|
|||||||
// which points to Object.
|
// which points to Object.
|
||||||
x.constructor = Decimal;
|
x.constructor = Decimal;
|
||||||
|
|
||||||
// Duplicate.
|
|
||||||
if (isDecimalInstance(v)) {
|
if (isDecimalInstance(v)) {
|
||||||
x.s = v.s;
|
x.s = v.s;
|
||||||
|
|
||||||
@ -4593,7 +4598,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function max() {
|
function max() {
|
||||||
return maxOrMin(this, arguments, 'lt');
|
return maxOrMin(this, arguments, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -4604,7 +4609,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function min() {
|
function min() {
|
||||||
return maxOrMin(this, arguments, 'gt');
|
return maxOrMin(this, arguments, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,18 +28,23 @@ T('min and max', function () {
|
|||||||
t(NaN, NaN, [Infinity, -2, NaN, 0, -1, -Infinity]);
|
t(NaN, NaN, [Infinity, -2, NaN, 0, -1, -Infinity]);
|
||||||
|
|
||||||
t(0, 0, [0, 0, 0]);
|
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, 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, -1, -Infinity]);
|
t(-Infinity, Infinity, [Infinity, -2, 0, -1, -Infinity]);
|
||||||
t(-Infinity, Infinity, [-Infinity, -2, 0, new Decimal(Infinity)]);
|
t(-Infinity, Infinity, [-Infinity, -2, 0, new Decimal(Infinity)]);
|
||||||
|
|
||||||
t(-2, 0, [-2, 0, -1]);
|
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, -2, -1]);
|
||||||
t(-2, 0, [0, -1, -2]);
|
t(-2, 0, [0, -1, -2]);
|
||||||
t(-2, 0, [-1, -2, 0]);
|
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, 0, 1]);
|
||||||
t(-1, 1, [-1, 1, 0]);
|
t(-1, 1, [-1, 1, 0]);
|
||||||
|
Loading…
Reference in New Issue
Block a user