我的代码是:
for(i <- -8.0 to 8.0 by 0.1) println(i)
输出是:
-8.0
-7.9
-7.800000000000001
-7.700000000000001
-7.600000000000001
-7.500000000000002
-7.400000000000002
-7.3000000000000025
-7.200000000000003
-7.100000000000003
-7.0000000000000036
-6.900000000000004
-6.800000000000004
-6.700000000000005
-6.600000000000005
-6.500000000000005
-6.400000000000006
-6.300000000000006
-6.200000000000006
-6.100000000000007
-6.000000000000007
-5.9000000000000075
-5.800000000000008
-5.700000000000008
-5.6000000000000085
-5.500000000000009
-5.400000000000009
-5.30000000000001
-5.20000000000001
-5.10000000000001
-5.000000000000011
-4.900000000000011
-4.800000000000011
-4.700000000000012
-4.600000000000012
-4.500000000000012
-4.400000000000013
-4.300000000000013
-4.2000000000000135
-4.100000000000014
-4.000000000000014
-3.900000000000014
-3.800000000000014
-3.700000000000014
-3.600000000000014
-3.5000000000000138
-3.4000000000000137
-3.3000000000000136
-3.2000000000000135
-3.1000000000000134
-3.0000000000000133
-2.9000000000000132
-2.800000000000013
-2.700000000000013
-2.600000000000013
-2.500000000000013
-2.400000000000013
-2.3000000000000127
-2.2000000000000126
-2.1000000000000125
-2.0000000000000124
-1.9000000000000123
-1.8000000000000123
-1.7000000000000122
-1.600000000000012
-1.500000000000012
-1.400000000000012
-1.3000000000000118
-1.2000000000000117
-1.1000000000000116
-1.0000000000000115
-0.9000000000000116
-0.8000000000000116
-0.7000000000000116
-0.6000000000000116
-0.5000000000000117
-0.4000000000000117
-0.3000000000000117
-0.2000000000000117
-0.10000000000001169
-1.1685097334179773E-14
0.09999999999998832
0.19999999999998833
0.29999999999998833
0.39999999999998836
0.49999999999998834
0.5999999999999883
0.6999999999999883
0.7999999999999883
0.8999999999999883
0.9999999999999882
1.0999999999999883
1.1999999999999884
1.2999999999999885
1.3999999999999886
1.4999999999999887
1.5999999999999888
1.6999999999999889
1.799999999999989
1.899999999999989
1.9999999999999891
2.099999999999989
2.199999999999989
2.299999999999989
2.3999999999999893
2.4999999999999893
2.5999999999999894
2.6999999999999895
2.7999999999999896
2.8999999999999897
2.99999999999999
3.09999999999999
3.19999999999999
3.29999999999999
3.39999999999999
3.4999999999999902
3.5999999999999903
3.6999999999999904
3.7999999999999905
3.8999999999999906
3.9999999999999907
4.099999999999991
4.19999999999999
4.29999999999999
4.39999999999999
4.499999999999989
4.599999999999989
4.699999999999989
4.799999999999988
4.899999999999988
4.999999999999988
5.099999999999987
5.199999999999987
5.2999999999999865
5.399999999999986
5.499999999999986
5.599999999999985
5.699999999999985
5.799999999999985
5.899999999999984
5.999999999999984
6.099999999999984
6.199999999999983
6.299999999999983
6.399999999999983
6.499999999999982
6.599999999999982
6.6999999999999815
6.799999999999981
6.899999999999981
6.9999999999999805
7.09999999999998
7.19999999999998
7.299999999999979
7.399999999999979
7.499999999999979
7.599999999999978
7.699999999999978
7.799999999999978
7.899999999999977
7.999999999999977
我希望结果如下:
(-8.0, -7.9, -7.8, -7.7......., 7.7, 7.8, 7.9, 8.0)
我该如何解决这个问题?
答案 0 :(得分:2)
你的期望是错误的。您假设浮点数在计算机中的工作方式与它们在纯数学中的工作方式相同,并且#34;。
嗯,他们没有,请参阅here。
所以你必须明白你的循环构造本身
user2.com
不会像你期望的那样发挥作用。