comp.lang.ada
 help / color / mirror / Atom feed
* Ceiling function in Ada
@ 1993-04-06 12:50 agate!dog.ee.lbl.gov!network.ucsd.edu!munnari.oz.au!csis!dubhe.anu.edu.au
  0 siblings, 0 replies; 4+ messages in thread
From: agate!dog.ee.lbl.gov!network.ucsd.edu!munnari.oz.au!csis!dubhe.anu.edu.au @ 1993-04-06 12:50 UTC (permalink / raw)


I'm a new user of Ada.  

My immediate problem is to implement the 
ceiling function - also called the next integer function.
I.e., for x a float, 1.0 < x <= 2.0, ceil(x) returns 2, etc.

My "real" problem is how to obtain only the integer part of the
floating point number.  In Pascal, and other languages, one can 
use a truncate function or coerce x to an integer which truncates
the fractional part. 

But, in Ada, coercion to integer actually rounds (so says the 
documentation) so how does one truncate a float in Ada?

Please don't bother suggesting adding 0.5 to x.  Not only is 
Integer(1.5) implementation dependant (so says the
documentation), but it is of no value anyway.  If Integer(1.5)
returns 2, then it's wrong for the ceiling function and I have
no means of determining that.  If it returns 1, then my function is
still implementation dependant and therefore not portable.

Thanks 

--
Karen George 				Email: 	karen@lobo.canberra.edu.au
Information Sciences and Engineering	Phone: 	+61 6 201 2430
University of Canberra,	AUSTRALIA 	FAX:	+61 6 201 5227
--
email: karen@ise.canberra.edu.au
       karen@lobo.canberra.edu.au
       karen@abel.anu.edu.au

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ceiling function in Ada
@ 1993-04-06 19:47 Michael Feldman
  0 siblings, 0 replies; 4+ messages in thread
From: Michael Feldman @ 1993-04-06 19:47 UTC (permalink / 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;

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ceiling function in Ada
@ 1993-04-07 12:55 Tucker Taft
  0 siblings, 0 replies; 4+ messages in thread
From: Tucker Taft @ 1993-04-07 12:55 UTC (permalink / raw)


In article <karen.734158490@lobo> 
  karen@lobo.canberra.edu.au (Karen George) writes:

>On the other hand, why wouldn't a high level language provide a
>function as basic as truncate.  This is an easy task at machine level,
>and is not an uncommon task.  . . .

In hindsight, it was a mistake in Ada 83 not to define
a standard package for support of floating point operations
such as truncate, ceiling, etc., as well as the elementary
functions.  At the time it was felt that there was not
a great need to standardize a lot of packages.  In more recent
years, language standards have devoted more and more "real estate"
to standard libraries.  In ANSI C, the standard libraries are
about half the standard.  In Common Lisp, of course, the standard
libraries are most of the standard (since there is almost no syntax
to speak of).

For Ada 9X, we have realized that standard packages can be just as
important as some bit of syntax, and a full set of primitive
and elementary functions for floating point types will be provided,
including Truncation, Ceiling, Floor, Rounding, Sin, Cos, ...
Hopefully this will avoid some of the wheel reinvention that
took place with Ada 83, and also encourage implementations to
map such standard operations directly to the appropriate hardware 
instructions.

In the meantime, there is a standard set of Generic Primitive Functions
that has been defined for Ada 83, and is progressing through the
ISO standardization process by itself.  Some vendors should be
supporting it already.

>Karen George 				Email: karen@lobo.canberra.edu.au
>Information Sciences and Engineering	Phone: 	+61 6 201 2430
>University of Canberra,	AUSTRALIA 	FAX:	+61 6 201 5227

S. Tucker Taft     stt@inmet.com
Ada 9X Mapping/Revision Team
Intermetrics, Inc.
Cambridge, MA  02138

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ceiling function in Ada
@ 1993-04-10  8:03 pipex!bnr.co.uk!demon!cix.compulink.co.uk!sjwright
  0 siblings, 0 replies; 4+ messages in thread
From: pipex!bnr.co.uk!demon!cix.compulink.co.uk!sjwright @ 1993-04-10  8:03 UTC (permalink / raw)


References: <karen.734158490@lobo>
X-Newsreader: TIN [version 1.1 PL6]

Karen George (karen@lobo.canberra.edu.au) wrote:
....
> On the other hand, why wouldn't a high level language provide a
> function as basic as truncate.  This is an easy task at machine level,
> and is not an uncommon task.  The code above is obvious, (after you get
> over the shock of finding out that Integer(x) actually rounds) but is
> less efficient (is efficiency important anymore?) than a straight
> translation to the machine function.
...

We used to produce two variants of the same computer where one (the earlier)
had a single-length Float->Fix instruction that truncated, the other had a
double-length Float->Fix that rounded. The latter always seemed so much more
sensible that I would have been shocked to find Integer (x) truncating!

--
Simon Wright
Ferranti International, Defence Systems Integration

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1993-04-10  8:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-04-10  8:03 Ceiling function in Ada pipex!bnr.co.uk!demon!cix.compulink.co.uk!sjwright
  -- strict thread matches above, loose matches on Subject: below --
1993-04-07 12:55 Tucker Taft
1993-04-06 19:47 Michael Feldman
1993-04-06 12:50 agate!dog.ee.lbl.gov!network.ucsd.edu!munnari.oz.au!csis!dubhe.anu.edu.au

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