简体   繁体   English

C上的浮点表示错误

[英]Floating-point representation error on C

So I want to verify the hexadecimal representation of the number 1.0 in Language C, below are my codes: 因此,我想验证语言C中数字1.0的十六进制表示形式,以下是我的代码:

int main(int argc, char **argv)
{
    void showBytes(unsigned char * p,int size){
        int i;
        for (i=0;i<size;i++){
            printf("%.2x",p[i]);
        }
    }

    float f;
    f=1.0;
    showBytes((unsigned char *)&f,sizeof(f));
    return 0;
}

When I build and run my program, I get 39300000, which is not the correct representation of 1.0(00003039). 当我构建并运行程序时,我得到39300000,这不是1.0(00003039)的正确表示。 Can any one explain to me why I get this incorrect value? 谁能向我解释为什么我得到这个不正确的值? Thanks!!! 谢谢!!!

This is an endianness mis-match. 这是字节顺序不匹配。 You are displaying the value using one endianness convention, but your expected value uses the other convention. 您正在使用一种字节序约定显示值,而您的期望值使用另一种字节序约定。

Endianness is a well-known issue for integer data types, but it is perhaps less well known endianness also affects floating point representation. 字节序是整数数据类型的一个众所周知的问题,但可能不那么知名的字节序也会影响浮点表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM