Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!mit-eddie!uw-beaver!ssc-vax!voodoo!tomm From: tomm@voodoo.UUCP Newsgroups: net.sources Subject: rounding function Message-ID: <303@voodoo.UUCP> Date: Thu, 9-Apr-87 17:40:56 EST Article-I.D.: voodoo.303 Posted: Thu Apr 9 17:40:56 1987 Date-Received: Sun, 12-Apr-87 01:48:03 EST Sender: root@voodoo.UUCP Reply-To: tomm@voodoo.UUCP (Tom Mackey) Distribution: world Organization: Voodoo Graphics Project Lines: 216 #! /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 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 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 round2.c <<'END_OF_round2.c' X#include X X#ifdef R2DEBUG X#include 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.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 testr2.c <<'END_OF_testr2.c' X#include X#include 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