Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- unsigned znak(unsigned long long h) {
- return h >> 63;
- }
- unsigned long long mantissa(unsigned long long h) {
- return (h & 0xFFFFFFFFFFFFF) | 0x10000000000000;
- }
- int poradok(unsigned long long h) {
- return (h >> 52) & 0x7FF;
- }
- void poradokd(unsigned long long h)
- {
- int i, y[11];
- for (i = 0; i < 11; i++) {
- y[i] = h % 2;
- h /= 2;
- }
- for (i = 10; i >= 0; i--) printf("%d", y[i]);
- }
- void mantisad(unsigned long long h)
- {
- int i, y[52];
- for (i = 0; i < 52; i++) {
- y[i] = h % 2;
- h /= 2;
- }
- for (i = 51; i >= 0; i--) printf("%d", y[i]);
- }
- void celoev2(long long h)
- {
- int i = 0, j;
- int y[64];
- if (h <= 0) {
- h = -h;
- printf("0");
- }
- while (h != 0) {
- y[i] = h % 2;
- h /= 2;
- i++;
- }
- for (j = i - 1; j >= 0; j--)
- printf("%d", y[j]);
- }
- void drobv2(double h) {
- int r; h -= (int)h;
- if (h < 0) { h = -h; printf("-0."); }
- else printf("0.");
- for (int i = 0; i < 52 && h != 0; i++) {
- h *= 2; r = (int)h;
- h -= r; printf("%d", r);
- }
- }
- unsigned long long celoepoman(unsigned long long h, int p)
- {
- p -= 1023;
- return h >> (52 - p);
- }
- unsigned long long drobpoman(unsigned long long h, int p)
- {
- p -= 1023;
- return h << (12 + p) >> (12 + p);
- }
- int umnozheniye(int* y)
- {
- int i, j, b = 0, y1[1052] = { 0 }, y2[1052] = { 0 }, res[1052] = { 0 }, r = 0;
- for (i = 3; i < 1051; i++)
- y1[i] = y[i - 3];
- for (i = 1; i < 1049; i++)
- y2[i] = y[i - 1];
- for (i = 1051; i >= 0; i--) {
- res[i] = y1[i] + y2[i] + b;
- if (res[i] >= 2) {
- res[i] -= 2;
- b = 1;
- }
- else {
- b = 0;
- }
- }
- for (i = 0; i < 4; i++) {
- b = 1;
- for (j = 0; j < 3 - i; j++) b *= 2;
- r += res[i] * b;
- }
- for (i = 0; i < 1048; i++) y[i] = res[i + 4];
- return r;
- }
- int esli0(int* y)
- {
- for (int i = 0; i < 1052; i++) {
- if (y[i] != 0) {
- return 0;
- }
- }
- return 1;
- }
- void obratno(unsigned long long h, long long p)
- {
- int i, y[1052] = { 0 }, res[256] = { 0 };
- p -= 1023;
- h <<= (12 + p);
- for (i = 0; i < 64; i++) {
- y[i] = (h << i) >> 63;
- }
- for (i = 64; i < 1052; i++) {
- y[i] = 0;
- }
- int index = 0;
- while (!esli0(y) && index < 256) {
- res[index] = umnozheniye(y);
- index++;
- }
- for (int j = 0; j < index; j++) {
- printf("%d", res[j]);
- }
- }
- int main()
- {
- union {
- unsigned long long i;
- double h;
- } a;
- printf("n= ");
- scanf_s("%lf", &a.h);
- printf("\nV 2 vide: %d", znak(a.i));
- poradokd(poradok(a.i));
- printf(" ");
- mantisad(mantissa(a.i));
- printf(" ");
- mantisad(mantissa(a.i));
- printf("\n\nZnak: %d", znak(a.i));
- printf("\nHaracteristica: "); poradokd(poradok(a.i));
- printf("\nMantissa:"); mantisad(mantissa(a.i));
- printf("\n\nPoryadok: %d", poradok(a.i) - 1023);
- printf("\n\nCeloe v 2 vide: ");
- celoev2(celoepoman(mantissa(a.i), poradok(a.i)));
- printf("\n\nDrob v 2 vide:");
- int p = poradok(a.i) - 1023;
- double fractional_part = (double)drobpoman(mantissa(a.i), poradok(a.i)) / (1LL << (52 - p));
- drobv2(fractional_part);
- printf("\n\nCeloe v 10 vide: %lld", znak(a.i) == 1 ? -1 * celoepoman(mantissa(a.i), poradok(a.i)) : celoepoman(mantissa(a.i), poradok(a.i)));
- printf("\n\nDrob v 10 vide: %c0.", znak(a.i) == 1 ? '-' : ' '); obratno(drobpoman(mantissa(a.i), poradok(a.i)), poradok(a.i));
- printf("\n\nChislo v 10 vide:%c%lld.", znak(a.i) == 1 ? '-' : ' ', celoepoman(mantissa(a.i), poradok(a.i))); obratno(drobpoman(mantissa(a.i), poradok(a.i)), poradok(a.i));
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement