zoukankan      html  css  js  c++  java
  • java8-Double

     具体可以参考我写的这个文章。IEEE754浮点数规范

    注意此类里面的以下几点

    有个神奇的语法。

    System.out.println(0xfp3);

    结果是120.0 0xf * 2 ^ 3

    另外注意这俩个方法的区别

    doubleToLongBits

    doubleToRawLongBits

    这俩个方法的区别在于对NAN的处理,doubleToLongBits对NAN返回的bit位符合IEEE754的规范。doubleToRawLongBits没有对NAN做任何的改变

    另外,对NAN的任何结果都是NAN

    public static void main(String[] args) throws NoSuchFieldException, SecurityException
        {
            check(Double.NEGATIVE_INFINITY);//-infinity
            check(Double.POSITIVE_INFINITY);//infinity
            check(Double.NaN);//NAN
            System.out.println(Double.NaN*Double.POSITIVE_INFINITY);//NaN
            System.out.println(Double.POSITIVE_INFINITY*1);//Infinity
            System.out.println(Double.NEGATIVE_INFINITY*1);//-Infinity
            System.out.println(Double.NEGATIVE_INFINITY*Double.POSITIVE_INFINITY);//-Infinity
            System.out.println(0xfp3);//120.0
            System.out.println(1e2);//100.0
        }

    注意 compareTo的实现,先比较double,double比较还是相等(可能是精度区分不出来),转成long比较。

     public static int compare(double d1, double d2) {
            if (d1 < d2)
                return -1;           // Neither val is NaN, thisVal is smaller
            if (d1 > d2)
                return 1;            // Neither val is NaN, thisVal is larger
    
            // Cannot use doubleToRawLongBits because of possibility of NaNs.
            long thisBits    = Double.doubleToLongBits(d1);
            long anotherBits = Double.doubleToLongBits(d2);
    
            return (thisBits == anotherBits ?  0 : // Values are equal
                    (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
                     1));                          // (0.0, -0.0) or (NaN, !NaN)
        }
  • 相关阅读:
    poj 3176 三角数和最大问题 dp算法
    poj 2236 网络连接问题 并查集
    poj 3614 奶牛美容问题 优先队列
    得时无怠
    Android入门(一)
    javascript之apply和call
    写一份面试答卷
    拿来即用的loading效果
    关于回调的唠叨
    Nodejs Guides(四)
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/11165901.html
Copyright © 2011-2022 走看看