Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why java is returning positive infinity when it exceed Double.Max_Value?

Tags:

java

double

When i try to execute this code how result goes greater than Double.MAX_Value?Will overflow/underflow affect double data type in java?

Code:

   result = Double.MAX_VALUE * Double.MAX_VALUE;

   if (result > Double.MAX_VALUE) {
      // Some return statements.
   }
like image 213
user492888 Avatar asked Dec 11 '25 05:12

user492888


1 Answers

From the Java Language Specification

The result of a floating-point multiplication is determined by the rules of IEEE 754 arithmetic:

  • [...]
  • If the magnitude of the product is too large to represent, we say the operation overflows; the result is then an infinity of appropriate sign.

Since Double.MAX_VALUE * Double.MAX_VALUE is too large to represent, its value becomes infinity.

like image 74
Sotirios Delimanolis Avatar answered Dec 12 '25 19:12

Sotirios Delimanolis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!