Getting the IEEE Single-precision bits for a float

后端 未结 4 1732
长情又很酷
长情又很酷 2021-01-07 03:54

I need to write an IEEE single-precision floating point number to a 32-bit hardware register at a particular address. To do that, I need to convert a variable of type float

相关标签:
4条回答
  • 2021-01-07 03:59

    EDIT: The union solution works everywhere I have tried it but somewhere on SO I had been pointed at standards that showed it didnt have to work. See the link below in the comments to find a LOT more info on this (Thank you Daniel!). Supposed to work or not supposed to work I would use it with care, I imagine endianness, etc gets involved as well (doubles broken into bytes, etc).

    Another solution is a dummy asm function. For example on arm:

    .globl echo 
    echo:
       bx lr
    
    
    unsigned int echo ( float );
    ...
    unsigned int ra; float f;
    f=1.0;
    ra=echo(f);
    

    some disassembly is required, needs to be on a system that doesnt have an fpu and/or uses gprs for carrying around floats.

    memcpy as already mentioned is the cleanest and most reliable and portable solution (be aware of endianness).

    0 讨论(0)
  • If you're trying to simply display the integral value of the float as it's stored in memory, then try using a union:

    union {
        float a;
        unsigned int target;
    } u;
    

    Store the float value:

    u.a = 2.39;
    

    Print both float and integer values:

    printf ("a = %f\n", u.a);
    printf ("target = %08X\n", u.target); /* you could use %u to show decimal */
    

    No compiler warnings. I use GNU compiler (gcc) on Linux. Notice that target is not a pointer; this is the beauty (and hideousness) of unions. ;-)

    0 讨论(0)
  • 2021-01-07 04:10

    You could creat a union type that contains a float and an unsigned int, store a value into the float member, then read it out of the int, like so:

    union reg_val
    {
      float f_val;
      unsigned int i_val;
    } myRegister;
    myRegister.f_val = 2.39;
    printf("target = %08X", myRegister.i_val);
    
    0 讨论(0)
  • 2021-01-07 04:14

    You can use type punning with a union,

    union {
        float f;
        uint32_t u;
    } un;
    un.f = your_float;
    uint32_t target = un.u;
    

    to get the bits. Or you can use memcpy.

    0 讨论(0)
提交回复
热议问题