comp.lang.ada
 help / color / mirror / Atom feed
* Integer truncation and other recipes
@ 1996-05-22  0:00 Paul Shedlarski
  1996-05-23  0:00 ` Jon S Anthony
  0 siblings, 1 reply; 7+ messages in thread
From: Paul Shedlarski @ 1996-05-22  0:00 UTC (permalink / raw)



In Ada, how do you convert from a real to an integer by truncation?
That is, how would you convert a value such as 1.9 to 1?

Moreover, is there a book of recipes that anyone can recommend for
development of a math library for Ada?  Most of us have a background
in FORTRAN.

Thanks.

Paul Shedlarski (code 5600)
David Taylor Model Basin
CARDEROCKDIV, NSWC
Bethesda, MD     20084




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

* Re: Integer truncation and other recipes
  1996-05-22  0:00 Integer truncation and other recipes Paul Shedlarski
@ 1996-05-23  0:00 ` Jon S Anthony
  1996-06-04  0:00   ` John English
                     ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Jon S Anthony @ 1996-05-23  0:00 UTC (permalink / raw)



In article <4nv3tmINNql5@oasys.dt.navy.mil> shedlars@oasys.dt.navy.mil (Paul Shedlarski) writes:

> In Ada, how do you convert from a real to an integer by truncation?
> That is, how would you convert a value such as 1.9 to 1?

Per the ARM (found using the HTML version so I could cut and paste
for demonstration...):

Go to index and find entry:

  Truncation attribute 
         A.5.3(42), K(248) 

Go to A.5.3(42) and find:

  (42)
      S'Truncation 
  	  S'Truncation denotes a function with the following specification: 
  
  (43)
  	  function S'Truncation (X : T)
  	    return T
  
  (44)
  	     The function yields the value Ceiling(X) when X is
  	     negative, and Floor(X) otherwise. A zero result has
  	     the sign of X when S'Signed_Zeros is True.


In this case, "going and finding" is just "pointing and clicking".
I'm not saying, "hey RTFM", I'm just trying to show that getting a
copy of the ARM for this sort of thing is much more time saving and
easy than posting for such answers...

BTW, I think you're going to like Ada95.

/Jon

-- 
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178

617.484.3383
jsa@organon.com





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

* Re: Integer truncation and other recipes
  1996-05-23  0:00 ` Jon S Anthony
  1996-06-04  0:00   ` John English
@ 1996-06-04  0:00   ` Jon S Anthony
  1996-06-05  0:00     ` Norman H. Cohen
  1996-06-05  0:00   ` Robert I. Eachus
  1996-06-08  0:00   ` Jon S Anthony
  3 siblings, 1 reply; 7+ messages in thread
From: Jon S Anthony @ 1996-06-04  0:00 UTC (permalink / raw)



In article <DsGC9A.4KD@bton.ac.uk> je@bton.ac.uk (John English) writes:

> : > In Ada, how do you convert from a real to an integer by truncation?
> : > That is, how would you convert a value such as 1.9 to 1?
> : Go to index and find entry:
> :   Truncation attribute 
> 
> Fine for floating point types, but you're out of luck if you want to truncate 
> *fixed point* types, for which the 'Truncation attribute isn't available...

First convert to floating point...

/Jon
-- 
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178

617.484.3383
jsa@organon.com





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

* Re: Integer truncation and other recipes
  1996-05-23  0:00 ` Jon S Anthony
@ 1996-06-04  0:00   ` John English
  1996-06-04  0:00   ` Jon S Anthony
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: John English @ 1996-06-04  0:00 UTC (permalink / raw)



