天天看點

IEEE-754标準,浮點0在計算機中的存儲方式

C++中double資料類型占64位,按IEEE-754的标準,1 位表示符号位,11位表示階碼,52位表示尾數。尾數實際能表示53位,最高位計算機預設存儲數值1。

浮點0的表示在計算機中每一位都是0,以下程式進行驗證。

//驗證浮點0的表示方式,IEEE-754标準,浮點0計算機存儲表示,64位全0。1位符号,11位階碼,52位尾數。

#include <iostream>

#include <bitset>

using namespace std;

int main()

{

 double x = 0;

 char * ptr;

    ptr = (char *)&x;

    for(int i = 0; i < 8; i++)

  cout << (int)ptr[i] << " ";        //驗證每個位元組的ASCII碼值,均為0,對應空字元'\0'。

 cout << endl;

 cout << ptr << endl;

    for(i = 0; i < 8; i++)

     cout << bitset<8>((int)(*ptr++)) << " ";   //輸出二進制位,共64位。

 cout << endl;

 return 0;

}

繼續閱讀