Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!samsung!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.fortran Subject: Re: rounding a real to a whole number Message-ID: <4191@goanna.cs.rmit.oz.au> Date: 2 Nov 90 08:50:56 GMT References: <67611@bu.edu.bu.edu> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 41 In article <67611@bu.edu.bu.edu>, merritt@buphy.BU.EDU (Sean Merritt) writes: > 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. > C = INT(A*10**B+0.5)/10**B C The original poster wanted something that rounds AWAY from zero. SUBROUTINE IRANGE(XLO, XHI, ILO, IHI, IERROR) C Imagine we are given REAL XLO, XHI C and are required to find INTEGER ILO, IHI C such that C ILO.LE.XLO .AND. XHI.LE.IHI C Here's how. IF (XLO .GT. XHI) THEN IERROR = 1 ELSE IF (XHI .GT. MAXINT) THEN C Where do you get MAXINT? That's _your_ business. IERROR = 2 ELSE IF (XLO .LT. MININT) THEN C Where do you get MININT? Try MACHAR. IERROR = 3 ELSE IHI = INT(XHI) IF (IHI .LT. XHI) IHI = IHI+1 ILO = INT(XLO) IF (ILO .GT. XLO) ILO = ILO-1 IERROR = 0 END IF END Crude. Totally lacking in imagination. But it _does_ "choose whole numbers _outside_ the range" as requested. -- The problem about real life is that moving one's knight to QB3 may always be replied to with a lob across the net. --Alasdair Macintyre.