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)
        }
  • 相关阅读:
    set的使用
    this.$watch(),this.$set(),this.$nextTick()=>{})
    web 错误代码解析
    samba 问题解决
    Linux postfix配置方法
    Linux rhcsa认证考试试题模拟
    Linux 使用nmcli配置网络
    Linux 链路聚合
    Linux ISCSI服务配置
    Linux Apache配置https访问
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/11165901.html
Copyright © 2011-2022 走看看