当前位置: 技术问答>java相关
double支持多少位的,为什么运算出错呢?该如何解决呢
来源: 互联网 发布时间:2015-11-14
本文导语: 看这个例子 --------------- package test; public class FloatTest { public FloatTest() { double qty1=1.999999999999999005; double qty2=0.001; double difQty=qty1-qty2; double addQty=difQty+qty2; //System.out.println("qty1...
看这个例子
---------------
package test;
public class FloatTest
{
public FloatTest()
{
double qty1=1.999999999999999005;
double qty2=0.001;
double difQty=qty1-qty2;
double addQty=difQty+qty2;
//System.out.println("qty1 is:1.1234567890123");
System.out.println("qty1 is:"+qty1);
System.out.println("qty2 is "+qty2);
//System.out.println("difQty is:1.1234567890123");
System.out.println("difQty is:"+difQty);
System.out.println("addQty is "+addQty);
}
public static void main(String[] args)
{
FloatTest floatTest1 = new FloatTest();
}
}
执行的结果是:
qty1 is:1.9999999999999991
qty2 is 0.0010
difQty is:1.9989999999999992
addQty is 1.9999999999999991
---------------
package test;
public class FloatTest
{
public FloatTest()
{
double qty1=1.999999999999999005;
double qty2=0.001;
double difQty=qty1-qty2;
double addQty=difQty+qty2;
//System.out.println("qty1 is:1.1234567890123");
System.out.println("qty1 is:"+qty1);
System.out.println("qty2 is "+qty2);
//System.out.println("difQty is:1.1234567890123");
System.out.println("difQty is:"+difQty);
System.out.println("addQty is "+addQty);
}
public static void main(String[] args)
{
FloatTest floatTest1 = new FloatTest();
}
}
执行的结果是:
qty1 is:1.9999999999999991
qty2 is 0.0010
difQty is:1.9989999999999992
addQty is 1.9999999999999991
|
没有查到java中关于double 型的精确度的定义,但我好像记得,关于计算机中浮点数的精确度是于系统有关的,就好像家用电脑在浮点数的精确度上是不能与巨型计算机比的。而且对浮点数的精确度所能达到的程度,是衡量一台机器的性能的一个重要的指标
|
float是32位的,double是64位的
double
-1.79769313486231570E+308 to +1.79769313486231570E+308
double
-1.79769313486231570E+308 to +1.79769313486231570E+308