Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!van-bc!ubc-cs!cheddar.ucs.ubc.ca!buckland From: buckland@cheddar.ucs.ubc.ca (Tony Buckland) Newsgroups: comp.lang.fortran Subject: Re: rounding a real to a whole number Message-ID: <10292@ubc-cs.UUCP> Date: 1 Nov 90 19:28:00 GMT References: Sender: news@cs.ubc.ca Reply-To: buckland@cheddar.ucs.ubc.ca (Tony Buckland) Organization: UBC Computing Centre, Vancouver, B.C., Canada Lines: 30 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. optionally, picking a 10-multiple of whole numbers ought to |be included; for -1.45567 -> 3.34112 , it should return -1.4 and 3.4 ... | |while this seems so simple, I don't see a way to do this reasonably with |Fortran intrinsics. I just can't get it to work correctly with truncations. | |anyone done this sort of thing listening out there? I imagine you meant -1.5, not -1.4. That quibble aside, a traditional way of rounding up is to add a floating-point quantity before truncating to integer. Watch what happens in this example: f = 3.34112 unrounded quantity f = f + 0.09999 add some variation on "just less than one" f = 10.0 * f we want to keep two digits f is now 34.4111 i = f i is now 34 f = i f is now 34.0000 f = f / 10.0 f is now 3.4 The negative case is left as an exercise for the student. The problem with inexact representation of floating-point machine representation of most non-integers is ignored.