2011-10-25 85 views
1

當我使用long double時,我的精度比使用double差。 3.14159265358979323846264L在源代碼中編寫long double const是否好用,或者我應該添加L以外的內容?精準雙倍差於雙倍

編輯 我解決了這個問題。我更改算法更準確。

+0

什麼平臺/架構/編譯器?你實際觀察到了什麼? – tenfour

+0

不,「L」是正確的。 – trojanfoe

+4

你將需要更多地解釋你自己。向我們展示一些使用double的代碼,一些使用long double的代碼,每個的結果以及爲什麼你認爲結果是錯誤的。 –

回答

5

您的精度並沒有變差。

發生了什麼事情是,當您將數字打印出來時,流庫將截斷顯示的值。使用std :: setprecision來獲得特定的精度。

double  x = 1.0/3; 
long double y = 1.0/6; 

// Prints out the precision 
std::cout << "Limit: " << std::numeric_limits<double>::digits10 << "\n"; 
std::cout << "Limit: " << std::numeric_limits<long double>::digits10 << "\n"; 

// prints the numbers with max precision. 
std::cout << std::setprecision(std::numeric_limits<double>::digits10) << x << "\n"; 
std::cout << std::setprecision(std::numeric_limits<long double>::digits10) << y << "\n";