English 中文(简体)
如何比较 Java的两个双重价值?
原标题:How to compare two double values in Java?
  • 时间:2011-11-10 15:19:33
  •  标签:
  • java

简略地比较 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("
"+((a-b)==1.0));
    }
}

以上代码看来是<代码>true, 对该表述的评价(a-b)=1.0>, 但其编号为t。 0.999999999999999999<0>。 克服这种局面的最佳和所建议的方法是什么?

最佳回答

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. It would produce the expected results.

参考请参看stackoverflow question

        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!");

考虑这一法典:

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

它回到了第一个Double是否等于第二个Double。 我不敢肯定这是否会在你的具体情况下发挥作用(如凯文所述,在浮动点上做任何数学都可能导致不准确的结果),但我难以对两倍进行比较,这二者实际上是平等的,但还是使用比较。 计算方法后为零。

我只想到,在这种情况下,任何人都需要进行比较,以检查他们是否确实平等,而不是相似。

这应当纳入 Java。 有两家图书馆在安全地比较双倍数方面做了出色的工作,其第三种ep价值是:

  • Guava s DoubleMath:
// this returns true if roughlyOne is very close to 1
DoubleMath.fuzzyEquals(1.0, roughlyOne, 0.0001d)
  • Apache Common s has a very similar method, Precision.equals

而对于其价值来说,Junnit最近对平等进行了解释,主张采用双倍的方法,其中也不包括ep门门门槛。

如果你不使用双手法,你可考虑使用以下方法:

double n1 = 0.1234567890123450;
double n2 = 0.1234567890123449;
boolean res = (Math.abs(n1 - n2) <= 1e-16 * Math.max(Math.abs(n1), 
Math.abs(n2)));
System.out.println(res);

但是,如果你仅比较一下<_____________1 - n2>,你可以得出以下比较结果:

boolean res = (Math.abs(n1 - n2) <= 1e-16);
System.out.println(res);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签