comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: Can compilers do this?
Date: 1996/02/26
Date: 1996-02-26T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.96Feb26114315@spectre.mitre.org> (raw)
In-Reply-To: 4gjd6g$mfq@newsbf02.news.aol.com

In article <4gjd6g$mfq@newsbf02.news.aol.com> bwburnsed@aol.com (BWBurnsed) writes:

  > I came across some very strange looking code that someone else wrote,
  > long, long ago, and (apparently) in a universe far, far away. But before I

  > make too many critical comments, I want to be sure I'm not missing
  > something.

  > Repeatedly in this code (in many files), there are places where a floating
  > point variable is tested to see if it is negative. However, the way it is
  > done is:

  >	if  X * abs(X)  <  0.0  then  ...

  > Is there (or was there ever) some pathological anomaly about
  > floating point implementations that would make a conversion (abs)
  > and floating point multiply preferable to testing a sign bit? Can,
  > and will, optimizing compilers recognize the real test desired in
  > such constructs, i.e. reduce it to a sign bit test?

   A lot of answers have shown up here which focused on hardware which
supports negative zeros.  But anyone with a through understanding of
floating-point should recognize that there are many legitimate
negative numbers for which this expression is false.  In fact it
becomes much more understandable if written:

   if X < 0.0 and then X * X > Float'Model_Small then  ...

  (Replace Model_Small with Safe_Small in Ada 83, and Float with the
appropriate type name if it isn't Float.)

   But of course this is just a (portable) approximation to the
behavior on your hardware.  In any case it looks as if the author of
the code is trying to avoid treating small deviations from zero,
probably due to floating point arithmetic errors, as negative.

   Of course if this is the intended effect, the "best" portable
source would be:

   Fuzz: constant Float := Sqrt(Float'Model_Small);  -- or whatever
   ...
   if X + Fuzz < 0.0 then ...

--

					Robert I. Eachus

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




  parent reply	other threads:[~1996-02-26  0:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-02-22  0:00 Can compilers do this? BWBurnsed
1996-02-23  0:00 ` Robert Dewar
1996-02-23  0:00 ` Mark A Biggar
1996-02-24  0:00   ` Robert A Duff
1996-02-23  0:00     ` Robert Dewar
1996-02-25  0:00   ` Robert Dewar
1996-02-23  0:00 ` Stuart Palin
1996-02-23  0:00 ` Robert Dewar
1996-02-23  0:00 ` Cordes MJ
1996-02-26  0:00 ` Robert I. Eachus [this message]
1996-02-26  0:00   ` BWBurnsed
  -- strict thread matches above, loose matches on Subject: below --
1996-02-26  0:00 Marin David Condic, 407.796.8997, M/S 731-93
replies disabled

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