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)
        }
  • 相关阅读:
    sql-select for update
    java-JDK动态代理
    idea-热部署jreble的使用
    vue-【el-table】转成【pl-table】
    mybatis-字段值为null或为''无法存储到数据库
    vue-本地开发热部署编译缓慢
    chrome-截长图
    el-cascader 级联选择器中选中任意一级去掉圆形按钮
    idea-绿色注释颜色16进制
    markdown 语法
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/11165901.html
Copyright © 2011-2022 走看看