Is there an easy way to determine the sign of a floating point number?
I experimented and came up with this:
#include <iostream>
int main(int argc, char** argv)
{
union
{
float f;
char c[4];
};
f = -0.0f;
std::cout << (c[3] & 0x10000000) << "\n";
std::cin.ignore();
std::cin.get();
return 0;
}
where (c[3] & 0x10000000) gives a value > 0 for a negative number but I think this requires me to make the assumptions that:
- The machine's bytes are 8 bits big
- a float point number is 4 bytes big?
- the machine's most significant bit is the left-most bit (endianness?)
Please correct me if any of those assumptions are wrong or if I have missed any.