Jon S Anthony (jsa@organon.com) wrote:
: In article <4nv3tmINNql5@oasys.dt.navy.mil> shedlars@oasys.dt.navy.mil (Paul S
: > In Ada, how do you convert from a real to an integer by truncation?
: > That is, how would you convert a value such as 1.9 to 1?
: Go to index and find entry:
:   Truncation attribute 

Fine for floating point types, but you're out of luck if you want to truncate 
*fixed point* types, for which the 'Truncation attribute isn't available...

-- 
----------------------------------------------------------------------------
John English <je@brighton.ac.uk>, Dept. of Computing, University of Brighton
  "Disks are divided into sex and tractors..."
----------------------------------------------------------------------------




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

* Re: Integer truncation and other recipes
  1996-06-04  0:00   ` Jon S Anthony
@ 1996-06-05  0:00     ` Norman H. Cohen
  0 siblings, 0 replies; 7+ messages in thread
From: Norman H. Cohen @ 1996-06-05  0:00 UTC (permalink / raw)



In article <JSA.96Jun4154923@organon.com>, jsa@organon.com (Jon S Anthony)
writes: 

|> In article <DsGC9A.4KD@bton.ac.uk> je@bton.ac.uk (John English) writes: 
|>
|> > : > In Ada, how do you convert from a real to an integer by truncation?
|> > : > That is, how would you convert a value such as 1.9 to 1?
|> > : Go to index and find entry: 
|> > :   Truncation attribute
|> >
|> > Fine for floating point types, but you're out of luck if you want to truncate
|> > *fixed point* types, for which the 'Truncation attribute isn't available...
|>
|> First convert to floating point...

Not a very satisfying answer.  Suppose you want to delay until the next
time the minute hand sweeps past the 12.  It would be nice to be able to
write: 

   declare
      Now         : Duration := Ada.Calendar.Seconds(Ada.Calendar.Clock);
      This_Minute : Duration := Duration'Floor(Now/60.0) * 60.0;
                    -- ILLEGAL!!! There is not really a Duration'Floor
                    --    attribute!
      Next_Minute : Duration := This_Minute + 60.0;
   begin
      delay until Next_Minute;
   end;

Duration(Float'Floor(Float(Now)/60.0)) is a poor substitute for
Duration'Floor(Now/60.0).

I think the failure to generalize certain of the floating-point
attributes to fixed-point types was an unfortunate oversight.

--
Norman H. Cohen    ncohen@watson.ibm.com




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

* Re: Integer truncation and other recipes
  1996-05-23  0:00 ` Jon S Anthony
  1996-06-04  0:00   ` John English
  1996-06-04  0:00   ` Jon S Anthony
@ 1996-06-05  0:00   ` Robert I. Eachus
  1996-06-08  0:00   ` Jon S Anthony
  3 siblings, 0 replies; 7+ messages in thread
From: Robert I. Eachus @ 1996-06-05  0:00 UTC (permalink / raw)



In article <4p4atg$tc5@watnews1.watson.ibm.com> ncohen@watson.ibm.com (Norman H. Cohen) writes:

  > Not a very satisfying answer.  Suppose you want to delay until the next
  > time the minute hand sweeps past the 12.  It would be nice to be able to
  > write...

  > Duration(Float'Floor(Float(Now)/60.0)) is a poor substitute for
  > Duration'Floor(Now/60.0).

  > I think the failure to generalize certain of the floating-point
  > attributes to fixed-point types was an unfortunate oversight.

   Agreed.  In fact on some implementations:

   60.0 * Duration(Integer(Now/Duration'SMALL)/
               (60*Integer(1.0/Duration'SMALL)))

   Is the only reasonable way to do this due to accuracy issues.
However, that approach depends on (in Ada 83) the UI which says that
1.0 is a model number of Duration.  If fact I have had to replace the
divisions by Duration'SMALL by Unchecked_Conversion with some
compilers that simply do not recognize this idiom.  (The problem is
not that they do a divide, it's that they do a divide which
overflows...) 


--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




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

* Re: Integer truncation and other recipes
  1996-05-23  0:00 ` Jon S Anthony
                     ` (2 preceding siblings ...)
  1996-06-05  0:00   ` Robert I. Eachus
@ 1996-06-08  0:00   ` Jon S Anthony
  3 siblings, 0 replies; 7+ messages in thread
From: Jon S Anthony @ 1996-06-08  0:00 UTC (permalink / raw)



In article <4p4atg$tc5@watnews1.watson.ibm.com> ncohen@watson.ibm.com (Norman H. Cohen) writes:

> Duration(Float'Floor(Float(Now)/60.0)) is a poor substitute for
> Duration'Floor(Now/60.0).

In what respect?  Losing what you are really after in a sea of extraneous
junk?  If so, the simple fix is to put it in a function (yeah, I know,
everyone will have to hack this one off).


> I think the failure to generalize certain of the floating-point
> attributes to fixed-point types was an unfortunate oversight.

Well, I certainly won't disagree with this.

/Jon
-- 
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178

617.484.3383
jsa@organon.com





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

end of thread, other threads:[~1996-06-08  0:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-05-22  0:00 Integer truncation and other recipes Paul Shedlarski
1996-05-23  0:00 ` Jon S Anthony
1996-06-04  0:00   ` John English
1996-06-04  0:00   ` Jon S Anthony
1996-06-05  0:00     ` Norman H. Cohen
1996-06-05  0:00   ` Robert I. Eachus
1996-06-08  0:00   ` Jon S Anthony

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