Why does java return positive infinity when it exceeds Double.Max_Value?

When I try to execute this code, how will the result be greater than Double.MAX_Value? Will overflow / undercompletion affect double datatype in java?

Code:

   result = Double.MAX_VALUE * Double.MAX_VALUE;

   if (result > Double.MAX_VALUE) {
      // Some return statements.
   }

      

+3


source to share


1 answer


From the Java Language Specification

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

  • [...]
  • If the quantity of the product is too large to represent, we say that the operation is overflowed ; the result is the infinity of the corresponding sign .


Since it is Double.MAX_VALUE * Double.MAX_VALUE

too large to represent, its meaning becomes infinite.

+6


source







All Articles