I have two doubles x
and y
with y
known to be a particular NaN value, I'd like to see if they are bitwise identical. That is, I want to determine if x
is exactly the same NaN value as y
.
NaNs cannot be usefully compared with the ==
opeator since NaNs are never equal to any other value (not even themselves!).
Is there something better than the following "bitwise equal" approach (and is this approach legal?):
bool bitwise_equal(double x, double y) {
unsigned char xbytes[sizeof(x)];
unsigned char ybytes[sizeof(y)];
memcpy(xbytes, &x, sizeof(x));
memcpy(ybytes, &y, sizeof(y));
return memcmp(xbytes, ybytes, sizeof(x)) == 0;
}