JavaScript Program to Handle Number Overflow
Last Updated :
16 Oct, 2023
Number overflow occurs when the result of a mathematical operation exceeds the maximum representable value for a numeric data type. In JavaScript, this limit is reached when dealing with numbers beyond the safe range defined by the Number.MAX_SAFE_INTEGER constant, which is approximately 9 quadrillion (9 followed by 15 zeros).
Overflow can lead to inaccurate calculations, unexpected behavior, and even program crashes. It’s crucial to handle this situation gracefully to ensure the reliability and robustness of your JavaScript applications.
Example:
Javascript
const num1 = Number.MAX_SAFE_INTEGER+1;
const num2 = Number.MAX_SAFE_INTEGER+2;
console.log( "max number:" ,Number.MAX_SAFE_INTEGER)
console.log( "after adding 1 and 2" ,num1, num2)
console.log( "Comparing both numbers:" ,num1===num2)
|
Output
max number: 9007199254740991
after adding 1 and 2 9007199254740992 9007199254740992
Comparing both numbers: true
It shows inaccurate results when it exceeds the maximum representable value.
Approach 1: Using BigInt
JavaScript’s BigInt data type allows you to work with arbitrarily large integers. Here’s an example of using BigInt to handle large numbers:
Example: This example shows the use of the above-explained approach.
Javascript
const largeNumber =
BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1);
console.log( "Large Number:" , largeNumber);
|
Output
Large Number: 9007199254740992n
Approach 2: Modular Arithmetic
Modular arithmetic can be useful for tasks like calculating remainders. For instance, to find the remainder when a large number is divided by another number, you can use the `%` operator.
Example: This example shows the use if the above-explained approach.
Javascript
const largeNumber = 12345678901234567890n;
const divisor = 7n;
const remainder = largeNumber % divisor;
console.log( "Remainder:" , remainder);
|
Approach 3: Error Handling
To gracefully handle potential overflows, you can implement error handling with conditional statements.
Example: Here’s an example of error handleing for number overflow.
Javascript
function performCalculation() {
const num1 = Number.MAX_SAFE_INTEGER;
const num2 = Number.MAX_SAFE_INTEGER;
return num1 + num2;
}
const result = performCalculation();
if (result > Number.MAX_SAFE_INTEGER) {
console.error(
"Overflow detected: Result exceeds safe integer limit." );
} else {
console.log( "Result:" , result);
}
|
Output:
Overflow detected: Result exceeds safe integer limit.
Approach 4: Library Utilization
Using libraries like BigNumber.js provides an effective way to perform high-precision arithmetic in JavaScript. First, install the library: (Bash Command)
npm install bignumber.js
Example: This example shows the use of the above-explained approach.
Javascript
const BigNumber = require( "bignumber.js" );
const num1 =
new BigNumber(
"1234567890123456789012345678901234567890" );
const num2 =
new BigNumber(
"9876543210987654321098765432109876543210" );
const result = num1.plus(num2);
console.log( "Result:" , result.toString());
|
Output:
Result: 1.11111111011111111101111111110111111111e+40
Share your thoughts in the comments
Please Login to comment...