Skip to content

Instantly share code, notes, and snippets.

@ddrone
Created June 26, 2011 00:20
Show Gist options
  • Save ddrone/1047075 to your computer and use it in GitHub Desktop.
Save ddrone/1047075 to your computer and use it in GitHub Desktop.
Double number representation
#include <cstdio>
#include <iostream>
using namespace std;
static union
{
double use;
char output[8];
};
void print_hex()
{
char buffer[20];
for (int i = 7; i >= 0; i--)
{
if (i < 7) putchar(' ');
if (output[i] >= 0)
{
printf("%02x", int(output[i]));
}
else
{
sprintf(buffer, "%x", output[i]);
putchar(buffer[6]);
putchar(buffer[7]);
}
}
putchar('\n');
}
void print_char(char c)
{
for (int i = 7; i >= 0; i--)
{
if (c & (1 << i)) putchar('1');
else putchar('0');
}
}
void print_bin()
{
print_char(output[7]);
for (int i = 6; i >= 0; i--)
{
putchar(' ');
print_char(output[i]);
}
putchar('\n');
}
void print()
{
cout << use << '\n';
print_hex();
print_bin();
putchar('\n');
}
int main()
{
use = 3.0;
print();
use = -3.0;
print();
use = 127.9;
print();
use = 0.0;
print();
use = -0.0;
print();
double zero = 1.0 - 1.0;
use = 0.0 / zero;
print();
use = 1.0 / zero;
print();
use = -1.0 / zero;
print();
system("pause");
return 0;
}
3
40 08 00 00 00 00 00 00
01000000 00001000 00000000 00000000 00000000 00000000 00000000 00000000
-3
c0 08 00 00 00 00 00 00
11000000 00001000 00000000 00000000 00000000 00000000 00000000 00000000
127.9
40 5f f9 99 99 99 99 9a
01000000 01011111 11111001 10011001 10011001 10011001 10011001 10011010
0
00 00 00 00 00 00 00 00
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
-0
80 00 00 00 00 00 00 00
10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
-1.#IND
ff f8 00 00 00 00 00 00
11111111 11111000 00000000 00000000 00000000 00000000 00000000 00000000
1.#INF
7f f0 00 00 00 00 00 00
01111111 11110000 00000000 00000000 00000000 00000000 00000000 00000000
-1.#INF
ff f0 00 00 00 00 00 00
11111111 11110000 00000000 00000000 00000000 00000000 00000000 00000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment