java浮点类型的结果精度

wangyue_1 发布于 2015/05/28 17:24
阅读 308
收藏 1

float[] fls = {0.50f,0.50f,0.5f,1f,1f,0.5f,0.5f,0.5f,0.0f,0.5f,0.5f,2.0f,2.0f,1.0f,2.00f,1.00f,1.00f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f,0.1f};
        
        float result = 0f;
        for (float f : fls) {
            result += f ;
        }
        System.out.println(result);



显示结果是15.900003??????这是为什么




加载中
0
如比如比
如比如比
0.0的二进制存储是[0][00000000][00000000000000000000000]=>1×2的-126次方×0.0=0.0
0.5的二进制存储是[0][01111110][00000000000000000000000]=>1×2的-1次方×1.0=0.5
1.0的二进制存储是[0][01111111][00000000000000000000000]=>1×2的0次方×1.0=1.0
1.5的二进制存储是[0][01111111][10000000000000000000000]=>1×2的0次方×1.5=1.5
2.5的二进制存储是[0][10000000][01000000000000000000000]=>1×2的1次方×1.25=2.5
3.5的二进制存储是[0][10000000][11000000000000000000000]=>1×2的1次方×1.75=3.5
4.0的二进制存储是[0][10000001][00000000000000000000000]=>1×2的2次方×1.0=4.0
4.5的二进制存储是[0][10000001][00100000000000000000000]=>1×2的2次方×1.125=4.5
5.0的二进制存储是[0][10000001][01000000000000000000000]=>1×2的2次方×1.25=5.0
5.0的二进制存储是[0][10000001][01000000000000000000000]=>1×2的2次方×1.25=5.0
5.5的二进制存储是[0][10000001][01100000000000000000000]=>1×2的2次方×1.375=5.5
6.0的二进制存储是[0][10000001][10000000000000000000000]=>1×2的2次方×1.5=6.0
8.0的二进制存储是[0][10000010][00000000000000000000000]=>1×2的3次方×1.0=8.0
10.0的二进制存储是[0][10000010][01000000000000000000000]=>1×2的3次方×1.25=10.0
11.0的二进制存储是[0][10000010][01100000000000000000000]=>1×2的3次方×1.375=11.0
13.0的二进制存储是[0][10000010][10100000000000000000000]=>1×2的3次方×1.625=13.0
14.0的二进制存储是[0][10000010][11000000000000000000000]=>1×2的3次方×1.75=14.0
15.0的二进制存储是[0][10000010][11100000000000000000000]=>1×2的3次方×1.875=15.0
15.1的二进制存储是[0][10000010][11100011001100110011010]=>1×2的3次方×1.8875000476837158=15.100000381469727
15.200001的二进制存储是[0][10000010][11100110011001100110100]=>1×2的3次方×1.9000000953674316=15.200000762939453
15.300001的二进制存储是[0][10000010][11101001100110011001110]=>1×2的3次方×1.9125001430511475=15.30000114440918
15.400002的二进制存储是[0][10000010][11101100110011001101000]=>1×2的3次方×1.9250001907348633=15.400001525878906
15.500002的二进制存储是[0][10000010][11110000000000000000010]=>1×2的3次方×1.937500238418579=15.500001907348633
15.600002的二进制存储是[0][10000010][11110011001100110011100]=>1×2的3次方×1.950000286102295=15.60000228881836
15.700003的二进制存储是[0][10000010][11110110011001100110110]=>1×2的3次方×1.9625003337860107=15.700002670288086
15.800003的二进制存储是[0][10000010][11111001100110011010000]=>1×2的3次方×1.9750003814697266=15.800003051757812
15.900003的二进制存储是[0][10000010][11111100110011001101010]=>1×2的3次方×1.9875004291534424=15.900003433227539


http://my.oschina.net/u/660460/blog/425019




























2
阳光test
阳光test
你可以去搜一下IEEE的浮点数标准,因为浮点数是:符号位 底数 * 2的指数幂这种,所以用它来做运算肯定拿不到准确的结果的。
1
霍啸林
霍啸林
个人理解,浮点数在内存中存了个近似值
1
返回顶部
顶部