comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: Truncation of FLOAT values
Date: 1996/04/22
Date: 1996-04-22T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.96Apr22160328@spectre.mitre.org> (raw)
In-Reply-To: 4l8a6h$274@newsbf02.news.aol.com

In article <dewar.829958772@schonberg> dewar@cs.nyu.edu (Robert Dewar) writes:

  > John Herro said

  > "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;"

  > Although this will typically work, Ada semantics do not guarantee that
  > it will work.
 
   While Professer Dewar is quite correct, a more helpful answer is:

    function Floor(X : in Float) return Integer is
      Answer : Integer;
    begin
      Answer := Integer(X);
      if Float(Answer) - X > 0.0 then return Answer - 1; end if;
      return Answer;
   exception
     when Constraint_Error =>
      if  X - Float(Integer'LAST - 1) > 0.0
      then if X - Float(Integer'LAST) < 0.0
           then return Integer'LAST - 1;
           elsif X - Float(Integer'LAST) < 1.0
           then return Integer'LAST;
           else raise;
           end if;
      elsif Float(Integer'FIRST + 1) - X < 0.0
      then if Float(Integer'FIRST) - X >= 0.0
      then return Integer'FIRST;
      else raise;
      end if;
   end Floor;

   Almost all floating point compares are against zero, the exception
is the case where you are testing for X < Integer'LAST + 1 in the
three special cases dealt with in the exception handler.  Yes, there
are still cases where the result returned by this function will not be
"mathematically correct" but they will only occur when X is converted
to a model number whose fractional part is zero as a side effect of
the call.  In other words on most hardware, Floor(1_000_000_000.0 -
Float'Epsilon) will return 1_000_000_000.  (On most other
implementations, it will raise an exception.)  This is a known problem
with any implementation of floating point arithmetic, and has to be
dealt with in the design of floating point algorithms.

--

					Robert I. Eachus

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




      parent reply	other threads:[~1996-04-22  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   ` Robert Dewar
1996-04-20  0:00     ` Cordes MJ
1996-04-21  0:00       ` Robert Dewar
1996-04-19  0:00   ` Peter Hermann
1996-04-19  0:00   ` John Herro
1996-04-19  0:00     ` Robert Dewar
1996-04-22  0:00       ` John Herro
1996-04-22  0:00     ` Robert I. Eachus [this message]
replies disabled

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