Exact binary representation of double

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, to get better accuracy, 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 he just writes -0.00000000000 ...

I want to see the exact binary representation of var.

In other words, I want to see what binary number is stored in the / register stack for this var.

+4
source share
3 answers
 union myUnion { double dValue; uint64_t iValue; }; myUnion myValue; myValue.dValue=123.456; cout << myValue.iValue; 

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:

  double someDouble=123.456; unsigned char rawBytes[sizeof(double)]; memcpy(rawBytes,&someDouble,sizeof(double)); //The C++ standard does not guarantee 8-bit bytes unsigned char startMask=1; while (0!=static_cast<unsigned char>(startMask<<1)) { startMask<<=1; } bool hasLeadBit=false; //set this to true if you want to see leading zeros size_t byteIndex; for (byteIndex=0;byteIndex<sizeof(double);++byteIndex) { unsigned char bitMask=startMask; while (0!=bitMask) { if (0!=(bitMask&rawBytes[byteIndex])) { std::cout<<"1"; hasLeadBit=true; } else if (hasLeadBit) { std::cout<<"0"; } bitMask>>=1; } } if (!hasLeadBit) { std::cout<<"0"; } 
+4
source

This method is guaranteed to work according to the standard:

 double d = -0.0; uint64_t u; memcpy(&u, &d, sizeof(d)); std::cout << std::hex << u; 
+2
source

Try:

 printf("0x%08x\n", myFloat); 

This should work for a 32-bit variable in order to display it in hexadecimal format. I have never tried using this technique to see a 64-bit variable, but I think this is:

 printf("%016llx\n", myDouble); 

EDIT: The 64-bit version has been tested, and it definitely works on Win32 (I seem to recall the need for a capital LL on GCC .. maybe)

EDIT2: if you really want to use the binary, you are best off using one of the other answers to get the version of your doubling uint64_t and then the loop:

 for ( int i = 63; i >= 0; i-- ) { printf( "%d", (myUint64 >> i ) & 1 ); } 
+1
source

Source: https://habr.com/ru/post/1386533/


All Articles