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;
}




 

posted @ 2021-12-02 15:55  lpyg  阅读(72)  评论(0编辑  收藏  举报