Assume the following:
#include <iostream>
int main()
{
double a = 5.6;
intptr_t b = (intptr_t) a;
double c = (double) b;
return 0;
}
c
will be 5
. My question is, since intptr_t
is also 64 bits on a 64 bit machine (same as double), how come the precision bits are not saved during casting?
Although intptr_t
is meant to represent a pointer to an int, its underling type is still an integer. Thus
intptr_t b = (intptr_t)a
Is still truncating the double
, similar to if you'd just written:
int b = (int)a;
What you want to do is take the address of a
:
intptr_t b = (intptr_t)&a
And then convert it back
double c = *(double*)b;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.