Type coercion in c: unsigned int to float?

You need to cast the pointers. Casting the values simply converts the int to float. Try: dest = *((float32*)&temp).

You need to cast the pointers. Casting the values simply converts the int to float. Try: *dest = *((float32*)&temp).

Also I'm very suspicious of the line temp = (float32)temp; The best you could hope for would be no effect, but I think in fact it turn the value you've so carefully constructed into the int "100". – Ernest Friedman-ll Jun 8 at 15:27 That worked, thank you for the speedy answer! – Trey Jun 8 at 15:29 @Trey Then don't forget to accept (and up-vote) his answer.

– Christian Rau Jun 8 at 16:03 2 This invokes undefined behavior and will result in incorrect code generation on modern compilers! – R.. Jun 8 at 16:49 The behavior is certainly undefined. Interpreting the bits of an int as a float can't be defined portably, so the language must treat it as undefined.

I'm not sure what you mean by incorrect code generation. Assuming that I know how the bits need to be laid out on my architecture, what will go wrong? (OTOH, the union answer below seems cleaner.) – andrewdski Jun 8 at 22:12.

The portable way that does not invoke undefined behavior due to aliasing rules violations: float f; uint32_t i; memcpy(&f, &i, sizeof f).

Here is one more solution: union test { float f; unsigned int i; } x; float flt = 100.0; unsigned int uint; x. F = flt; uint = x. I; Now unit has the bit pattern as it was in f.

Isn't Hex (IEEE754 ) representation of float 100.0 -->0x42c80000.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions