Which *might* be exactly what he wants. In my earlier followup, I
assumed that Angus actually wanted to obtain the representation of the
double value as a 64-bit integer. jacob is correct that this might
not make much sense; on the other hand, Angus hasn't given us enough
information to be sure about what he's trying to accomplish.
If you want to obtain the representation, using memcpy() as Ben
Bacarisse suggested is probably the best approach, though it can be
perilous.
If you want a *value* conversion, just assign it (the result will be
3, and you'll lose the .3 fractional part). (A cast will perform an
explicit value conversion, but it's usually not needed; in most
contexts, any needed conversion will be done implicitly.)
For example:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(void)
{
double d = 3.3;
unsigned long long rep;
unsigned long long val;
assert(sizeof d == sizeof rep);
memcpy(&rep, &d, sizeof d);
val = d;
printf("d = %g\n", d);
printf("rep = 0x%llx\n", rep);
printf("val = %lld\n", val);
return 0;
}
On my system, I get:
d = 3.3
rep = 0x400a666666666666
val = 3
--
Keith Thompson (The_Other_Keith) <
[email protected]>
Nokia
"We must do something. This is something. Therefore, we must do this.."
-- Antony Jay and Jonathan Lynn, "Yes Minister"- Hide quoted text -
- Show quoted text -