简体   繁体   English

如何在Java中比较两个double值?

[英]How to compare two double values in Java?

A simple comparison of two double values in Java creates some problems. Java中两个double值的简单比较会产生一些问题。 Let's consider the following simple code snippet in Java. 让我们考虑以下Java中的简单代码段。

package doublecomparision;

final public class DoubleComparision 
{
    public static void main(String[] args) 
    {
        double a = 1.000001;
        double b = 0.000001;

        System.out.println("\n"+((a-b)==1.0));
    }
}

The above code appears to return true , the evaluation of the expression ((ab)==1.0) but it doesn't. 上面的代码似乎返回true ,即表达式((ab)==1.0)的求值,但不是。 It returns false instead because the evaluation of this expression is 0.9999999999999999 which was actually expected to be 1.0 which is not equal to 1.0 hence, the condition evaluates to boolean false . 它返回false因为此表达式的评估值为0.9999999999999999 ,实际上该期望值为1.0 ,不等于1.0因此该条件的评估值为boolean false What is the best and suggested way to overcome such a situation? 解决这种情况的最佳建议方法是什么?

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) {...}

Instead of using doubles for decimal arithemetic, please use java.math.BigDecimal. 请勿将java.math.BigDecimal用作双精度十进制算术。 It would produce the expected results. 它将产生预期的结果。

For reference take a look at this stackoverflow question 作为参考,请看一下这个stackoverflow问题

You can use Double.compare ; 您可以使用Double.compare ; It compares the two specified double values. 它比较两个指定的double值。

        int mid = 10;
        for (double j = 2 * mid; j >= 0; j = j - 0.1) {
            if (j == mid) {
                System.out.println("Never happens"); // is NOT printed
            }

            if (Double.compare(j, mid) == 0) {
                System.out.println("No way!"); // is NOT printed
            }

            if (Math.abs(j - mid) < 1e-6) {
                System.out.println("Ha!"); // printed
            }
        }
        System.out.println("Gotcha!");
double a = 1.000001;
double b = 0.000001;

System.out.println( a.compareTo(b) );

Returns : 返回值

  • -1 : 'a' is numerically less than 'b'. -1:“ a”在数值上小于“ b”。

  • 0 : 'a' is equal to 'b'. 0:“ a”等于“ b”。

  • 1 : 'a' is greater than 'b'. 1:“ a”大于“ b”。

Consider this line of code: 考虑以下代码行:

Math.abs(firstDouble - secondDouble) < Double.MIN_NORMAL

It returns whether firstDouble is equal to secondDouble. 它返回firstDouble是否等于secondDouble。 I'm unsure as to whether or not this would work in your exact case (as Kevin pointed out, performing any math on floating points can lead to imprecise results) however I was having difficulties with comparing two double which were, indeed, equal, and yet using the 'compareTo' method didn't return 0. 我不确定这是否适合您的具体情况(正如凯文指出的那样,对浮点数进行任何数学运算都可能导致结果不准确),但是我很难比较两个相等的双精度数,这实际上是相等的,但是使用'compareTo'方法并没有返回0。

I'm just leaving this there in case anyone needs to compare to check if they are indeed equal, and not just similar. 我只是把它留在那里,以防有人需要比较以检查它们是否确实相等,而不仅仅是相似。

Just use Double.compare() method to compare double values. 只需使用Double.compare()方法比较双精度值即可。
Double.compare((d1,d2) == 0) Double.compare((d1,d2)== 0)

double d1 = 0.0;
double d2 = 0.0;

System.out.println(Double.compare((d1,d2) == 0))  // true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM