首页 > Java中double类型精度丢失的问题

Java中double类型精度丢失的问题

为什么使用add2方法的结果是错误的?Double.toString(d1)为什么能保持double的精度不丢失?

    public static double add(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.add(bd2).doubleValue();
    }
    
    public static double add2(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(d1);
        BigDecimal bd2 = new BigDecimal(d2);
        return bd1.add(bd2).doubleValue();
    }

    public static void main(String[] args) {
        double d1 = 1.0000002;
        double d2 = 0.0000002;
        System.out.println(Double.toString(d1));
        System.out.println("d1 + d2 = " + (d1 + d2)); // 1.0000003999999998
        System.out.println("d1 + d2 = " + add(d1, d2)); // 1.0000004
        System.out.println("d1 + d2 = " + add2(d1, d2)); // 1.0000003999999998
    }

PS:引出一个概念问题:

double d = XXXX.XXXXXX;
System.out.println(d);    //这里总是不会丢失精度的,为什么?

BigDecimal在用double做入参的时候,二进制无法精确地表示十进制小数,编译器读到字符串"0.0000002"和“1.0000002”之后,必须把它转成8个字节的double值,也就是1.0000001999999998947288304407265968620777130126953125类似这种。
所以,运行的时候,实际传给BigDecimal构造函数的真正的数值是1.0000001999999998947288304407265968620777130126953125
BigDecimal在用String做入参的时候,能够正确地把字符串转化成真正精确的浮点数。

System.out.println部分,如果入参是string,那么直接输出,如果入参是其他类型,那么会调用Object.toString方法进行转化之后进行输出。而Double.toString会使用一定的精度来四舍五入double,然后再输出。

BigDecimal构造方法上的注释就写了这个问题:

 The results of this constructor can be somewhat unpredictable.
     * One might assume that writing {@code new BigDecimal(0.1)} in
     * Java creates a {@code BigDecimal} which is exactly equal to
     * 0.1 (an unscaled value of 1, with a scale of 1), but it is
     * actually equal to
     * 0.1000000000000000055511151231257827021181583404541015625.
     * This is because 0.1 cannot be represented exactly as a
     * {@code double} (or, for that matter, as a binary fraction of
     * any finite length).  Thus, the value that is being passed
     * <i>in</i> to the constructor is not exactly equal to 0.1,
     * appearances notwithstanding.
     
     The {@code String} constructor, on the other hand, is
 * perfectly predictable: writing {@code new BigDecimal("0.1")}
 * creates a {@code BigDecimal} which is <i>exactly</i> equal to
 * 0.1, as one would expect.  Therefore, it is generally
 * recommended that the {@linkplain #BigDecimal(String)
 * <tt>String</tt> constructor} be used in preference to this one.
 
 

补充说明一下:
Double.toString这个方法输出的是一个String,而且会进行四舍五入处理。
new BigDecimal(Double.toString(d1))这个入参在处理完毕之后是一个String,调用的是BigDecimal(String val)这个构造方法。
源码里BigDecimal(String val)这个方法是会将val处理成char[]数组:

this(val.toCharArray(), 0, val.length());

然后调用BigDecimal(char[] in)这个构造方法。

而new BigDecimal(d1)调用的是 BigDecimal(double val),这个方法进来之后第一件事就是

long valBits = Double.doubleToLongBits(val); 

把入参转换成二进制,所以会造成精度丢失。

double相加造成的精度丢失和上面的情况一样,先转成bit之后进行计算,然后精度丢失。。

【热门文章】
【热门文章】