util: add isSafeAddition.

This commit is contained in:
Christopher Jeffrey 2017-06-17 18:19:12 -07:00
parent 5bf68a765f
commit be0e7e02d2
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -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.