测试浮点平等。 (FE_FLOATING_POINT_EQUALITY) [英] Test for floating point equality. (FE_FLOATING_POINT_EQUALITY)

查看:2870
本文介绍了测试浮点平等。 (FE_FLOATING_POINT_EQUALITY)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ant脚本中使用FindBugs的,我无法弄清楚如何解决我的两个错误。我已阅读文档,但不明白。这里是我的错误和code与他们有云:

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))。一般来说,实现可比接口和违背此条件应该清楚地指出这一事实的任何类。推荐的语言是注意:这个类有一个自然排序与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."

和有关也浮点平等文档

此操作两个浮点数值是否相等。由于浮点计算可能涉及四舍五入,计算float和double值可能不准确。对于值必须是precise,如货币值,可以考虑使用固定precision类型,如BigDecimal的。对于该值不必是precise,考虑到一些范围内争取平等的比较,例如:如果(Math.abs(X - Y)&​​LT; 0.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问题,你不应该用 == 运营商,因为由于微小的舍入误差直接比较两个浮点值,该值可能是语义上平等您的应用程序即使条件值1 ==数值2 不成立。

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.

为了解决这个问题,修改code如下:

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是一个常数,你应该在你的code定义,并重新presents小的差异是可以接受您的应用程序,例如0.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)真正。在您的code你已经实现了的compareTo ,但是你有没有重写等于,所以你继承的实现的等于对象,和上述条件不能满足。

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.

为了解决这个问题,重写等于(也许散code )在你的类,所以,当则x.compareTo(Y)返回0,则 x.equals(Y)将返回真正

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屋!

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