java double 运算精度_java double 比较运算会精度丢失吗

展开全部

public BigDecimal(double val)

Translates a double into a BigDecimal which is the exact decimal representation of the double's binary

floating-point value. The scale of the returned BigDecimal is the

smallest value such that (10scale × val) is an integer.

Notes:

Parameters:

val - double value to be converted to

BigDecimal.

Throws:

NumberFormatException - if val is infinite or NaN.

The results of this constructor can be somewhat unpredictable. One might

assume that writing new BigDecimal(0.1) in Java creates a

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 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 String constructor, on the other hand, is perfectly

predictable: writing new BigDecimal("0.1") creates a

BigDecimal which is exactly equal to 0.1, as one would

expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.

When a double must be used as a source for a

BigDecimal, note that this constructor provides an exact

conversion; it does not give the same result as converting the

double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.

这是java.math.BigDecimal类的其中一个构造器的介绍。好好看,格e69da5e887aa3231313335323631343130323136353331333363393666式好像乱了。。传图一直传不上去,不造是为啥。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,float和double都是用二进制浮点数表示的,它们的精度是有限的。因为二进制浮点数无法精确表示某些十进制数,如0.1,所以在进行精确计算时需要特别注意。 在进行浮点数计算时,可以使用BigDecimal类来实现精确计算。BigDecimal类可以表示任意精度的十进制数,而且支持加、减、乘、除等基本的数学运算。 下面是一个使用BigDecimal类进行浮点数计算的例子: ``` import java.math.BigDecimal; public class FloatDoublePrecision { public static void main(String[] args) { float f1 = 0.1f; float f2 = 0.2f; double d1 = 0.1; double d2 = 0.2; BigDecimal b1 = new BigDecimal(Float.toString(f1)); BigDecimal b2 = new BigDecimal(Float.toString(f2)); BigDecimal b3 = new BigDecimal(Double.toString(d1)); BigDecimal b4 = new BigDecimal(Double.toString(d2)); BigDecimal result1 = b1.add(b2); BigDecimal result2 = b3.add(b4); System.out.println("Float计算结果:" + result1); System.out.println("Double计算结果:" + result2); } } ``` 输出结果如下: ``` Float计算结果:0.300000011920928955078125 Double计算结果:0.3000000000000000444089209850062616169452667236328125 ``` 可以看到,使用float和double进行计算得到的结果都存在精度问题。而使用BigDecimal类进行计算可以得到精确的结果。 需要注意的是,使用BigDecimal类进行计算时需要使用字符串形式的构造方法,而不能直接使用浮点数进行构造,否则仍然存在精度问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值