Explain Codes LogoExplain Codes Logo

How to implement infinity in Java?

java
infinity
math-operations
floating-point-numbers
Anton ShumikhinbyAnton Shumikhin·Dec 30, 2024
TLDR

To represent infinity in Java, use the constants provided by the Double and Float classes: Double.POSITIVE_INFINITY for positive infinity and Double.NEGATIVE_INFINITY for negative infinity, like so:

double posInf = Double.POSITIVE_INFINITY; // The limit does not exist! - Mean Girls of Math double negInf = Double.NEGATIVE_INFINITY; // Like your chances of winning the lottery

Alternatively, creating infinity can be achieved by dividing a positive or negative number by zero:

double posInfCalc = 1.0 / 0.0; // Like dividing the number of stars in the universe by zero double negInfCalc = -1.0 / 0.0; // Like dividing the number of times I've been to Mars by zero

Important: Remember that dividing integers by zero will throw an ArithmeticException, so always utilize floats or doubles when calculating infinities.

Running arithmetic operations with infinity

Infinity in Java adheres to the IEEE 754 standard, which includes operations with infinity. Here's a quick guide to running the mathematics:

  • Addition: Double.POSITIVE_INFINITY + anyPositiveDouble stays Double.POSITIVE_INFINITY. Likewise, Double.NEGATIVE_INFINITY + anyNegativeDouble remains Double.NEGATIVE_INFINITY.
  • Subtraction: Beware! Double.POSITIVE_INFINITY - Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY - Double.NEGATIVE_INFINITY equates to NaN (Not A Number).
  • Multiplication: Multiplying Double.POSITIVE_INFINITY by a negative number gives you Double.NEGATIVE_INFINITY and vice versa.
  • Division: Any positive number divided by Double.POSITIVE_INFINITY results in 0.

Choosing Double.POSITIVE_INFINITY or Float.POSITIVE_INFINITY depends on the required precision and the problem you're solving.

Performing comparisons with infinity

Comparing values with infinity? Here are some handy tips:

  • Comparing infinities: When comparing, remember that Double.POSITIVE_INFINITY is always larger than any finite value, and Double.NEGATIVE_INFINITY is less than any finite value.
  • NaN Comparisons: Any comparisons involving NaN, including NaN == NaN, always returns false. To check if a number is NaN, use Double.isNaN().
  • Infinity equals Infinity: Surprisingly, Double.POSITIVE_INFINITY equals Double.POSITIVE_INFINITY, and the same goes for their negative counterparts.

Feeling infinite: Practical use cases

Infinity has its practical uses. In some algorithms where a value represents an unreachable limit, Double.POSITIVE_INFINITY is a valid choice. Similarly, in optimization problems, infinity can be employed as an initial impossible minimum cost. Coding with infinity? Sure will feel exalting!

Pitfalls and how to avoid them

Infinity handling can have its quirks:

  • Integral types: Unlike floating-point types (double, float), integral types (int, long) do not natively support infinity. Misusing Integer.MAX_VALUE or Integer.MIN_VALUE can lead to integer overflow issues.
  • Unexpected NaN: Operations like 0 * Double.POSITIVE_INFINITY will result in NaN, which might seem counterintuitive but is a result of IEEE 754 standards.
  • Serialization issues: Be careful when serializing or converting floating-point infinities to other formats or languages. Not all systems play nice with them.