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 doubleas 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 doublea synonym for double.
在MSDN上, 可以發現:
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 thelongdouble data type replaces the double data type. The long doubleversions of these functions should not be used in new code.
相信很多人學C語言時, 對long double的印象就是, 它能儲存精度比double更高的浮點數.但事實上並不完全是這樣.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 doubleas 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 doublea synonym for double.
在MSDN上, 可以發現:
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 thelongdouble 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位了. 儘管數學函式如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位元組(64位)