fork download
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int validIEEE(void) {
    if (sizeof (double) != 8) return 0;
    double x = -3.14159;
    unsigned char data[] = {0x6e, 0x86, 0x1b, 0xf0, 0xf9, 0x21, 0x09, 0xc0};
    if (memcmp(&x, data, 8)) return 0;
    return 1;
}

void printIEEE(void *xx) {
    unsigned char *x = xx;
    putchar((x[7] & 0x80) ? '-' : '+');
    printf("1.");
    for (int j = 3; j >= 0; j--) {
        putchar((x[6] & (1 << j)) ? '1' : '0');
    }
    for (int k = 5; k >= 0; k--) {
        for (int j = 7; j >= 0; j--) {
            putchar((x[k] & (1 << j)) ? '1' : '0');
        }
    }
    unsigned exp = ((x[7] & 0x7fu) << 4) + (x[6] >> 4);
    printf(" %+d ", (int)exp - 1023);
    putchar('\n');
}

int main(void) {
    if (!validIEEE()) {
        fprintf(stderr, "This computer does not pass the tests for valid IEEE numbers.\n");
        exit(EXIT_FAILURE);
    }
    while (1) {
        double x;
        printf("Enter a number: ");
        fflush(stdout);
        if (scanf("%lf", &x) != 1) break;
        printf("%f is ", x);
        printIEEE(&x);
    }
    return 0;
}
Success #stdin #stdout 0s 2164KB
stdin
1.25
-12.5
3.14159
-0.0000000000001
-1000000000000
1000000000000
foo
stdout
Enter a number: 1.250000 is +1.0100000000000000000000000000000000000000000000000000 +0 
Enter a number: -12.500000 is -1.1001000000000000000000000000000000000000000000000000 +3 
Enter a number: 3.141590 is +1.1001001000011111100111110000000110111000011001101110 +1 
Enter a number: -0.000000 is -1.1100001001011100001001101000010010010111011010000010 -44 
Enter a number: -1000000000000.000000 is -1.1101000110101001010010100010000000000000000000000000 +39 
Enter a number: 1000000000000.000000 is +1.1101000110101001010010100010000000000000000000000000 +39 
Enter a number: