Javatpoint Logo
Javatpoint Logo

Comparing Doubles in Java

Working with floating-point numbers such as doubles is a common practice in Java. While comparing integers in Java, sometimes be a bit difficult due to their inherent errors. Comparison of integers in Java, is straightforward, compared with double values that requires careful consideration and accounting for possible rounding errors. In this section, we will explore various ways to double-lock in Java and discuss the pitfalls to avoid while doing so.

Equality Comparison

A common mistake when comparing doubles in Java is trying to use the equality operator (==). This is problematic because small differences can occur due to pairwise representation of different values, making direct equivalence tests unreliable.

For example:

Surprisingly, the above code will print "Not Equal" because of the precision issues with double values.

Comparing with Epsilon

The most common way to solve the accuracy problem is to use the epsilon value. It is a small number that represents the maximum acceptable difference between two binary values and we can then absolutely differentiate it between the two binaries is closed epsilon. If the absolute difference is less than an epsilon, the values are considered equal. Here's an example:

In this case, the code will print "Equal" because the absolute difference between a and b is smaller than the chosen epsilon.

Using Double.compare()

Java provides the Double.compare() method to compare two double values. Returns a value indicating that the first value is less than, equal to, or greater than the second value. The method handles the precision issues for us and is a recommended way to compare doubles.

The above code will correctly identify that a and b are equal.

Using BigDecimal Class

We can use the BigDecimal class for more accurate decimal comparisons, especially in financial or critical applications. BigDecimal allows us to do decimal calculations and comparisons more accurately. Here is an example of how to use BigDecimal for double comparisons.

This code will correctly identify that a and b are equal, and it provides a high level of precision.

Dealing with NaN and Infinite Values

When comparing double values, we should also consider special cases like NaN (Not-a-Number) and infinite values. To check for NaN, we can use the Double.isNaN() method. To check for infinity, we can use Double.isInfinite().

Here's a complete Java program that demonstrates the different approaches to comparing double values, along with their corresponding outputs.

File Name: CompareDoubles.java

Output:

Approach 1:
a and b are not equal.
Approach 2:
a and b are equal.
Approach 3:
a and b are equal.
Approach 4:
a and b are equal.
Special Cases:
c is NaN
d is infinite
e is infinite

As we can see, the program demonstrates the different comparison approaches and handles special cases like NaN and infinity, providing the correct output for each case.

Conclusion:

When comparing double values in Java, precision issues can lead to unexpected results if we rely on simple equality checks. It's essential to use appropriate techniques like epsilon comparisons, Double.compare(), or BigDecimal for more accurate and reliable comparisons. Additionally, always handle special cases like NaN and infinite values appropriately to ensure robust code in our applications.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA