bool isNaN(double x) { // Wipe out the sign bit const uint64 y = *(uint64*)(&x) & ((uint64(BIN32(01111111,11111111,11111111,11111111)) << 32) + 0xFFFFFFFF); // If the remaining number has all of the exponent bits set and atleast one // fraction bit set, then it is NaN return (y > (uint64(BIN32(01111111,11110000,00000000,00000000)) << 32)); }
void test_binary_32() { TEST_ASSERT_EQUAL_HEX32(0x00000000, BIN32(00000000, 00000000, 00000000, 00000000)); TEST_ASSERT_EQUAL_HEX32(0x0000000f, BIN32(00000000, 00000000, 00000000, 00001111)); TEST_ASSERT_EQUAL_HEX32(0x000000f0, BIN32(00000000, 00000000, 00000000, 11110000)); TEST_ASSERT_EQUAL_HEX32(0x00000f00, BIN32(00000000, 00000000, 00001111, 00000000)); TEST_ASSERT_EQUAL_HEX32(0x0000f000, BIN32(00000000, 00000000, 11110000, 00000000)); TEST_ASSERT_EQUAL_HEX32(0x000f0000, BIN32(00000000, 00001111, 00000000, 00000000)); TEST_ASSERT_EQUAL_HEX32(0x00f00000, BIN32(00000000, 11110000, 00000000, 00000000)); TEST_ASSERT_EQUAL_HEX32(0x0f000000, BIN32(00001111, 00000000, 00000000, 00000000)); TEST_ASSERT_EQUAL_HEX32(0xf0000000, BIN32(11110000, 00000000, 00000000, 00000000)); TEST_ASSERT_EQUAL_HEX32(0xffffffff, BIN32(11111111, 11111111, 11111111, 11111111)); }
bool isNaN(float x) { // Wipe out the sign bit const uint32 y = *(uint32*)(&x) & BIN32(01111111,11111111,11111111,11111111); // If the remaining number has all of the exponent bits set and atleast one // fraction bit set, then it is NaN return (y > 0x7F800000); }