我的浮动值与C中的值不匹配

Arn*_*aud 4 c floating-point

我正在尝试将木板与覆盆子连接。我必须通过modbus来将值读/写到板上,但是不能像板一样写浮点值。

我正在使用C和Eclipse调试透视图来直接查看变量的值。董事会寄给我0x46C35000至12月应值25'000,但日食显示我1.18720512e + 009 ...

当我在此网站上尝试http://www.binaryconvert.com/convert_float.html?hexadecimal=46C35000时,我得到25,000。

有什么问题 ?出于测试目的,我正在使用:

int main(){

    while(1){ // To view easily the value in the debug perspective 
        float test = 0x46C35000;
        printf("%f\n",test);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

谢谢 !

dbu*_*ush 6

执行此操作时:

float test = 0x46C35000;
Run Code Online (Sandbox Code Playgroud)

您将设置为0x46C35000(十进制1187205120),而不是表示形式

您可以执行以下操作:

union {
    uint32_t i;
    float f;
} u = { 0x46C35000 };

printf("f=%f\n", u.f);
Run Code Online (Sandbox Code Playgroud)

这样可以安全地将无符号的32位值解释为float

  • @Arnaud请阅读有关工会在C语言中的工作方式的信息:https://www.tutorialspoint.com/cprogramming/c_unions.htm (2认同)