Well, static_cast
is "safe" and it has defined behavior, but this is probably not what you need. Converting an integral value to float type will simply attempt to represent the same integral value in the target floating-point type. I.e. 5
of type int
will turn into 5.0
of type float
(assuming it is representable precisely).
What you seem to be doing is building the object representation of float
value in a piece of memory declared as Uint32
variable. To produce the resultant float
value you need to reinterpret that memory. This would be achieved by reinterpret_cast
assert(sizeof(float) == sizeof val);
return reinterpret_cast<float &>( val );
or, if you prefer, a pointer version of the same thing
assert(sizeof(float) == sizeof val);
return *reinterpret_cast<float *>( &val );
Although this sort of type-punning is not guaranteed to work in a compiler that follows strict-aliasing semantics. Another approach would be to do this
float f;
assert(sizeof f == sizeof val);
memcpy(&f, &val, sizeof f);
return f;
Or you might be able to use the well-known union hack to implement memory reinterpretation. This is formally illegal in C++ (undefined behavior), meaning that this method can only be used with certain implementations that support it as an extension
assert(sizeof(float) == sizeof(Uint32));
union {
Uint32 val;
float f;
} u = { val };
return u.f;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…