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
.
Double.MAX_VALUE
1.7976931348623157E308
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.
Double.POSITIVE_INFINITY
Double
Please explain.
When you say "gives", are you meaning the string representation? The way a double is converted to a string is described in detail in the Javadoc.
– Andy Turner
10 mins ago
Maybe you can find the desired answer here: JLS §4.2.3.
– L.Spillner
3 mins ago
2 Answers
2
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.
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.
POSITIVE_INFINITY
NEGATIVE_INFINITY
System.out.println( 1E300 * 1E20 ); // Infinity
System.out.println( –1E300 * 1E20 ); // -Infinity
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It's a special case
– Maurice Perry
11 mins ago