Double#doubleToLongBits(double)值排序

时间:2012-12-13 06:33:22

标签: java jpa double

如果两个双值(a和b)

  1. 两者都不是否定的
  2. 两者都不是NaN,也不是(POSITIVE|NEGATIVE)_INFINITY
  3. a> = b
  4. 总是如此

    1. Double.doubleToLongBits(a)> = Double.doubleToLongBits(b)
    2. 每个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;
      }
      

1 个答案:

答案 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