测试浮点相等性.(FE_FLOATING_POINT_EQUALITY) [英] Test for floating point equality. (FE_FLOATING_POINT_EQUALITY)
问题描述
我在 ANT 脚本中使用了 findbugs,但我不知道如何修复我的两个错误.我已经阅读了文档,但不明白.以下是我的错误以及与之相关的代码:
I am using a findbugs in an ANT script and I can't figure out how to fix two of my errors. I have read the documentation, but don't understand. Here are my errors and the code that goes with them:
错误 1:测试浮点相等性.(FE_FLOATING_POINT_EQUALITY)
Error 1: Test for floating point equality. (FE_FLOATING_POINT_EQUALITY)
private boolean equals(final Quantity other) {
return this.mAmount == convertedAmount(other);
}
错误 2:EQ_COMPARETO_USE_OBJECT_EQUALS
Error 2: EQ_COMPARETO_USE_OBJECT_EQUALS
public final int compareTo(final Object other) {
return this.description().compareTo(((Decision) other).description());
}
我已经阅读了说明
强烈推荐,但不严格要求 (x.compareTo(y)==0) == (x.equals(y)).一般来说,任何实现 Comparable 接口并违反此条件的类都应该清楚地表明这一事实.推荐的语言是注意:这个类有一个与equals不一致的自然顺序."
It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."
还有关于浮点等式的文档
and also the docs regarding the floating point equality
此操作比较两个浮点值是否相等.由于浮点计算可能涉及四舍五入,因此计算出的浮点和双精度值可能不准确.对于必须精确的值(例如货币值),请考虑使用固定精度类型,例如 BigDecimal.对于不需要精确的值,请考虑比较某个范围内的相等性,例如: if ( Math.abs(x - y) <.0000001 ).请参阅 Java 语言规范,第 4.2.4 节.
This operation compares two floating point values for equality. Because floating point calculations may involve rounding, calculated float and double values may not be accurate. For values that must be precise, such as monetary values, consider using a fixed-precision type such as BigDecimal. For values that need not be precise, consider comparing for equality within some range, for example: if ( Math.abs(x - y) < .0000001 ). See the Java Language Specification, section 4.2.4.
虽然我不明白.任何人都可以帮忙吗?
I don't get it though. Can anyone please help?
推荐答案
问题 1:
对于 FE_FLOATING_POINT_EQUALITY 问题,您不应该直接使用 ==
运算符比较两个浮点值,因为由于舍入误差很小,即使您的应用程序的值在语义上相等"条件 value1 == value2
不成立.
For the FE_FLOATING_POINT_EQUALITY issue, you should not be comparing two float values directly with the ==
operator, since due to tiny rounding errors, the values might be semantically "equal" for your application even if the condition value1 == value2
does not hold true.
为了解决这个问题,修改你的代码如下:
In order to fix this, modify your code as follows:
private boolean equals(final Quantity other) {
return (Math.abs(this.mAmount - convertedAmount(other)) < EPSILON);
}
EPSILON 是您应该在代码中定义的常量,代表您的应用程序可接受的微小差异,例如.0000001.
Where EPSILON is a constant that you should define in your code, and represents small differences that are acceptable to your application, e.g. .0000001.
问题 2:
对于 EQ_COMPARETO_USE_OBJECT_EQUALS 问题:强烈建议在 x.compareTo(y)
返回零的地方,x.equals(y)
应该是 true代码>.在你的代码中你已经实现了
compareTo
,但是你没有覆盖 equals
,所以你从 Object<继承了
equals
的实现/code>,不满足上述条件.
For the EQ_COMPARETO_USE_OBJECT_EQUALS issue: It is strongly recommended that wherever x.compareTo(y)
returns zero, x.equals(y)
should be true
. In your code you have implemented compareTo
, but you have not overriden equals
, so you are inheriting the implementation of equals
from Object
, and the above condition is not met.
为了解决这个问题,在你的类中覆盖equals
(可能还有hashCode
),这样当x.compareTo(y)
返回 0,然后 x.equals(y)
将返回 true
.
In order to fix this, override equals
(and perhaps hashCode
) in your class, so that when x.compareTo(y)
returns 0, then x.equals(y)
will return true
.
这篇关于测试浮点相等性.(FE_FLOATING_POINT_EQUALITY)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!