How Is "Infinity" of Type Double in Java

How Is "Infinity" of Type Double in Java

This may be a very simple question but I am very confused here.

Double.MAX_VALUE gives 1.7976931348623157E308 which indeed is a floating point literal and hence double.

However, Double.POSITIVE_INFINITY gives infinity. How is infinity of type Double? It doesn't look like a decimal number or even a number.

Please explain.

4

2 Answers

At a binary level in IEEE 754 (which is not exactly the same as Java floating point), infinity is represented as:

Positive and negative infinity are represented thus:

  • sign = 0 for positive infinity, 1 for negative infinity.

  • biased exponent = all 1 bits.

  • fraction = all 0 bits.

1

POSITIVE_INFINITY (and its counterpart, NEGATIVE_INFINITY) are special constants that Java uses to notify you of overflow of certain operations where the result can no longer be represented as a Double (or Float) value, e.g.

System.out.println( 1E300 * 1E20 ); // Infinity
System.out.println( –1E300 * 1E20 ); // -Infinity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.