2017-01-18 123 views
0

我的代碼是:for循環雙斯卡拉

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.900000000000
-1.800000000000
-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

你無法修復它。 https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems – Reactormonk

+0

我欣賞接受:-) – GhostCat

回答

2

你的期望是錯的。你認爲浮點數在計算機上的工作方式與「純數學」中的相同。

那麼,他們不會,看到here

所以,你要明白,你的循環構建自身

for(i <- -8.0 to 8.0 by 0.1) 

,你期望它不起作用。

+0

我不知道我同意這只是一個字符串格式問題。 –

+1

但事實並非如此。這是關於浮點的固有不準確性。 –

+0

「你需要什麼:舍入/ resp。格式化你的double值。」和「爲了得到預期的輸出,你必須把所有的值變成字符串」是錯誤的。繼續做一些本質上不準確的事情,並且在產出中進行篡改並不是正確的方式(顯然,IMO)。根據確切的值,像'for(i <-8.0到8.0 by 0.1)'可能會產生意想不到的元素數量(因爲預期的最終測試由於不準確而失敗),並且舍入/格式化/將double值轉換爲字符串將會解決這個問題。 –