java double的精度丢失_Java中double类型精度丢失的问题

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

* in 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 exactly equal to

* 0.1, as one would expect. Therefore, it is generally

* recommended that the {@linkplain #BigDecimal(String)

* String 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之后进行计算,然后精度丢失。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值