CSC373/406: Example: Floating Point [3/8] Previous pageContentsNext page

file:float1.c [source]
00001: #include <stdio.h>
00002: #include <math.h>
00003: #include "show-bytes.c"
00004: 
00005: int main() {
00006:   float x;
00007:   printf("Enter a floating point number\n");
00008:   scanf("%f", &x);
00009:   printf("%f in 2's complement is ", x);
00010:   show_bytes((byte_pointer) &x, sizeof x);
00011:   printf("\n");
00012:   return 0;
00013: }
00014: 

file:float2.c [source]
00001: #include <stdio.h>
00002: #include <math.h>
00003: #include "show-bytes.c"
00004: 
00005: int main() {
00006:   float v = 1.0,    // 1.0
00007:         w = 2.5,    // 10.1      
00008:         x = 3.25,   // 11.01
00009:         y = 32.5;   // 100000.1
00010:   double z = 75.75; // 1001011.11
00011:   float a = .5,
00012:         b = 3./16.;
00013:   int i=1;
00014:   float c = *((float *) ((byte_pointer) &i));
00015:   float d = 10.0/0.0;
00016:   float n1 = -1.0,
00017:         n2 = -3.25;
00018:   //  float c = 0.00000011920928955078125; // smallest possible float
00019: 
00020:   printf("%f (1.0) = ", v);
00021:   show_bytes((byte_pointer) &v, sizeof v);
00022:   printf("\n");  
00023: 
00024:   printf("%f (10.1) = ", w);
00025:   show_bytes((byte_pointer) &w, sizeof w);
00026:   printf("\n");
00027: 
00028:   printf("%f (11.01) = ", x);
00029:   show_bytes((byte_pointer) &x, sizeof x);
00030:   printf("\n");
00031: 
00032:   printf("%f (100000.1) = ", y);
00033:   show_bytes((byte_pointer) &y, sizeof y);
00034:   printf("\n");
00035: 
00036:   printf("%f (1001011.11) = ", z);
00037:   show_bytes((byte_pointer) &z, sizeof z);
00038:   printf("\n");
00039: 
00040:   printf("%f (.1) = ", a);
00041:   show_bytes((byte_pointer) &a, sizeof a);
00042:   printf("\n");
00043: 
00044:   printf("%f (.0011) = ", b);
00045:   show_bytes((byte_pointer) &b, sizeof b);
00046:   printf("\n");
00047: 
00048:   printf("%1.50f (smallest possible float) = ", c);
00049:   show_bytes((byte_pointer) &c, sizeof c);
00050:   printf("\n");  
00051: 
00052:   printf("10.0/0.0 = %f =", d);
00053:   show_bytes((byte_pointer) &d, sizeof d);
00054:   printf("\n");  
00055: 
00056:   printf("%f (-1.0) = ", n1);
00057:   show_bytes((byte_pointer) &n1, sizeof n1);
00058:   printf("\n");  
00059: 
00060:   printf("%f (-11.01) = ", n2);
00061:   show_bytes((byte_pointer) &n2, sizeof n2);
00062:   printf("\n");  
00063: 
00064:   return 0;
00065: }
00066: 

file:float-b2u.c [source]
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: 

Previous pageContentsNext page