#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 2_7.c 2_7.cmp 2_7a.c 2_7tst.c makefile # # To extract the files from this shell archive file simply # create a directory for this file, move the archive file # to it and enter the command # # sh filename # # The files will be extracted automatically. # Note: Do not use csh. # # Archive created: Mon Jul 30 22:57:14 EDT 1990 # echo x - 2_7.c sed 's/^X//' > 2_7.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // print out the exponent and mantissa of a double #include #include "2_7a.c" /* EXPAND */ void prdouble(double d) { double_byte db; db.d = d; cout << d << "\n"; cout << (db.s_m.sign ? "-" : " "); cout << hex(db.s_m.exponent, 3) << " "; cout << hex(db.s_m.mantissa, 1); for (int i = 2; i < sizeof(double); i++) cout << hex(db.c[i], 3); cout << "\n"; } !EOF! ls -l 2_7.c echo x - 2_7.cmp sed 's/^X//' > 2_7.cmp << '!EOF!' arg 1 1 3ff 0 0 0 0 0 0 0 arg 2 2 400 0 0 0 0 0 0 0 arg 3 -1 -3ff 0 0 0 0 0 0 0 arg 4 4.4 401 1 99 99 99 99 99 9a !EOF! ls -l 2_7.cmp echo x - 2_7a.c sed 's/^X//' > 2_7a.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // union to pick apart the double union double_byte { double d; struct // first 2 bytes only { unsigned sign : 1; unsigned exponent : 11; unsigned mantissa : 4; } s_m; char c[sizeof(double)]; // all 8 bytes }; !EOF! ls -l 2_7a.c echo x - 2_7tst.c sed 's/^X//' > 2_7tst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include "2_7.c" main(int argc, char **argv) { for (register int i = 1; i < argc; i++) { double x = atof(argv[i]); cout << "arg " << i << "\n"; prdouble(x); } return 0; } !EOF! ls -l 2_7tst.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC all: 2_7tst 2_7tst: 2_7tst.c 2_7a.c 2_7.c $(CC) 2_7tst.c -o 2_7tst test: all 2_7.cmp 2_7tst 1.0 2.0 -1.0 4.4 > 2_7.out cmp 2_7.out 2_7.cmp echo tests done !EOF! ls -l makefile # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0