Basically you shouldn't do exact comparisons, you should do something like this:
double a = 1.000001;
double b = 0.000001;
double c = a-b;
if (Math.abs(c-1.0) <= 0.000001) {...}
Answer from Kevin on Stack Overflow Top answer 1 of 9
114
Basically you shouldn't do exact comparisons, you should do something like this:
double a = 1.000001;
double b = 0.000001;
double c = a-b;
if (Math.abs(c-1.0) <= 0.000001) {...}
2 of 9
14
Instead of using doubles for decimal arithemetic, please use java.math.BigDecimal. It would produce the expected results.
For reference take a look at this stackoverflow question
00:56
Comparing Doubles with a comparator of Numbers? - Cracking the ...
01:47
How to Compare Two Double Values in Java - YouTube
10:04
Learning Java: Part 12: Comparing Doubles and Using the ...
Java Floating-Point Comparison - double / float Variable ...
06:45
Java String Comparison Tutorial (Equals vs == in Java) - YouTube
Comparing Doubles with a comparator of Numbers ...
Tutorialspoint
tutorialspoint.com › java › lang › double_compare.htm
Java - Double compare() method
We're having two double values and using compare() method, we're comparing these double values and then result is compared to 0. If result is greater than 0 then first number is greater than second number.
HowToDoInJava
howtodoinjava.com › home › java examples › correct way to compare floats or doubles in java
Correct way to compare floats or doubles in Java
January 25, 2022 - Both methods are changing global var and are invoked twice. doubleOne results in 0.1*11+0.1*11, doubleTwo results in 0.1*11*11 You should: Either don’t change global vars in the methods, that’s the best. Invoke them once only, there is no need in invocation prior to if – remove those two method calls before the if block in main. Comments are closed. ... A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
Scaler
scaler.com › home › topics › java double compare()
Java Double compare() - Scaler Topics
May 4, 2023 - The compare() method in Java Double class is a static method used to compare two double values.
Mtu
cclc.mtu.edu › java-doubles-and-floating-point-numbers
Java Doubles and Floating Point Numbers – CCLC
Another way to compare doubles is rounding. However, the more calculations, the greater the possibility that there will be larger rounding errors. Rounding is not always guaranteed to work. A third way to compare doubles is to use Java’s BigDecimal class to define floating point numbers.
Reddit
reddit.com › r/learnprogramming › how do you compare two double values
r/learnprogramming on Reddit: How do you compare two double values
October 17, 2023 -
How do you compare two double values in C programming?
for Example:
double a = .846;
double b = .655 ;
I know that I can not treat them as an int and do the following
if(a > b){
print - a is larger
} else{
print - b is larger
}
But how should I solve it?
Top answer 1 of 9
10
You can compare doubles just like you showed. Where it gets tricky is when you want to see if they are equal. That's because rounding errors may mean they are very close but not exactly equal. For example, if you calculate 10 / 3 * 3, the answer may be closer to 9.999 than 10. Then if you compare the values for equality, you may have a problem. In these cases, subtract the values and check they are close to what you need e.g. if (abs(a - b) < 0.0001) rather than if (a == b).
2 of 9
7
you take an epsilon (you pick a value of how small a difference between the two you consider the same). double e = 0.000000001 double diff = Math.abs(a - b); if (diff < e) { // equal } else if (a < b) { // a < b } else { // a > b } you pick your own value of epsilon. Mostly because each situation has different requirements.
Educative
educative.io › answers › what-is-the-doublecompare-method-in-java
What is the Double.compare method in Java?
compare is a static method of the Double class which is used to compare two double values.
Javatpoint
javatpoint.com › java-double-compare-method
Java Double compare() Method - Javatpoint
Java Double.compareTo() Method The compareTo() method of Double class compares two double values numerically.
Tutorialspoint
tutorialspoint.com › java › lang › double_compareto.htm
Java - Double compareTo() method
We're having two Double objects and using compareTo() method, we're comparing these double objects and then result is compared to 0. If result is greater than 0 then first number is greater than second number.
GeeksforGeeks
geeksforgeeks.org › java › java-double-compareto-method
Java Double.compareTo() Method - GeeksforGeeks
May 15, 2025 - For comparing primitive double values directly, consider using Double.compare(double d1, double d2).
Oracle
docs.oracle.com › en › java › javase › 24 › docs › api › java.base › java › lang › Double.html
Double (Java SE 24 & JDK 24)
July 15, 2025 - This method imposes a total order on Double objects with two differences compared to the incomplete order defined by the Java language numerical comparison operators (<, <=, ==, >=, >) on double values.
Caff
caff.de › posts › double_comparison
de·caff · Double Equality
February 10, 2023 - The standard solution in Java is falling back to java.lang.Double#compare(double, double).
DevGenius
blog.devgenius.io › double-comparison-in-java-d643f73ad7df
Double comparison in Java. Recently I was solving an interesting… | by Pavel Polívka | Dev Genius
May 12, 2021 - This inaccuracy is caused by rounding errors. We need to use a different approach to compare those doubles. If we do not have access to any libraries and want to solve this with Java only we can use something called the threshold method.