how to tell the value of a float pointer when it has been referenced from an integer? ex: float *f= (float *)someInteger

Report
Question

Please briefly explain why you feel this question should be reported .

Report Cancel

I cannot figure out how to convert the value of a referenced float pointer when it is referenced from an integer casted into a float pointer. I’m sorry if I’m wording this incorrectly. Here is an example of what I mean:

#include <stdio.h>

main() {
    int i;
    float *f;

    i = 1092616192;

    f = (float *)&i;

    printf("i is %d and f is %fn", i, *f);
}

the output for f is 10. How did I get that result?

solved 0
3 Answer 28 views 0

Answers ( 3 )

    4
    January 11, 2017 at 4:00 pm

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    Normally, the value of 1092616192 in hexadecimal is 0x41200000.

    In floating-point, that will give you:

    sign = positive (0b)
    exponent = 130, 2^3 (10000010b)
    significand = 2097152, 1.25 (01000000000000000000000b)
    

    2^3*1.25
    = 8 *1.25
    = 10

    To explain the exponent part uses an offset encoding, so you have to subtract 127 from it to get the real value. 130 – 127 = 3. And since this is a binary encoding, we use 2 as the base. 2 ^ 3 = 8.

    To explain the significand part, you start with an invisible ‘whole’ value of 1. the uppermost (leftmost) bit is half of that, 0.5. The next bit is half of 0.5, 0.25. Because only the 0.25 bit and the default ‘1’ bit is set, the significand represents 1 + 0.25 = 1.25.

    Best answer
    2
    January 11, 2017 at 4:00 pm

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    What you are trying to do is called type-punning. It should be done via a union, or using memcpy() and is only meaningful on an architecture where sizeof(int) == sizeof(float) without padding bits. The result is highly dependent on the architecture: byte ordering and floating point representation will affect the reinterpreted value. The presence of padding bits would invoke undefined behavior as the representation of float 15.0 could be a trap value for type int.

    Here is how you get the number corresponding to 15.0:

    #include <stdio.h>
    
    int main(void) {
        union {
            float f;
            int i;
            unsigned int u;
        } u;
    
        u.f = 15;
    
        printf("re-interpreting the bits of float %.1f as int gives %d (%#x in hex)n",
               u.f, u.i, u.u);
    
        return 0;
    }
    

    output on an Intel PC:

    re-interpreting the bits of float 15.0 as int gives 1097859072 (0x41700000 in hex)
    
    0
    January 11, 2017 at 4:00 pm

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    You are trying to predict the consequence of an undefined activity – it depends on a lot of random things, and on the hardware and OS you are using.

    Basically, what you are doing is throwing a glass against the wall and getting a certain shard. Now you are asking how to get a differently formed shard. well, you need to throw the glass differently against the wall…

Leave an answer

Browse

What is the capital of Egypt ? ( Cairo )

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>