From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE, MSGID_SHORT autolearn=no autolearn_force=no version=3.4.4 Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!wayback!arny From: arny@wayback.UUCP (Arny B. Engelson) Newsgroups: net.lang.ada Subject: Re: Getting the integer part of a real Message-ID: <996@wayback.UUCP> Date: Fri, 7-Nov-86 09:27:50 EST Article-I.D.: wayback.996 Posted: Fri Nov 7 09:27:50 1986 Date-Received: Sat, 8-Nov-86 06:42:59 EST References: <38000034@gypsy.UUCP> Organization: AT&T Bell Labs, Whippany, NJ Summary: This only works for POSITIVE numbers! List-Id: Dave Emery writes: > > When we discovered the 'feature' in Ada that an implementor can pick how > he rounds, we discussed various ways to get the integer part of a number. > Here is the 'best' (meaning most portable) way: (p.s. this 'algorithm' > is obviously language-independent) > > declare > x : real; > i : integer; -- integer part of x > begin > i := integer(x); > if (i > x) then > -- machine rounded up > i := i - 1; > end if; > end; > The above algorithm only works for POSITIVE values of X! For example, if X = -0.7 this will return -1 instead of 0. The "integer part of a real number" is a truncate function: function Trunc (X : Float) return Integer is I : Integer := Integer (X); begin if I > 0 and then Float(I) > X then I := I - 1; elsif I < 0 and then Float(I) < X then I := I + 1; end if; return I; end Trunc; This should return whatever is before the decimal point in a floating point number. Arny B. Engelson {ihnp4 | bonnie | clyde } wayback!arny (201) 386-4816