In java I can use Double.longBitsToDouble() static function but in objective-C I can't find a way to do like that. How can I get that?
Thanks :)
In java I can use Double.longBitsToDouble() static function but in objective-C I can't find a way to do like that. How can I get that?
Thanks :)
I didn't really use that method in Java much, but what is wrong with simply setting a long long value to a variable of type double?
A long long will be 64 bits regardless of platform.
You can check for NaN in the double using isnan() in math.h if needed.
You can use memcpy
to copy the bit representation into the address of a variable of type double
. I'm not sure whether this enters the realm of undefined behaviour.
//assert(sizeof(double) == sizeof(unsigned long long));
unsigned long long bitPattern = 0x7ff0000000000000ULL;
double doubleValue;
memcpy(&doubleValue, &bitPattern, sizeof doubleValue);
//assert(isinf(doubleValue));
The bit pattern for positive infinity was borrowed from this page which appears to document the longBitsToDouble
method.
uint64_t bitPattern = whatever;// use this rather than long long because it is explicitly 64 bits
double doubleValue;
doubleValue = *(double*)&bitPattern;