From: milton!mfeldman@beaver.cs.washington.edu (Michael Feldman)
Subject: Red-faced professor gets bitten in search for portability
Date: 15 Nov 91 18:59:59 GMT [thread overview]
Message-ID: <1991Nov15.185959.5002@milton.u.washington.edu> (raw)
Asked by students whether Ada has an equivalent of the Pascal "Trunc"
operation, which just returns the integer part of its argument,
I put together a function based on the old trick we used
in the Fortran days.
FUNCTION Trunc (X: Float) RETURN Integer IS
BEGIN
RETURN Integer(X - 0.5);
END Trunc;
Since conversion of Float to Integer is, in Ada, a rounding operation,
this looks like a good solution, right? WRONG! The trouble is that,
according to the LRM, the result of the conversion is implementation-
dependent if the fractional part of the float quantity lies just
between the two integers (that is, = 0.5). (LRM sect. 4.6)
To see the hidden nastiness here, suppose Y has an integral value.
If Y = 10.0 (say), then Trunc(Y) returns 10 on compilers where the 0.5
case rounds _up_, and returns 9 on compilers where the 0.5 case rounds
_down_. Oops! A simple bit of code falls right into a portability trap.
The only solution seems to lie in forcing an integer division, since integer
division indeed truncates, portably. So our new function is
FUNCTION Trunc (X: Float) RETURN Integer IS
BEGIN
RETURN Integer(2.0 * X) / 2;
END Trunc;
which, I suppose, can be optimized efficiently, but seems like a
kludgy way to do a simple truncation. I think this is an example of
an operation that's much more easily done as an intrinsic than as
a programmer-defined function.
Any thoughts in net-land?
By the way - I am all the more red-faced because I let the dangerous
Trunc monster loose into a textbook. 'Course, I haven't run into
another Ada text that even discusses the question...
Always in search of the portable Ada program, I remain
Mike
-------------------------------------------------------------------------------
Michael B. Feldman
Visiting Professor 1991-92 Professor
Dept. of Comp. Sci. and Engrg. Dept. of Elect. Engrg. and Comp. Sci.
University of Washington FR-35 The George Washington University
Seattle, WA 98105 Washington, DC 20052
mfeldman@cs.washington.edu mfeldman@seas.gwu.edu
(206) 632-3794 (voice) (202) 994-5253 (voice)
(206) 543-2969 (fax) (202) 994-5296 (fax)
-------------------------------------------------------------------------------
next reply other threads:[~1991-11-15 18:59 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
1991-11-15 18:59 Michael Feldman [this message]
-- strict thread matches above, loose matches on Subject: below --
1991-11-15 20:58 Red-faced professor gets bitten in search for portability Mike Murphy
1991-11-16 0:01 Michael Feldman
1991-11-19 15:06 Norman H. Cohen
1991-11-20 2:55 csus.edu!wupost!cs.utexas.edu!sun-barr!cronkite.Central.Sun.COM!newstop!s
1991-11-20 23:59 micro-heart-of-gold.mit.edu!wupost!sdd.hp.com!uakari.primate.wisc.edu!use
1991-11-22 2:29 micro-heart-of-gold.mit.edu!wupost!zaphod.mps.ohio-state.edu!uakari.prima
1991-11-22 14:06 psinntp!vitro.com!v7.vitro.com!vaxs09
1991-11-22 15:24 The Sunset Kid
1991-11-22 21:10 Jonathan Parker
1991-11-22 22:11 Dik T. Winter
1991-11-22 22:16 Dik T. Winter
1991-11-25 6:09 csus.edu!wupost!spool.mu.edu!munnari.oz.au!metro!cluster!swift!sunaus!ass
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox