comp.lang.ada
 help / color / mirror / Atom feed
From: johnherro@aol.com (John Herro)
Subject: Re: Truncation of FLOAT values
Date: 1996/04/19
Date: 1996-04-19T00:00:00+00:00	[thread overview]
Message-ID: <4l8a6h$274@newsbf02.news.aol.com> (raw)
In-Reply-To: 4l6bjn$b96@cliffy.lfwc.lockheed.com

Franz.Kruse@erno.de (Franz Kruse) writes:
> Is there a simple way in Ada 83 to determine
> the integer part or the fraction part of a FLOAT
> value?  A conversion to INTEGER cannot be
> used, because it rounds.

     Here are two functions from my Ada Tutor program that will work in
Ada 83.  The two functions behave the same for positive Floats but
differently for negative Floats.  This first function Floor computes the
largest Integer less than or equal to a given Float, always truncating
toward negative infinity:

function Floor(X : in Float) return Integer is
   Answer : Integer := Integer(X);
begin
   if Float(Answer) > X then
      Answer := Answer - 1;
   end if;
   return Answer;
end Floor;

This second function always truncates toward zero:

function Truncate(X : in Float) return Integer is
   Answer : Integer := Integer(X);
begin
   if Answer > 0 and Float(Answer) > X then
      Answer := Answer - 1;
   elsif Answer < 0 and Float(Answer) < X then
      Answer := Answer + 1;
   end if;
   return Answer;
end Truncate;

     Needless to say, getting the fractional part of a Float after you
have the Integer part is trivial - just convert the Integer back to Float
and subtract from the original number.  I hope this helps.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




  parent reply	other threads:[~1996-04-19  0:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-04-18  0:00 Truncation of FLOAT values Franz Kruse
1996-04-18  0:00 ` Cordes MJ
1996-04-19  0:00   ` Peter Hermann
1996-04-19  0:00   ` John Herro [this message]
1996-04-19  0:00     ` Robert Dewar
1996-04-22  0:00       ` John Herro
1996-04-22  0:00     ` Robert I. Eachus
1996-04-19  0:00   ` Robert Dewar
1996-04-20  0:00     ` Cordes MJ
1996-04-21  0:00       ` Robert Dewar
replies disabled

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