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: 6 Apr 93 19:47:04 GMT From: seas.gwu.edu!mfeldman@uunet.uu.net (Michael Feldman) Subject: Re: Ceiling function in Ada Message-ID: <1993Apr6.194704.25340@seas.gwu.edu> List-Id: In article karen@lobo.canberra.edu.au (Karen George) wri tes: >I'm a new user of Ada. > >But, in Ada, coercion to integer actually rounds (so says the >documentation) so how does one truncate a float in Ada? > This is from the math library developed by Broman. This library is pretty close to that specified by the Numerics Working Group, and will, I believe, become part of the Ada9X standard. To do truncation portably, without it being an intrinsic function supplied with a compiler that knows the target hardware, is not so easy, as you can see here. This version returns Float, but you can safely coerce the result to Integer without change, because the result is computed to be integral. Mike Feldman function truncate (x: Float) return Float is -- -- truncate x to the nearest integer value with absolute value -- not exceeding abs( x). No conversion to an integer type -- is expected, so truncate cannot overflow for large arguments. -- -- large: Float := 1073741824.0; type long is range - 1073741824 .. 1073741824; -- 2**30 is longer than any single-precision mantissa rd: Float; begin if abs( x) >= large then return x; else rd := Float ( long( x)); if x >= 0.0 then if rd <= x then return rd; else return rd - 1.0; end if; else if rd >= x then return rd; else return rd + 1.0; end if; end if; end if; end truncate;