From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 22 Nov 91 02:29:26 GMT From: micro-heart-of-gold.mit.edu!wupost!zaphod.mps.ohio-state.edu!uakari.prima te.wisc.edu!usenet.coe.montana.edu!milton!mfeldman@bloom-beacon.mit.edu (Micha el Feldman) Subject: Re: Red-faced professor gets bitten in search for portability Message-ID: <1991Nov22.022926.14175@milton.u.washington.edu> List-Id: In article daves@assip.csasyd.oz.au writes: [ stuff deleted ] > > FUNCTION Trunc (X: Float) RETURN Integer IS > BEGIN > RETURN Integer(2.0 * X) / 2; > END Trunc; > >still takes the integer before the division. In fact, as you've now doubled t he >number, the horrible rounding beast is twice as active (i.e. 0.25 and 0.75 >both fall victim). Indeed. Oops. > >I think Mike Murphy's code looks awkward because it's sort of upside down. Th e >problem can be restated as: "if integer () rounds up, return one less than the >value of integer (), otherwise return the value of integer ()". > >This translates to: > > function trunc (number: float) return integer is > begin > if float (integer (number)) > number then -- does it round up? > return integer (number) - 1; -- yes - stop it! > else > return integer (number); -- no - ok as it is. > end if; > end trunc; > >(I'm assuming that my compiler is clever enough to eliminate the redundant con versions to integer.) > Better and simpler. Portable, too, I think. Mike Feldman