Why below code output result is equal? How to compare?
var num1 = 340282000000000000000000000000000000001
var num2 = 340282000000000000000000000000000000000 // 3.402823E+38
if(num1 == num2) {
console.log('num1 equal to num2');
} else if(num1 > num2) {
console.log('num1 bigger than num2');
} else {
console.log('num1 lower than num2')
}
2
Answers
Hi it seems like you hit the maximum value that Java can handle for an integer, for such high value, you can try :
var num1 = BigInteger("340282000000000000000000000000000000001");
var num2 = BigInteger("340282000000000000000000000000000000000");
BigInteger class is used for the mathematical operation which involves very big integer calculations that are outside the limit of all available primitive data types.
Note that exponential notation is not meant for precision, it’s an imprecise number, most of the time.
Both numbers provided result in the same exponential number.
It’s possible to convert a big number to big integer just adding the suffix
n
: