C++查看 IEEE 754 浮点数格式的代码

293 阅读1分钟
把内容过程中较好的一些内容片段备份一次,下边资料是关于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;
}