2012-03-12 107 views
1

可能重複:
Double precision problems on .NET
Double calculation producing odd result不可預知的雙

我知道這雙價值0.2的內部represantation有點像0.199999。但是下面的代碼仍然讓我困惑。

CODE:

public static void main(String[] args) { 
    double d= 0.3d; 
    double f= 0.1d; 
    System.out.println(d+f); 
    System.out.println(d*f); 
    System.out.println(d); 
    System.out.println(f); 
    System.out.println(d-f); 
    System.out.println(d/f); 
    System.out.println((d-f)*(d-f)); 
} 

OUTPUT:

0.4 
0.03 
0.3 
0.1 
0.19999999999999998 
2.9999999999999996 
0.039999999999999994 

什麼是真正回事?此外,乘法運行良好,但減法,除法不是。任何人都可以請詳細說明爲什麼加法不同於減法

+10

必須成爲最受關注的問題之一併回答了計算中的問題。 :P – 2012-03-12 12:51:28

+1

...和Stackoverflow – Thilo 2012-03-12 12:52:25

+6

看來你的問題的第一句話就是答案! – assylias 2012-03-12 12:53:28

回答

4

簡短的回答是你有浮點運算的表示錯誤和舍入錯誤。 toString()「知道」表示錯誤,所以如果沒有舍入錯誤,你就不會看到它。但是,如果舍入誤差太大,你就會這樣做。

解決方法是使用BigDecimal或舍入結果。


如果您使用BigDecimal,它將顯示您確實具有的確切值。

double d = 0.3d; 
double f = 0.1d; 
System.out.println("d= " + new BigDecimal(d)); 
System.out.println("f= " + new BigDecimal(f)); 
System.out.println("d+f= " + new BigDecimal(d + f)); 
System.out.println("0.4= " + new BigDecimal(0.4)); 
System.out.println("d*f= " + new BigDecimal(d * f)); 
System.out.println("0.03= " + new BigDecimal(0.03)); 
System.out.println("d-f= " + new BigDecimal(d - f)); 
System.out.println("0.2= " + new BigDecimal(0.2)); 
System.out.println("d/f= " + new BigDecimal(d/f)); 
System.out.println("(d-f)*(d-f)= " + new BigDecimal((d - f) * (d - f))); 

打印

d= 0.299999999999999988897769753748434595763683319091796875 
f= 0.1000000000000000055511151231257827021181583404541015625 
d+f= 0.40000000000000002220446049250313080847263336181640625 
0.4= 0.40000000000000002220446049250313080847263336181640625 
d*f= 0.0299999999999999988897769753748434595763683319091796875 
0.03= 0.0299999999999999988897769753748434595763683319091796875 
d-f= 0.1999999999999999833466546306226518936455249786376953125 
0.2= 0.200000000000000011102230246251565404236316680908203125 
d/f= 2.999999999999999555910790149937383830547332763671875 
(d-f)*(d-f)= 0.03999999999999999389377336456163902767002582550048828125 

你會發現,0.1稍微過大,0.3稍微偏小。這意味着當你添加或乘以它們時,你會得到一個正確的數字。但是,如果您使用減法或除法,那麼這些誤差會累積起來,並且您會得到一個距離所代表數字太遠的數字。

也就是說,您可以看到0.1和0.3的結果與0.4的值相同,而0.3 - 0.1的結果與0的值不同。2


BTW圓答案,而無需使用BigDecimal的,你可以使用

System.out.printf("d-f= %.2f%n", d - f); 
System.out.printf("d/f= %.2f%n", d/f); 
System.out.printf("(d-f)*(d-f)= %.2f%n", (d - f) * (d - f)); 

打印

d-f= 0.20 
d/f= 3.00 
(d-f)*(d-f)= 0.04 

System.out.println("d-f= " + roundTo6Places(d - f)); 
System.out.println("d/f= " + roundTo6Places(d/f)); 
System.out.println("(d-f)*(d-f)= " + roundTo6Places((d - f) * (d - f))); 

public static double roundTo6Places(double d) { 
    return (long)(d * 1e6 + (d > 0 ? 0.5 : -0.5))/1e6; 
} 

打印

System.out.println("d-f= " + roundTo6Places(d - f)); 
System.out.println("d/f= " + roundTo6Places(d/f)); 
System.out.println("(d-f)*(d-f)= " + roundTo6Places((d - f) * (d - f))); 

舍入移除的舍入誤差(留下其中的toString被設計爲僅處理所述表示錯誤)


可前和0.1後表示該值可以作爲

double before_f = Double.longBitsToDouble(Double.doubleToLongBits(f) - 1); 
System.out.println("The value before 0.1 is " + new BigDecimal(before_f) + " error= " + BigDecimal.valueOf(0.1).subtract(new BigDecimal(before_f))); 
System.out.println("The value after 0.1 is " + new BigDecimal(f) + " error= " + new BigDecimal(f).subtract(BigDecimal.valueOf(0.1))); 
來計算

打印

The value before 0.1 is 0.09999999999999999167332731531132594682276248931884765625 
    error= 8.32667268468867405317723751068115234375E-18 
The value after 0.1 is 0.1000000000000000055511151231257827021181583404541015625 
    error= 5.5511151231257827021181583404541015625E-18 
+0

非常感謝:)爲什麼0.1有更多和0.3的實際少? – Ahamed 2012-03-12 13:09:56

+0

'0.1'稍微多一些,因爲這是最接近的可表示的值。對於'0.3',最接近的可表示值略小。 OMG! – 2012-03-12 13:23:47

+0

OMG!爲什麼是這樣的:(最接近的實際是什麼意思? – Ahamed 2012-03-13 05:44:29

5

如果你絕望的精確使用BigDecimal。

public static void main(String[] args) { 
    BigDecimal d = BigDecimal.valueOf(0.3d); 
    BigDecimal f = BigDecimal.valueOf(0.1d); 
    System.out.println(d.add(f)); 
    System.out.println(d.multiply(f)); 
    System.out.println(d); 
    System.out.println(f); 
    System.out.println(d.subtract(f)); 
    System.out.println(d.divide(f)); 
    System.out.println((d.subtract(f)).multiply(d.subtract(f))); 
} 

輸出

0.4 
0.03 
0.3 
0.1 
0.2 
3 
0.04 

或圓你的結果,DecimalFormat的做到這一點很好地使用#符號意義只顯示必要小數

double d = 0.3d; 
    double f = 0.1d; 
    DecimalFormat format = new DecimalFormat("#.##"); 
    System.out.println(format.format(d + f)); 
    System.out.println(format.format(d * f)); 
    System.out.println(format.format(d)); 
    System.out.println(format.format(f)); 
    System.out.println(format.format(d - f)); 
    System.out.println(format.format(d/f)); 
    System.out.println(format.format((d - f) * (d - f))); 

輸出

0.4 
0.03 
0.3 
0.1 
0.2 
3 
0.04