如果两个双值(a和b)
NaN
,也不是(POSITIVE|NEGATIVE)_INFINITY
,总是如此
每个IEEE 754的内容是否有任何错误?
我打算将两个正(非零)整数的速率值存储为长值而不是双精度值。
@Entity
public class ExchangeRate {
@PrePersist
protected void prePersist() {
targetAmountPerSourceAmount = Double.doubleToLongBits(
(double) targetAmount / (double) sourceAmount);
}
@Basic
private long targetAmountPerSourceAmount;
@Min(1)
@Max(Short.MAX_VALUE)
private short targetAmount;
@Min(1)
@Max(Short.MAX_VALUE)
private short sourceAmount;
}
答案 0 :(得分:2)
嗯,零会给你带来问题,因为+0.0 == -0.0但它们有不同的表示形式。
double a = -0.0;
double b = +0.0;
System.out.println("a == b? " + (a == b)); // true
long aLong = Double.doubleToLongBits(a);
long bLong = Double.doubleToLongBits(b);
System.out.println("a' >= b'? " + (aLong >= bLong)); // false