1. 程式人生 > 其它 >C++檢視 IEEE 754 浮點數格式的程式碼

C++檢視 IEEE 754 浮點數格式的程式碼

把內容過程中較好的一些內容片段備份一次,下邊資料是關於C++檢視 IEEE 754 浮點數格式的內容。

for binary floating-point numbers (IEEE 754) is to use a union, as shown in the following example:

#include <iostream>
#include <basetsd.h>
#include <iomanip>
#include <cstdlib>
using namespace std;

{
}fn;

union DoubleNum
{
}dn;

union LongDoubleNum
{
}ldn;

int main()
{
cout << "nsize of float = " << dec << sizeof(fn.fx) << endl;
cout << setprecision(10) << fn.fx << " = 0x" << hex << fn.lx << endl;

cout << "nsize of double = " << dec << sizeof(dn.dx) << endl;
cout << dn.dx <<" = 0x" << hex << dn.lx << endl;

cout << "nsize of long double = " << dec << sizeof(ldn.dx) << endl;
cout << setprecision(10) << ldn.dx << " = 0x" << hex << ldn.lx[2] << ldn.lx[1] << ldn.lx[0] << endl;
return 0;
}