如果两个双精度值(a和b)


两者都不是负面的
两者都不是NaN也不是任何(POSITIVE|NEGATIVE)_INFINITY
a> = b


总是这样吗


Double.doubleToLongBits(a)> = Double.doubleToLongBits(b)




每个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.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

10-07 23:53