ava中浮点数的计算
今天在数值计算时碰到一个问题.程序以下:
double a = (3.3-2.4)/0.1;
System.out.println(a);
你可能认为结果很简单,不就是9嘛,是事实上,结果为:8.999999998,为何呢?我翻阅了一些资料,终于找出了缘由.
为何浮点数会丢失精度?
十进制数的二进制表示可能不够精确html
浮点数或是双精度浮点数没法精确表示的状况并很多见。浮点数值没办法用十进制来精确表示的缘由要归咎于CPU表示浮点数的方法。这样的话您就可能会牺牲一些精度,有些浮点数运算也会引入偏差。以上面提到的状况为例,2.4的二进制表示并不是就是精确的2.4。反而最为接近的二进制表示是 2.3999999999999999。缘由在于浮点数由两部分组成:指数和尾数。浮点数的值其实是由一个特定的数学公式计算获得的。您所遇到的精度损失会在任何操做系统和编程环境中遇到。
注意: 您可使用Binary Coded Decimal (BCD)库来保持精度。BCD数字编码方法会把每个十进制数字位单独编码。
类型失配
您可能混合了浮点数和双精度浮点数类型。请肯定您在进行数学运算的时候全部的数据类型所有相同。
注意:float类型的变量只有7位的精度,而double类型的变量有15位的精度。
如何进行浮点数精度计算?
Java中的简单浮点数类型float和double不可以进行运算。不光是Java,在其它不少编程语言中也有这样的问题。在大多数状况下,计算的结果是准确的,可是多试几回(能够作一个循环)就能够试出相似上面的错误。如今终于理解为何要有BCD码了。
这个问题至关严重,若是你有9.999999999999元,你的计算机是不会认为你能够购买10元的商品的。
在有的编程语言中提供了专门的货币类型来处理这种状况,可是Java没有。如今让咱们看看如何解决这个问题。
四舍五入
咱们的第一个反应是作四舍五入。Math类中的round方法不能设置保留几位小数,咱们只能象这样(保留两位):
public double round(double value){
return Math.round(value*100)/100.0;
}
很是不幸,上面的代码并不能正常工做,给这个方法传入4.015它将返回4.01而不是4.02,如咱们在上面看到的
4.015*100=401.49999999999994
所以若是咱们要作到精确的四舍五入,不能利用简单类型作任何运算
java.text.DecimalFormat也不能解决这个问题:
System.out.println(new java.text.DecimalFormat("0.00").format(4.025));
输出是4.02
BigDecimal
在《Effective Java》这本书中也提到这个原则,float和double只能用来作科学计算或者是工程计算,在商业计算中咱们要用java.math.BigDecimal。BigDecimal一共有4个够造方法,咱们不关心用BigInteger来够造的那两个,那么还有两个,它们是:
BigDecimal(double val)
Translates a double into a BigDecimal.
BigDecimal(String val)
Translates the String repre sentation of a BigDecimal into a BigDecimal.
上面的API简要描述至关的明确,并且一般状况下,上面的那一个使用起来要方便一些。咱们可能想都不想就用上了,会有什么问题呢?等到出了问题的时候,才发现上面哪一个够造方法的详细说明中有这么一段:
Note: the results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .1000000000000000055511151231257827021181583404541015625. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances nonwithstanding.
The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.
原来咱们若是须要精确计算,非要用String来够造BigDecimal不可!在《Effective Java》一书中的例子是用String来够造BigDecimal的,可是书上却没有强调这一点,这也许是一个小小的失误吧。
解决方案
如今咱们已经能够解决这个问题了,原则是使用BigDecimal而且必定要用String来够造。
可是想像一下吧,若是咱们要作一个加法运算,须要先将两个浮点数转为String,而后够形成BigDecimal,在其中一个上调用add方法,传入另 一个做为参数,而后把运算的结果(BigDecimal)再转换为浮点数。你可以忍受这么烦琐的过程吗?下面咱们提供一个工具类Arith来简化操做。它 提供如下静态方法,包括加减乘除和四舍五入:
public static double add(double v1,double v2)
public static double sub(double v1,double v2)
public static double mul(double v1,double v2)
public static double div(double v1,double v2)
public static double div(double v1,double v2,int scale)
public static double round(double v,int scale)
附录
源文件Arith.java:
import java.math.BigDecimal;
public class Arith{
//默认除法运算精度
private static final int DEF_DIV_SCALE = 10;
//这个类不能实例化
private Arith(){
}
public static double add(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2).doubleValue();
}
public static double sub(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue();
}
public static double mul(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue();
}
public static double div(double v1,double v2){
return div(v1,v2,DEF_DIV_SCALE);
}
public static double div(double v1,double v2,int scale){
if(scale<0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static double round(double v,int scale){
if(scale<0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
};java
针对double d = 2.4;
System.out.println(d);//输出2.4,却不是2.3999999999999999呢?
翻阅了一些资料,当单个输出double 型值时,能够正确的用十进制显示,具体为何,俺也似懂非懂,但进行浮点计算,浮点计算是指浮点数参与的运算,這種运算一般伴随着由于没法精确表示而进行的近似或舍入。也许和Double.toString方法的 FloatingDecimal(d).toJavaFormatString()有关系
猜想大概是2.3999999999999999超出了输出的精度因此被截取的缘由吧编程
摘自: http://blog.sina.com.cn/s/blog_68e4d2910100j0x8.html
app