Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!uakari.primate.wisc.edu!aplcen!haven!adm!cmcl2!lanl!jlg From: jlg@lanl.gov (Jim Giles) Newsgroups: comp.lang.fortran Subject: Re: rounding a real to a whole number Message-ID: <5524@lanl.gov> Date: 10 Nov 90 02:04:06 GMT References: <6125@harrier.ukc.ac.uk> Organization: Los Alamos Natl Lab, Los Alamos, N.M. Lines: 52 In article u714092@eagle.larc.nasa.gov (prichard devon ) writes: > [...] > what I want to do is to have a subroutine which, given a range of real > numbers, for example -123.445 -> 1033.02 , and choose whole numbers > outside that range. obviously the first application is axis values for > x-y plots. How about: Subroutine ticks(lbound,hbound,bottom,top,tick) C takes low and high bound of interval and returns C a 'tick' value for the interval between tick marks C on a graph axis. The value of tick is always C either a power of 10, twice a power of ten or C five times a power of ten. A new interval is C also returned which begins below the original C and ends above the original and is an integral C number of 'tick's long. The number of ticks in C the new interval is (about) 5 to 13. real lbound,hbound,bottom,top,tick real lowtic, basetk integer tenp lowtic = (hbound-lbound)/12.5 tenp=int(alog10(lowtick)) if (tenp.gt.alog10(lowtick)) tenp=tenp-1 basetk = 10**tenp if (tick.lt.lowtick) tick = 2 * basetk if (tick.lt.lowtick) tick = 5 * basetk if (tick.lt.lowtick) tick = 10 * basetk bottom = nint(lbound/tick)*tick if (bottom.gt.lbound) bottom = bottom - tick top = nint(hbound/tick)*tick if (top.lt.hbound) top = top + tick return Given your numbers: -123.445 -> 1033.02, this routine returns TICK equal to 100.0, BOTTOM equal to -200.0, and TOP equal to 1100.0. The number of ticks in the result interval is always less than the divisor for LOWTICK plus one (in this case, the divisor is 12.5, so the maximum number of ticks is 13). The minimum number of ticks in the final interval is the LOWTICK divisor itself divided by 2.5. So, if you want finer resolution, pick a number larger than 12.5. If you want coarse resolution, pick a smaller number to divide by. You can even make the divisor one of the subroutine arguments if you want. Given your numbers:-123.445 -> 1033.02, and a divisor of 25, the answer is - TICK = 50.0, BOTTOM = -150.0, and TOP = 1050.0 giving 24 intervals. Not elegant, but not bad off the top of my head. J. Giles