java里面double 做乘法和加法会丢失精度嘛?
除法和减法会不精确我知道,我也知道用java.math.BigDecimal可以保证不丢失精度,我只是想单纯的知道double做乘法和加法会丢失精度么?现在用的电脑上没有...
除法和减法会不精确我知道,我也知道用java.math.BigDecimal可以保证不丢失精度,我只是想单纯的知道double 做乘法和加法会丢失精度么?
现在用的电脑上没有ecplise,不能自己验证,求大牛帮忙解答。 展开
现在用的电脑上没有ecplise,不能自己验证,求大牛帮忙解答。 展开
2个回答
展开全部
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类的其中一个构造器的介绍。好好看,格式好像乱了。。传图一直传不上去,不造是为啥。。。。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-11-28
展开全部
个人觉得double做那运算足够了,你在控制台上输出double类型的10000/3这样的值,会发现java输出的值,小数点后至少是8位的了,如果这样还不够的话,那你做的是啥计算啊,需要那么精确的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询