comp.lang.ada
 help / color / mirror / Atom feed
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
Date: 22 Nov 91 02:29:26 GMT	[thread overview]
Message-ID: <1991Nov22.022926.14175@milton.u.washington.edu> (raw)

In article <daves.690605701@condor> 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

             reply	other threads:[~1991-11-22  2:29 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1991-11-22  2:29 micro-heart-of-gold.mit.edu!wupost!zaphod.mps.ohio-state.edu!uakari.prima [this message]
  -- strict thread matches above, loose matches on Subject: below --
1991-11-25  6:09 Red-faced professor gets bitten in search for portability csus.edu!wupost!spool.mu.edu!munnari.oz.au!metro!cluster!swift!sunaus!ass
1991-11-22 22:16 Dik T. Winter
1991-11-22 22:11 Dik T. Winter
1991-11-22 21:10 Jonathan Parker
1991-11-22 15:24 The Sunset Kid
1991-11-22 14:06 psinntp!vitro.com!v7.vitro.com!vaxs09
1991-11-20 23:59 micro-heart-of-gold.mit.edu!wupost!sdd.hp.com!uakari.primate.wisc.edu!use
1991-11-20  2:55 csus.edu!wupost!cs.utexas.edu!sun-barr!cronkite.Central.Sun.COM!newstop!s
1991-11-19 15:06 Norman H. Cohen
1991-11-16  0:01 Michael Feldman
1991-11-15 20:58 Mike Murphy
1991-11-15 18:59 Michael Feldman
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox