comp.lang.ada
 help / color / mirror / Atom feed
From: dove!bruce.nist.gov@uunet.uu.net  (Jonathan Parker)
Subject: Re: Red-faced professor gets bitten in search for portability
Date: 22 Nov 91 21:10:01 GMT	[thread overview]
Message-ID: <1042@dove.nist.gov> (raw)

In article <1991Nov22.090650.28@v7.vitro.com> vaxs09@v7.vitro.com writes:
> In article <11919@spim.mips.COM>, murphy@mips.com (Mike Murphy) writes:
> > Why not just
> > check whether you indeed rounded down after doing the integer conversion?
> > For example:
> > 	FUNCTION Trunc (X: Float) RETURN Integer IS
> > 		itrunc : integer = integer(x - 0.5);
> > 	BEGIN
> > 		if x - float(itrunc) >= 1.0 then
> > 			-- rounded down, so add back a 1
> > 			return itrunc+1;
> > 		else
> > 			return itrunc;
> > 		end if;
> > 	END Trunc;
> >
> I think this version focuses more clearly on the original problem.
> 
> 	FUNCTION Trunc ( X : Float ) RETURN Integer IS
> 		i : integer := integer ( x );
> 		if x < float(i) then
> 			return i - 1;
> 		else
> 			return i;
> 		endif;
> 	END Trunc;
  There's still more to it, if I understand the Pascal Trunc function
correctly.  Trunc doesn't round, it truncates.  For example, Trunc(-3.999)
is -3, not -4.  Below is an excerpt from W. A. Whitaker's implementation
of the elementary math functions in Cody and Waite's famous book on the
subject.  I took this unmodified from files in the Ada repository on
SIMTEL-20. Floating is a generic Float type.   -- Jonathan  

    function Truncate (X : Floating) return Floating is
    --  Optimum code depends on how the system rounds at exact halves
        Z : Floating := Floating (Integer (X));
    begin
        if X = Z then
            return Z;
        else
            if X > Zero then
        --  For instance, you can get into trouble when 1.0E-20 is overwhelmed
        --  by HALF and X - HALF is -HALF and that is "truncated" to -1
        --  so a simple floating (Integer (X - Half)) does not work in all  
cases
                if X < Z then
                    return Z - One;
                else
                    return Z;
                end if;
            elsif X = Zero then
                return Zero;
            else
                if X < Z then
                    return Z;
                else
                    return Z + One;
                end if;
            end if;
        end if;
    end Truncate;

             reply	other threads:[~1991-11-22 21:10 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1991-11-22 21:10 Jonathan Parker [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 15:24 The Sunset Kid
1991-11-22 14:06 psinntp!vitro.com!v7.vitro.com!vaxs09
1991-11-22  2:29 micro-heart-of-gold.mit.edu!wupost!zaphod.mps.ohio-state.edu!uakari.prima
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