为什么在Java float比较中使用Float.floatToIntBits()? [英] Why use Float.floatToIntBits() in Java float comparisons?

查看:874
本文介绍了为什么在Java float比较中使用Float.floatToIntBits()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JBox2d中,存在以下代码: Vec2.equals()

  @Override 
public boolean equals(Object obj){//由Eclipse自动生成
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass()!= obj.getClass())
return false;
Vec2 other =(Vec2)obj;
if(Float.floatToIntBits(x)!= Float.floatToIntBits(other.x))
return false;
if(Float.floatToIntBits(y)!= Float.floatToIntBits(other.y))
return false;
返回true;
}

我想知道float int - 这里。这是否提供了解决Java的浮点比较不准确问题的方法(如果可能的话)?还是完全是另一回事?我想知道,如果这是一个替代的epsilon方法:

  if(Math.abs(floatVal1  -  floatVal2)< epsilon )

PS。为了完整性和利益,这里是 Vec2.hashCode()

  @Override 
public int hashCode(){//由Eclipse自动生成
final int prime = 31;
int result = 1;
result = prime * result + Float.floatToIntBits(x);
result = prime * result + Float.floatToIntBits(y);
返回结果;





$ b $ p


$ b $ p $ { - 散列ID必须是整数。

解决方案

可以在 Joshua Bloch的Effective Java float 和<$ c $因为存在 -0.0 NaN ,所以需要特殊的处理无限,负无穷。这就是为什么Sun JVM的 Float.equals()看起来像这样(6u21):

  public boolean equals(Object obj)
{
return(obj instanceof Float)
&& (floatToIntBits(((Float)obj).value)== floatToIntBits(value)); (





$所以,不, Math.abs()与epsilon不是一个好的选择。从Javadoc:
$ b


如果f1和f2都表示Float.NaN,
那么equals方法返回true,
即使Float.NaN == Float.NaN有
的值为false。如果f1表示
+ 0.0f,而f2表示-0.0f,反之亦然,即使0.0f == - 0.0f
的值为true,相等测试的
值为false 。

这就是为什么Eclipse的自动生成的代码为你做的。


In JBox2d, there exists the following code for Vec2.equals():

@Override
public boolean equals(Object obj) { //automatically generated by Eclipse
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Vec2 other = (Vec2) obj;
    if (Float.floatToIntBits(x) != Float.floatToIntBits(other.x))
        return false;
    if (Float.floatToIntBits(y) != Float.floatToIntBits(other.y))
        return false;
    return true;
}

I am wondering what purpose the float<->int bit conversions functions serve, here. Does this provide a way to get around Java's float comparison inaccuracy problem (if such is even possible)? Or is it something else altogether? I am wondering if it is an alternative to the epsilon approach:

if (Math.abs(floatVal1 - floatVal2) < epsilon)

PS. for the sake of completeness and interest, here is Vec2.hashCode():

@Override
public int hashCode() { //automatically generated by Eclipse
    final int prime = 31;
    int result = 1;
    result = prime * result + Float.floatToIntBits(x);
    result = prime * result + Float.floatToIntBits(y);
    return result;
}

FYI, I can see perfectly why the conversion functions are used in hashCode() -- hash IDs must be integers.

解决方案

The explanation can be found in Joshua Bloch's Effective Java: float and Float need special treatment because of the existence of -0.0, NaN, positive infinity, and negative infinity. That's why the Sun JVM's Float.equals() looks like this (6u21):

public boolean equals(Object obj)
{
    return (obj instanceof Float)
           && (floatToIntBits(((Float)obj).value) == floatToIntBits(value));
}

So, no, Math.abs() with an epsilon is not a good alternative. From the Javadoc:

If f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false. If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true.

That's why Eclipse's autogenerated code does that for you.

这篇关于为什么在Java float比较中使用Float.floatToIntBits()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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