diff --git a/lib/utils/util.js b/lib/utils/util.js index e98ff10b..aebb15f5 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -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.