关于 java 中 Double 型 用于计算产生的误差举例 ,应该用BigDecimal代替Double

乘法

use             Double                                            BigDecimal

1.4×1.5=  2.0999999999999996                   2.10
1.4×1.6=  2.2399999999999998                   2.24
1.4×1.9=  2.6599999999999997                   2.66

1.4×2.3=  3.2199999999999998                   3.22
1.5×1.6=  2.4000000000000004                   2.40

1.5×1.9=  2.8499999999999996                   2.85

1.5×2.1=  3.1500000000000004                   3.15
1.5×2.2=  3.3000000000000003                   3.30
1.5×2.3=  3.4499999999999997                   3.45

1.6×1.8=  2.8800000000000003                   2.88

1.6×2.1=  3.3600000000000003                   3.36
1.6×2.2=  3.5200000000000005                   3.52
1.6×2.3=  3.6799999999999997                   3.68

1.7×2.3=  3.9099999999999997                   3.91

1.8×2.1=  3.7800000000000002                   3.78
1.8×2.2=  3.9600000000000004                   3.96

1.9×2.1=  3.9899999999999998                   3.99

1.9×2.3=  4.369999999999999                    4.37

2.1×2.2=  4.620000000000001                    4.62

 

除法

4.2/1.4=  3.0000000000000004                     3.0

4.8/1.6=  2.9999999999999996                     3.0

5.7/1.9=  3.0000000000000004                     3.0

6.6/2.2=  2.9999999999999996                     3.0
6.9/2.3=  3.0000000000000004                     3.0

4.2-1.4=  2.8000000000000003                   2.8

4.8-1.6=  3.1999999999999997                   3.2
5.1-1.7=  3.3999999999999995                   3.4
5.4-1.8=  3.6000000000000005                   3.6
5.7-1.9=  3.8000000000000003                   3.8

6.3-2.1=  4.199999999999999                    4.2
6.6-2.2=  4.3999999999999995                   4.4
6.9-2.3=  4.6000000000000005                   4.6

 

[1.4] 累加                                      
1次: 1.4                                                            1.4                        
2次: 2.8                                                            2.8                        
3次: 4.199999999999999                           4.2                        
4次: 5.6                                                            5.6                        
5次: 7.0                                                            7.0                        
6次: 8.4                                                            8.4                        
7次: 9.8                                                            9.8                        
8次: 11.200000000000001                         11.2                       
9次: 12.600000000000001                         12.6                       
10次: 14.000000000000002                       14.0                        
              
[1.6]累加。                                      
1次: 1.6                                                           1.6                        
2次: 3.2                                                           3.2                        
3次: 4.800000000000001                          4.8                        
4次: 6.4                                                           6.4                        
5次: 8.0                                                           8.0                        
6次: 9.6                                                           9.6                        
7次: 11.2                                                         11.2                       
8次: 12.799999999999999                        12.8                       
9次: 14.399999999999999                        14.4                       
10次: 15.999999999999998                      16.0                       
[1.7]累加。                                       
1次: 1.7                                                            1.7                        
2次: 3.4                                                            3.4                        
3次: 5.1                                                            5.1                        
4次: 6.8                                                            6.8                        
5次: 8.5                                                            8.5                        
6次: 10.2                                                         10.2                       
7次: 11.899999999999999                         11.9                       
8次: 13.599999999999998                         13.6                       
9次: 15.299999999999997                         15.3                       
10次: 16.999999999999996                        17.0                       
[1.8]累加。                                  
1次: 1.8                                        1.8                        
2次: 3.6                                        3.6                        
3次: 5.4                                        5.4                        
4次: 7.2                                        7.2                        
5次: 9.0                                        9.0                        
6次: 10.8                                       10.8                       
7次: 12.600000000000001                         12.6                       
8次: 14.400000000000002                         14.4                       
9次: 16.200000000000003                         16.2                       
10次: 18.000000000000004                         18.0                       
[1.9]累加。                                      
1次: 1.9                                        1.9                        
2次: 3.8                                        3.8                        
3次: 5.699999999999999                          5.7                        
4次: 7.6                                        7.6                        
5次: 9.5                                        9.5                        
6次: 11.4                                       11.4                       
7次: 13.3                                       13.3                       
8次: 15.200000000000001                         15.2                       
9次: 17.1                                       17.1                       
10次: 19.0                                       19.0                        
               
[2.1]累加。                                      
1次: 2.1                                        2.1                        
2次: 4.2                                        4.2                        
3次: 6.300000000000001                          6.3                        
4次: 8.4                                        8.4                        
5次: 10.5                                       10.5                       
6次: 12.6                                       12.6                       
7次: 14.7                                       14.7                       
8次: 16.8                                       16.8                       
9次: 18.900000000000002                         18.9                       
10次: 21.000000000000004                         21.0                       
[2.2]累加。                                       
1次: 2.2                                        2.2                        
2次: 4.4                                        4.4                        
3次: 6.6000000000000005                         6.6                        
4次: 8.8                                        8.8                        
5次: 11.0                                       11.0                       
6次: 13.2                                       13.2                       
7次: 15.399999999999999                         15.4                       
8次: 17.599999999999998                         17.6                       
9次: 19.799999999999997                         19.8                       
10次: 21.999999999999996                         22.0                       
[2.3]累加。                                        
1次: 2.3                                        2.3                        
2次: 4.6                                        4.6                        
3次: 6.8999999999999995                         6.9                        
4次: 9.2                                        9.2                        
5次: 11.5                                       11.5                       
6次: 13.8                                       13.8                       
7次: 16.1                                       16.1                       
8次: 18.400000000000002                         18.4                       
9次: 20.700000000000003                         20.7                       
10次: 23.000000000000004                         23.0                       

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页