00001: #include <stdio.h>
00002: #include <math.h>
00003: #include "show-bytes.c"
00004:
00005: unsigned int b2u(char *s) {
00006: unsigned int x=0;
00007: while (*s!='\0') { // while s not pointing to NULL
00008: if (*s!='1' && *s!='0') { s++; continue; } // skip non-digits
00009: x <<= 1; // shift left one bit
00010: if (*s=='1') { x++; } // add this bit
00011: s++; // advance s
00012: }
00013: return x;
00014: }
00015: int main() {
00016: double v;
00017: float f;
00018: float *p, *q;
00019: unsigned int x, y;
00020:
00021: p = (float *) &x;
00022: q = (float *) &y;
00023:
00024: v = 0;
00025: printf("%le = ", v);
00026: show_bytes((byte_pointer) &v, sizeof v);
00027: printf("\n\n");
00028:
00029: v = HUGE_VAL;
00030: printf("%le = ", v);
00031: show_bytes((byte_pointer) &v, sizeof v);
00032: printf("\n\n");
00033:
00034: v = -HUGE_VAL;
00035: printf("%le = ", v);
00036: show_bytes((byte_pointer) &v, sizeof v);
00037: printf("\n\n");
00038:
00039: v = 15213;
00040: printf("%le = ", v);
00041: show_bytes((byte_pointer) &v, sizeof v);
00042: printf("\n\n");
00043:
00044: f = 15213;
00045: printf("%e = ", f);
00046: show_bytes((byte_pointer) &f, sizeof f);
00047: printf("\n\n");
00048:
00049: //x = b2u("111100001111");
00050: //printf("%u = ", x);
00051: //show_bytes((byte_pointer) &x, sizeof x);
00052: //printf("\n\n");
00053:
00054: // Pos and neg zero
00055: x = b2u("0 00000000 00000000000000000000000");
00056: printf("%e = (+0)\n", *p);
00057: show_bytes((byte_pointer) p, sizeof (*p));
00058: printf("\n\n");
00059:
00060: y = b2u("1 00000000 00000000000000000000000");
00061: printf("%e = (-0)\n", *q);
00062: show_bytes((byte_pointer) q, sizeof (*q));
00063: printf("\n\n");
00064: printf("(0)==(-0)? %d", x==y);
00065: printf("\n\n");
00066:
00067: // Denormalized
00068: x = b2u("0 00000000 00000000000000000000001");
00069: printf("%e = (0+epsilon)\n", *p);
00070: show_bytes((byte_pointer) p, sizeof (*p));
00071: printf("\n\n");
00072:
00073: x = b2u("0 00000000 11111111111111111111111");
00074: printf("%e = (largest denorm)\n", *p);
00075: show_bytes((byte_pointer) p, sizeof (*p));
00076: printf("\n\n");
00077:
00078: // Normalized
00079: x = b2u("0 00000001 00000000000000000000000");
00080: printf("%e = (smallest norm)\n", *p);
00081: show_bytes((byte_pointer) p, sizeof (*p));
00082: printf("\n\n");
00083:
00084: x = b2u("0 01111110 11111111111111111111111");
00085: printf("%e = (1-epsilon)\n", *p);
00086: show_bytes((byte_pointer) p, sizeof (*p));
00087: printf("\n\n");
00088:
00089: x = b2u("0 01111111 00000000000000000000000");
00090: printf("%e = (1)\n", *p);
00091: show_bytes((byte_pointer) p, sizeof (*p));
00092: printf("\n\n");
00093:
00094: x = b2u("0 11111110 11111111111111111111111");
00095: printf("%e = (largest norm)\n", *p);
00096: show_bytes((byte_pointer) p, sizeof (*p));
00097: printf("\n\n");
00098:
00099: // Specials
00100: x = b2u("0 11111111 00000000000000000000000");
00101: printf("%e = (+INFINTIY)\n", *p);
00102: show_bytes((byte_pointer) p, sizeof (*p));
00103: printf("\n\n");
00104:
00105: x = b2u("0 11111111 00000000000000000010000");
00106: printf("%e = (NaN)\n", *p);
00107: show_bytes((byte_pointer) p, sizeof (*p));
00108: printf("\n\n");
00109:
00110: // Denormalized
00111: x = b2u("1 00000000 00000000000000000000001");
00112: printf("%e = (0+epsilon)\n", *p);
00113: show_bytes((byte_pointer) p, sizeof (*p));
00114: printf("\n\n");
00115:
00116: x = b2u("1 00000000 11111111111111111111111");
00117: printf("%e = (largest denorm)\n", *p);
00118: show_bytes((byte_pointer) p, sizeof (*p));
00119: printf("\n\n");
00120:
00121: // Normalized
00122: x = b2u("1 00000001 00000000000000000000000");
00123: printf("%e = (smallest norm)\n", *p);
00124: show_bytes((byte_pointer) p, sizeof (*p));
00125: printf("\n\n");
00126:
00127: x = b2u("1 01111110 11111111111111111111111");
00128: printf("%e = (1-epsilon)\n", *p);
00129: show_bytes((byte_pointer) p, sizeof (*p));
00130: printf("\n\n");
00131:
00132: x = b2u("1 01111111 00000000000000000000000");
00133: printf("%e = (1)\n", *p);
00134: show_bytes((byte_pointer) p, sizeof (*p));
00135: printf("\n\n");
00136:
00137: x = b2u("1 11111110 11111111111111111111111");
00138: printf("%e = (largest norm)\n", *p);
00139: show_bytes((byte_pointer) p, sizeof (*p));
00140: printf("\n\n");
00141:
00142: // Specials
00143: x = b2u("1 11111111 00000000000000000000000");
00144: printf("%e = (-INFINTIY)\n", *p);
00145: show_bytes((byte_pointer) p, sizeof (*p));
00146: printf("\n\n");
00147:
00148: x = b2u("1 11111111 00000001000000000000000");
00149: printf("%e = (NaN)\n", *p);
00150: show_bytes((byte_pointer) p, sizeof (*p));
00151: printf("\n\n");
00152:
00153: return 0;
00154: }
00155:
|