在JBox2d中,存在用于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;
}
我想知道这里的浮点数<->整型位转换函数有什么作用。这是否提供了一种绕过Java浮点比较不准确问题的方法(如果可能的话)?或者完全是别的什么东西?我想知道它是否是epsilon方法的替代方法:
if (Math.abs(floatVal1 - floatVal2) < epsilon)
PS。出于完整性和趣味性的考虑,这里是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;
}
仅供参考,我完全明白为什么在hashCode()中使用转换函数--散列ID必须是整数。
https://stackoverflow.com/questions/3667932
复制相似问题