long double可不一定精度比double高♂喲


相信很多人學C語言時, 對long double的印象就是, 它能存儲精度比double更高的浮點數(shù).
但事實上并不完全是這樣.
C98的標準是: double類型的值是long double的子集

C++ 98 standard:
and the type long double provides at least as much precision as double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double.
也就是說,long double只是定義為至少跟double一樣精度(即是可以一樣)

在wiki上的long double上找到:

On the x86 architecture, most C compilers implement long double
as the 80-bit extended precision type supported by x86 hardware (sometimes stored as 12 or 16 bytes to maintain data structure alignment), as specified in the C99 / C11 standards (IEC 60559 floating-point arithmetic (Annex F)).

An exception is Microsoft Visual C++ for x86, which makes long double
a synonym for double
.

在MSDN上, 可以發(fā)現(xiàn):

Previous 16-bit versions of Microsoft C/C++ and Microsoft Visual C++ supported the long double, 80-bit precision data type. In Win32 programming, however, the long double data type maps to the double, 64-bit precision data type. The Microsoft run-time library provides long double versions of the math functions only for backward compatibility. The long double function prototypes are identical to the prototypes for their double counterparts, except that the long double data type replaces the double data type. The long doubleversions of these functions should not be used in new code.

早期windows 16位是支持long double類型(80位), 可是在Win32編程中, double跟long double等價了, 都是64位了. 盡管數(shù)學函數(shù)如sin, cos等仍然保留long double類型,可是這些僅僅用來保持向后兼容(windows為了向后兼容付出了很多的..還有dll地獄一說)
也就是說, 在32位中, long double 跟 double等價, 精度也是一樣的.

我在自己機器上測試,

cout << "sizeof(double) = " << sizeof(double) << endl;
cout << "sizeof(long double) = " << sizeof(long double) << endl;

輸出都是8字節(jié)(64位)

這是contribute to Wine 時遇到的問題,將嘗試解答的email整理補充而來。感謝Hong Qian一直以來的熱情幫助~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容