comp.lang.ada
 help / color / mirror / Atom feed
From: seas.gwu.edu!mfeldman@uunet.uu.net  (Michael Feldman)
Subject: Re: Ceiling function in Ada
Date: 6 Apr 93 19:47:04 GMT	[thread overview]
Message-ID: <1993Apr6.194704.25340@seas.gwu.edu> (raw)

In article <karen.734100643@lobo> 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;

             reply	other threads:[~1993-04-06 19:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-04-06 19:47 Michael Feldman [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-04-10  8:03 Ceiling function in Ada pipex!bnr.co.uk!demon!cix.compulink.co.uk!sjwright
1993-04-07 12:55 Tucker Taft
1993-04-06 12:50 agate!dog.ee.lbl.gov!network.ucsd.edu!munnari.oz.au!csis!dubhe.anu.edu.au
replies disabled

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