Possible Duplicate:
Float to binary in C++
I have a very small double var, and when I print it I get -0. (using C++). Now in order to get better precision I tried using
cout.precision(18); \\i think 18 is the max precision i can get.
cout.setf(ios::fixed,ios::floatfield);
cout<<var;\\var is a double.
but it just writes -0.00000000000...
I want to see the exact binary representation of the var.
In other words I want to see what binary number is written in the stack memory/register for this var.
Try:
This should work for a 32 bit variable, to display it in hex. I've never tried using this technique to see a 64 bit variable, but I think it's:
EDIT: tested the 64-bit version and it definitely works on Win32 (I seem to recall the need for uppercase LL on GCC.. maybe)
EDIT2: if you really want binary, you are best off using one of the other answers to get a uint64_t version of your double, and then looping:
Update:
The version above will work for most purposes, but it assumes 64 bit doubles. This version makes no assumptions and generates a binary representation:
This way is guaranteed to work by the standard: