public class MathOps{
public static void main(String[] args){
double q,a,p;
q=0.613979739737064
a=0.5714594790600264
p = a+q;
System.out.println(p);
float u; 
float v=0.39938408f;
float w=0.6350466f;
u = v+w;
System.out.println(u);
}
}
结果是:1.1854392187970904
       1.0344307;
谁能解释一下结果,我试了很多次,发现float的精度最高为8位,double为17位,难道在java中float 形的精度就是8位,double形的就是17位?请高手来解释一下,谢谢.

解决方案 »

  1.   

    ^_^,不好意思,定点小数和浮点小数怎么区分我已经明白了,好白痴的问题!
    呵呵!如果分不够,我可以再加的! 
    定点数的最大值不是1-2的-(n-1)次房吗.哪float能表示的最大值不是1-2的-31此方.怎么java里面只能表示这么几位?
      

  2.   

    先申明一下:我不是高手,帮你顶一下
    我和你有同感:在java中float 形的精度就是8位,double形的就是17位
      

  3.   

    Type   Storage Requirement       Range
     
    float  4 bytes    approximately ±3.40282347E+38F (6–7 significant decimal digits)
      
    double 8 bytes   approximately ±1.79769313486231570E+308 (15 significant decimal digits)
    在 core java 2 volume I 中有提到,这个精度是遵循工业标准的,没有理由,double占用64bit只是意味着它的宽度.
    引用:
    Floating-point numbers are not suitable for financial calculation where roundoff errors cannot be tolerated. For example, the command System.out.println(2.0 - 1.1) prints 0.8999999999999999, not 0.9 as you would expect. Such roundoff errors are caused by the fact that floating-point numbers are represented in the binary number system. There is no precise binary representation of the fraction 1/10, just as there is no accurate representation of the fraction 1/3 in the decimal system. If you need precise numerical computations without roundoff errors, you need to use the BigDecimal class 
      

  4.   

    那照上面的说法,float的精度默认是6-7位,double是15位,可是我得出来的是8位,与17位呀?还有宽度是什么意思?
      

  5.   

    请参见API里Float.intbitToFloat()方法里对 float类型的详细介绍,float的32位中,最高位符号位,接下来8位(可能是7位,不大记住)是科学计数法里的E的幂指数值.余下的位才用来记录标准数据及小数点.解释得可能不清楚,但大致思路是这样,请大家参考这思路再查相关文档