在Java中比较浮点数 [英] Comparing floating point numbers in Java

查看:214
本文介绍了在Java中比较浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


我是否应该在比较Java中的浮点数时使用Epsilon? Float.compare(float f1,float f2)静态方法是否可以安全地使用?



谢谢!

编辑:我想我知道了。

当我写,在eclipse中3.6f,编译器将这个数字解释为3.6。但是,当我写1.2f * 3.0f时,结果是3.6000001。虽然在数学上这是错误的,但这两个结果显然是不平等的。因此在检查相等时需要有一个epsilon。

但是,当我在eclipse中写入0.0f时,编译器严格地将其解释为0,因为IEEE 754能够处理它正确。因此,确保浮动是严格正面的(值> 0.0f)是正确的。

我看到的唯一问题是当计算机不使用IEEE 754表示法时,而是使用不正确处理0的表示法。



我是正确的吗?

解决方案Math.ulp()方法在测试中有实际的用处。正如你毫无疑问知道的那样,你通常不应该为了完全相等而比较浮点数。相反,你检查他们在一定的容忍度内是否相等。例如,在JUnit中,您将预期的实际浮点值与如下所示进行比较:
$ b

assertEquals(expectedValue,actualValue,0.02); $ b

这表明实际值在预期值的0.02以内。然而,0.02是否合理?如果预期值是10.5或-107.82,那么0.02可能是好的。然而,如果期望值是几十亿,那么0.02可能完全与零不可区分。通常应该测试的是ULP方面的相对误差。根据计算需要多少准确度,通常选择1到10个ULP之间的某个公差。例如,在这里我指定实际结果需要在真值的5个ULP范围内:
$ b

assertEquals(expectedValue,actualValue,5 * Math.ulp(expectedValue));



http://www.ibm.com/developerworks/java/library/j-math2/index.html


Possible Duplicate:
Manipulating and comparing floating points in java

Am I supposed to use an Epsilon while comparing floating point numbers in Java ? Does the Float.compare(float f1, float f2) static method is safe to use ?

Thanks !

EDIT : I think I got it.

When I write, let's say, 3.6f in eclipse, the compilator interpret this number as 3.6. However, when I write 1.2f * 3.0f, the result is 3.6000001. While mathematically this is false, these two results are obviously inequals. Hence the need to have an epsilon while checking equality.

However, when I write 0.0f in eclipse, the compilator interpret this as 0 strictly, because IEEE 754 is able to handle it correctly. Therefore, ensuring a float is strictly positive with (value > 0.0f) is correct.

The only problem I see is when the computer doesn't use IEEE 754 representation, and instead use a representation which doesn't handle 0 correctly.

Am I right ?

解决方案

Math.ulp() method has a practical use in testing. As you undoubtedly know, you should usually not compare floating-point numbers for exact equality. Instead, you check that they are equal within a certain tolerance. For example, in JUnit you compare expected to actual floating-point values like so:

assertEquals(expectedValue, actualValue, 0.02);

This asserts that the actual value is within 0.02 of the expected value. However is 0.02 a reasonable tolerance? If the expected value is 10.5 or -107.82, then 0.02 is probably fine. However, if the expected value is several billion, then the 0.02 may be completely indistinguishable from zero. Often what you should test is the relative error in terms of ULPs. Depending on how much accuracy the calculation requires, you usually select a tolerance somewhere between 1 and 10 ULPs. For example, here I specify that the actual result needs to be within 5 ULPs of the true value:

assertEquals(expectedValue, actualValue, 5*Math.ulp(expectedValue));

http://www.ibm.com/developerworks/java/library/j-math2/index.html

这篇关于在Java中比较浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