double转16进制科学记数

double 5.6716进制浮点数

 

测试程序

/// @file topic1.c

/// @brief test on vs6 cl and link

/// 5.67 double值转16进制科学记数法

 

#include 

#include 

 

void clear_input_buffer();

 

int main(int argc, char* argv[])

{

    int iIndex = 0;

    double fIn = 5.67;

    /// 想验证下,进程中的变量地址对应的16进制浮点数的具体值

    /// 然后在手工运算这样就知道自己算得对不对

printf("&fIn = %08x, fIn = %f\r\n", &fIn, fIn);

    

    for (iIndex = 7; iIndex >= 0; iIndex--)

    {

        printf("%.2x ", (unsigned char)(*((unsigned char*)&fIn + iIndex)));

    }

    

    printf("\n");

    

    /** run result

    &fIn = 0012ff78, fIn = 5.670000

    40 16 ae 14 7a e1 47 ae

    */

 

    /// 40 16 ac 00 00 00 00 00 这是我算出来的 5.76 double, 精度是小数点后8

    /// 看看和电脑算的差多少.

    printf("please use winhex to modify fIn value, then press any key to continue\n");

    system("pause");

    printf("&fIn = %08x, fIn = %f\r\n", &fIn, fIn);

    

printf("END, press any key to quit\n");

getchar();

return 0;

}

 

void clear_input_buffer()

{

char ch = '\0';

/// scanf 的回车 0x0a, 留在了buffer里面getchar()还能读的到

/// clear input buffer

do

{

ch = getchar();

} while ((ch != EOF) && (ch != '\n'));

}

 

手工演算

/// * 5.67 double值转16进制科学记数法

5.67 = 5 + 0.67 = 101.10101011B = 1.0110101011B*10B^2

5 = 101B

0.67 = 10101011B

0.67*2 = 0.34 1

0.34*2 = 0.68 0

0.68*2 = 0.36 1

0.36*2 = 0.72 0

0.72*2 = 0.44 1

0.44*2 = 0.88 0

0.88*2 = 0.76 1

0.76*2 = 0.52 1

 

S = 0

E = 1023 + 2 = 1025 = 10000000001B

D = 0110101011 0000000000 0000000000 0000000000 0000000000 00

 

0 10000000001 0110101011 0000000000 0000000000 0000000000 0000000000 00

 

0100000000010110101011000000000000000000000000000000000000000000

 

0100,0000,0001,0110,1010,1100,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000

4    0   1    6   a    C   0    0   0   0    0    0    0   0   0   0

 

4 016a C0000000000

 

虽然和电脑算的(40 16 ae 14 7a e1 47 ae)不一样,但是我的精度只有小数点后82进制数我用WinHex将电脑算出的double值,换成我的看看和5.67差多少

 

/// 实验证明:我算的是对的

D:\ls\2015_1022\Topic1>call topic1.exe

&fIn = 0012ff78, fIn = 5.670000

40 16 ae 14 7a e1 47 ae

please use winhex to modify fIn value, then press any key to continue

请按任意键继续. . .

&fIn = 0012ff78, fIn = 5.667969

END, press any key to quit

你可能感兴趣的:(double转16进制科学记数)