Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll have a look at the concept of infinity in Java and how we can work with it.

2. Intro to Numbers in Java

In mathematics, we have a set of real numbers and a set of integers. Evidently, both of these sets are unlimited, and both contain positive and negative infinity.

In the computer world, we need a memory location where we can store the values for these sets, and this location must be finite as the memory of a computer is finite.

For int type in Java, the concept of infinity is not covered. We can only store integer numbers that fit in the memory location that we chose.

For real numbers, we also have the concept of infinity, either positive or negative. The 32 bits float type and also the 64 bits double type supports this in Java. Moving forward, we’ll use the double type for exemplifying as it is also the most used type for real numbers in Java, for it has better precision.

3. Positive Infinity

The constant Double.POSITIVE_INFINITY holds the positive infinity value of type double. This value is obtained by dividing 1.0 by 0.0. Its String representation is Infinity. This value is a convention, and its hexadecimal representation is 7FF0000000000000. Every double variable with this bitwise value contains positive infinity.

4. Negative Infinity

The constant Double.NEGATIVE_INFINITY holds the negative infinity value of type double. This value is obtained by dividing -1.0 by 0.0. Its String representation is -Infinity. This value is also a convention, and its hexadecimal representation is FFF0000000000000. Every double variable with this bitwise value contains negative infinity.

5. Operations with Infinity

Let’s declare a double variable called positiveInfinity and assign to it the value Double.POSITIVE_INFINITY and another double variable negativeInfinity and assign to it value Double.NEGATIVE_INFINITY. Then, we get the following results for the operations:

Double positiveInfinity = Double.POSITIVE_INFINITY;
Double negativeInfinity = Double.NEGATIVE_INFINITY;
        
assertEquals(Double.NaN, (positiveInfinity + negativeInfinity));
assertEquals(Double.NaN, (positiveInfinity / negativeInfinity));
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - negativeInfinity));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - positiveInfinity));
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity * negativeInfinity));

Here, the constant Double.NaN represents a result that is not a number.

Let’s see the mathematical operations with infinity and a positive number:

Double positiveInfinity = Double.POSITIVE_INFINITY;
Double negativeInfinity = Double.NEGATIVE_INFINITY;
double positiveNumber = 10.0; 
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity + positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity + positiveNumber));
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - positiveNumber));
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity * positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity * positiveNumber));
       
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity / positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity / positiveNumber));
        
assertEquals(Double.NEGATIVE_INFINITY, (positiveNumber - positiveInfinity));
assertEquals(Double.POSITIVE_INFINITY, (positiveNumber - negativeInfinity));
        
assertEquals(0.0, (positiveNumber / positiveInfinity));
assertEquals(-0.0, (positiveNumber / negativeInfinity));

Now, let’s see the mathematical operations with infinity and a negative number:

Double positiveInfinity = Double.POSITIVE_INFINITY;
Double negativeInfinity = Double.NEGATIVE_INFINITY;
double negativeNumber = -10.0; 
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity + negativeNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity + negativeNumber));
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - negativeNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - negativeNumber));
        
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity * negativeNumber));
assertEquals(Double.POSITIVE_INFINITY, (negativeInfinity * negativeNumber));
        
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity / negativeNumber));
assertEquals(Double.POSITIVE_INFINITY, (negativeInfinity / negativeNumber));
        
assertEquals(Double.NEGATIVE_INFINITY, (negativeNumber - positiveInfinity));
assertEquals(Double.POSITIVE_INFINITY, (negativeNumber - negativeInfinity));
        
assertEquals(-0.0, (negativeNumber / positiveInfinity));
assertEquals(0.0, (negativeNumber / negativeInfinity));

There are a few rules of thumb to remember these operations better:

  • Replace the negative and positive infinities with Infinity and -Infinity, respectively, and perform the sign operations first
  • For any operation between a number different from zero and infinity, you will get a result of infinity
  • When we add or divide positive and negative infinity, we do get not a number result

6. Division by Zero

The division by zero is a special case of division because it produces negative and positive infinity values.

To exemplify, let’s take a double value d and check the results of the following divisions by zero:

double d = 1.0;
        
assertEquals(Double.POSITIVE_INFINITY, (d / 0.0));
assertEquals(Double.NEGATIVE_INFINITY, (d / -0.0));
assertEquals(Double.NEGATIVE_INFINITY, (-d / 0.0));
assertEquals(Double.POSITIVE_INFINITY, (-d / -0.0));

7. Conclusion

In this article, we explored the concept and usage of positive and negative infinity in Java. The implementation and code snippets can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.