I had this question on my exam, and I couldn't realy solve it, will appreciate some help.
Fill the blanks only, function must return true if and only if x<y. Assume x,y cannot be NaN (but can be +-inf) no casting is allowed, use only ux, uy, sx, sy
bool func(float x, float y) {
unsigned* uxp = ______________ ;
unsigned* uyp = ______________ ;
unsigned ux = *uxp;
unsigned uy = *uyp;
unsigned sx = (ux>>31);
unsigned sy = (uy>>31);
return ___________________________;
}
Presumably the assignment assumes
floatuses IEEE-754 binary32 andunsignedis 32 bits.It is not proper to alias
floatobjects with anunsignedtype, although some C implementations support it. Instead, you can create a compound literal union, initialize itsfloatmember with thefloatvalue, and access itsunsignedmember. (This is supported by the C standard but not by C++.)After that, it is simply a matter of dividing the comparison into cases depending on the sign bits:
Sample output: