Created
July 4, 2014 06:55
-
-
Save fpt/9a58a0c88a6618db9dd8 to your computer and use it in GitHub Desktop.
print double numbers in binary form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <bitset> | |
#include <limits> | |
// | |
// Print double numbers in binary form | |
// | |
typedef std::numeric_limits<double> dbl; | |
using namespace std; | |
typedef union { | |
unsigned long long u; | |
double d; | |
} dfloat; | |
int main() { | |
dfloat x, y, z; | |
x.d = 0.1234567890123455l; | |
y.d = x.d * 10.0; | |
z.d = x.d * 10000000000000000.0l; | |
cout.precision(dbl::digits10); | |
cout << "x(dec): " << fixed << x.d << endl; | |
cout << "x(bin): " << bitset<64>(x.u) << endl; | |
cout << "y(dec): " << fixed << y.d << endl; | |
cout << "y(bin): " << bitset<64>(y.u) << endl; | |
cout << "z(dec): " << fixed << z.d << endl; | |
cout << "z(bin): " << bitset<64>(z.u) << endl; | |
return 1; | |
} | |
// http://stackoverflow.com/questions/7349689/c-how-to-print-using-cout-the-way-a-number-is-stored-in-memory | |
// http://stackoverflow.com/questions/8521638/exact-binary-representation-of-a-double | |
// http://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment