tomm@voodoo.UUCP (04/10/87)
#! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh <file", e.g.. If this archive is complete, you # will see the following message at the end: # "End of shell archive." # Contents: README Makefile round2.c round2.misc testr2.c # Wrapped by tomm@voodoo on Thu Apr 9 14:37:02 1987 PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test -f README -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"README\" else echo shar: Extracting \"README\" \(953 characters\) sed "s/^X//" >README <<'END_OF_README' XAbout every 3 years I have needed a function that rounds a number Xto the closest [some other number]. The most common use is where Xthe engineers might ask that all numbers be rounded to the nearest X0.03 inch, or material thickness be specified to the nearest 0.0625... X XHaving needed this function in almost every language I have used, I Xhereby give it to you in C. The file round2.c contains the function Xround2(); if compiled with -DR2DEBUG, it prints out its input and Xoutput. The file testr2.c is a simple test program for round2(). XThe file round2.misc contains the same function written in fortran77 Xand APT (Automatic Programmed Tools). X XI figure that this is not a hard function to write, but it sure has Xbeen useful. Hope you find a use for it, too. X-- XTom Mackey (206) 342-1442 (wk) XBoeing Computer Services ....uw-beaver!ssc-vax!voodoo!tomm XM/S 03-73, P.O. Box 24346, Seattle, WA 98124-0346 X END_OF_README if test 953 -ne `wc -c <README`; then echo shar: \"README\" unpacked with wrong size! fi # end of overwriting check fi if test -f Makefile -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"Makefile\" else echo shar: Extracting \"Makefile\" \(887 characters\) sed "s/^X//" >Makefile <<'END_OF_Makefile' X# X# Makefile for testr2.c [test program for round2() function] X# X XDESTDIR = . X XINCDIR = . X XLIBDIR = . X XFNAME = testr2 X XCSRC = round2.c testr2.c X XTMPFILES = round2.o testr2.o X XCOBJCTS = round2.o testr2.o X XCFLAGS = -O -DR2DEBUG X XLIBS = -lm X XLINTLIB = X XPRTFILES = $(CSRC) round2.misc Makefile X XPRTNAME = $(FNAME) X XFFHDR = "@ `date` `pwd`/* Page @@@" X XPRTFMT = /local/bin/ff -b -N 8 -H 3 -h $(FFHDR) -T 8 -w 128 -i 2 X XPRTDEV = /usr/ucb/lpr -Plp -J$(PRTNAME) X XCC = cc X X.DEFAULT: X co $@ X Xall: testr2 X @echo Done. X Xtestr2: $(COBJCTS) X $(CC) -o $(FNAME) $(COBJCTS) $(LIBS) X Xround2.o: round2.c X Xtestr2.o: testr2.c X Xinstall: all X cp $(FNAME) $(DESTDIR)/$(FNAME) X chmod 755 $(DESTDIR)/$(FNAME) X strip $(DESTDIR)/$(FNAME) X Xclean: X rm -f $(TMPFILES) core make.out X Xclobber: clean X rm -f $(FNAME) X Xlint: X lint -ac -I$(INCDIR) $(CSRC) $(LINTLIB) X Xprint: X $(PRTFMT) $(PRTFILES) | $(PRTDEV) X END_OF_Makefile if test 887 -ne `wc -c <Makefile`; then echo shar: \"Makefile\" unpacked with wrong size! fi # end of overwriting check fi if test -f round2.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"round2.c\" else echo shar: Extracting \"round2.c\" \(353 characters\) sed "s/^X//" >round2.c <<'END_OF_round2.c' X#include <math.h> X X#ifdef R2DEBUG X#include <stdio.h> X Xfloat round2(a, b) Xfloat a, b; X{ X float c; X X c = (floor((1.0 / b) * (a + (b / 2.0)))) / (1.0 / b); X X printf("rounding %f to nearest %f; result: %f\n",a, b, c); X X return c; X} X X#else X Xfloat round2(a, b) Xfloat a, b; X{ X return (floor((1.0 / b) * (a + (b / 2.0)))) / (1.0 / b); X} X X#endif X END_OF_round2.c if test 353 -ne `wc -c <round2.c`; then echo shar: \"round2.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f round2.misc -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"round2.misc\" else echo shar: Extracting \"round2.misc\" \(517 characters\) sed "s/^X//" >round2.misc <<'END_OF_round2.misc' X----------------------- fortran 77 ------------------------------ XC FUNCTION RONDTO RETURNS A ROUNDED TO NEAREST B X FUNCTION RONDTO(A, B) X RONDTO = (INT((1.0 / B) * (A + (B / 2.0)))) / (1.0 / B) X END X X------------------------- APT [IV, AC] -------------------------- X$$ WARNING: I DON'T REMEMBER IF INDENTATION IS ALLOWED OR WHAT X$$ COLUMN STATEMENTS MUST START IN. XRONDTO = MACRO/ A, B $$ ROUNDS A TO NEAREST B X RX = (INTF((1 / B) * (A + (B / 2)))) / (1 / B) X PRINT/2, A, B, RX XTERMAC X END_OF_round2.misc if test 517 -ne `wc -c <round2.misc`; then echo shar: \"round2.misc\" unpacked with wrong size! fi # end of overwriting check fi if test -f testr2.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"testr2.c\" else echo shar: Extracting \"testr2.c\" \(314 characters\) sed "s/^X//" >testr2.c <<'END_OF_testr2.c' X#include <stdio.h> X#include <math.h> X Xextern float round2(); X Xmain() X{ X float f[5]; X int i; X X f[0] = round2(M_PI, 5.0); X f[1] = round2(M_PI, 0.5); X f[2] = round2(M_PI, 0.1); X f[3] = round2(M_PI, 0.05); X f[4] = round2(M_PI, 0.02); X X for (i = 0; i < 5; printf("f[%d] = %f\n", i, f[i++])); X} X END_OF_testr2.c if test 314 -ne `wc -c <testr2.c`; then echo shar: \"testr2.c\" unpacked with wrong size! fi # end of overwriting check fi echo shar: End of shell archive. exit 0 -- Tom Mackey (206) 342-1442 (wk) Boeing Computer Services ....uw-beaver!ssc-vax!voodoo!tomm M/S 03-73, P.O. Box 24346, Seattle, WA 98124-0346