#include "defs" s26(pd0) /* 2.6 Hardware Characteristics */ struct defs *pd0; { static char qs26[8] = "s26 "; char *ps, *pt; char c0, c1; struct {float stemp, sone, sdeltaf;} S1; /* force it to store */ #define one S1.sone #define temp S1.stemp #define deltaf S1.sdeltaf struct {double stempd, soned, sdeltad;} S2; /* force it to store */ #define oned S2.soned #define tempd S2.stempd #define deltad S2.sdeltad static char s[] = "%3d bits in %ss.\n"; static char s2[] = "%e is the least number that can be added to 1. (%s).\n"; ps = qs26; pt = pd0->rfs; while(*pt++ = *ps++); /* Here, we shake the machinery a little to see what falls out. First, we find out how many bits are in a char. */ pd0->cbits = 0; c0 = 0; c1 = 1; while(c0 != c1) { c1 = c1<<1; pd0->cbits = pd0->cbits+1; } /* That information lets us determine the size of everything else. */ pd0->ibits = pd0->cbits * sizeof(int); pd0->sbits = pd0->cbits * sizeof(short); pd0->lbits = pd0->cbits * sizeof(long); pd0->ubits = pd0->cbits * sizeof(unsigned); pd0->fbits = pd0->cbits * sizeof(float); pd0->dbits = pd0->cbits * sizeof(double); /* We have now almost reconstructed the table in section 2.6, the exception being the range of the floating point hardware. Now there are just so many ways to conjure up a floating point representation system that it's damned near impossible to guess what's going on by writing a program to interpret bit patterns. Further, the information isn't all that useful, if we consider the fact that machines that won't handle numbers between 10**30 and 10**-30 are very hard to find, and that people playing with numbers outside that range have a lot more to worry about than just the capacity of the characteristic. A much more useful measure is the precision, which can be ex- pressed in terms of the smallest number that can be added to 1. without loss of significance. We calculate that here, for float and double. */ one = 1.; deltaf = 1.; temp = 0.; while(temp != one) { temp = one+deltaf; deltaf = deltaf/2.; } pd0->fprec = deltaf * 4.; oned = 1.; deltad = 1.; tempd = 0.; while(tempd != oned) { tempd = oned+deltad; deltad = deltad/2.; } print("delta=%g\n", deltad); pd0->dprec = deltad * 4.; /* Now, if anyone's interested, we publish the results. */ if(pd0->flgm != 0) { printf(s,pd0->cbits,"char"); printf(s,pd0->ibits,"int"); printf(s,pd0->sbits,"short"); printf(s,pd0->lbits,"long"); printf(s,pd0->ubits,"unsigned"); printf(s,pd0->fbits,"float"); printf(s,pd0->dbits,"double"); printf(s2,pd0->fprec,"float"); printf(s2,pd0->dprec,"double"); } /* Since we are only exploring and perhaps reporting, but not testing any features, we cannot return an error code. */ return 0; }