Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is "infinity" of type Double in Java

Tags:

java

oop

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.

like image 243
Akhil Prajapati Avatar asked Oct 17 '25 23:10

Akhil Prajapati


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.

like image 97
Alex Taylor Avatar answered Oct 20 '25 13:10

Alex Taylor


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
like image 36
Gereon Avatar answered Oct 20 '25 13:10

Gereon