util: add isSafeAddition.
This commit is contained in:
parent
5bf68a765f
commit
be0e7e02d2
@ -200,6 +200,14 @@ if (Object.assign)
|
||||
|
||||
util.MAX_SAFE_INTEGER = 0x1fffffffffffff;
|
||||
|
||||
/**
|
||||
* Max safe addition (52 bits).
|
||||
* @const {Number}
|
||||
* @default
|
||||
*/
|
||||
|
||||
util.MAX_SAFE_ADDITION = 0xfffffffffffff;
|
||||
|
||||
/**
|
||||
* Test whether a number is below MAX_SAFE_INTEGER.
|
||||
* @param {Number} value
|
||||
@ -210,6 +218,19 @@ util.isSafeInteger = function isSafeInteger(value) {
|
||||
return value >= -0x1fffffffffffff && value <= 0x1fffffffffffff;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether the result of a positive
|
||||
* addition would be below MAX_SAFE_INTEGER.
|
||||
* @param {Number} value
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
util.isSafeAddition = function isSafeAddition(a, b) {
|
||||
assert(a >= 0);
|
||||
assert(b >= 0);
|
||||
return a <= util.MAX_SAFE_ADDITION && b <= util.MAX_SAFE_ADDITION;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether a number is Number,
|
||||
* finite, and below MAX_SAFE_INTEGER.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user