public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Comparing doubles
@ 2002-07-01 11:16 Lars Brinkhoff
  2002-07-01 11:55 ` Geoff Keating
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Lars Brinkhoff @ 2002-07-01 11:16 UTC (permalink / raw)
  To: gcc

Does the C standard define exactly how floating-point comparisons
should work?  Sections 6.5.8 Relational operators and 6.5.9 Equality
operators in C99 doesn't spell out any details.

In particular, would it be acceptable for a GCC back end to compare
two doubles by subtracting them, converting the result to float, and
then compare the result against zero?

^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: Comparing doubles
@ 2002-07-06 10:28 Robert Dewar
  0 siblings, 0 replies; 16+ messages in thread
From: Robert Dewar @ 2002-07-06 10:28 UTC (permalink / raw)
  To: gcc, lars

> In particular, would it be acceptable for a GCC back end to compare
> two doubles by subtracting them, converting the result to float, and
> then compare the result against zero?

no, this is not even approximately correct

^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: Comparing doubles
@ 2002-07-06 10:58 Robert Dewar
  2002-07-07  9:09 ` Paul Koning
  0 siblings, 1 reply; 16+ messages in thread
From: Robert Dewar @ 2002-07-06 10:58 UTC (permalink / raw)
  To: geoffk, lars; +Cc: gcc

> If you define __STDC_IEC_559__, no.  Otherwise, you can do whatever
> you like, although it might give a less-than-useful FP implementation.

Well by this viewpoint, you can implement addition by doing a subtraction.
Although a fully formal view of the standard might be consistent with this
approach, in practice we expect + to do an addition, and we expect equality
to do an equality operation, both in the usually understood informal meanings
of the terms, i.e. + translates to a floating-point add, and = translates
to a floating-point equals. Doing floating-point equality by subtraction
and test for zero is just too far from this expectation on any modern
machine.

^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: Comparing doubles
@ 2002-07-07  9:17 Robert Dewar
  2002-07-08  3:10 ` Kai Henningsen
  0 siblings, 1 reply; 16+ messages in thread
From: Robert Dewar @ 2002-07-07  9:17 UTC (permalink / raw)
  To: dewar, pkoning; +Cc: gcc, geoffk, lars

<<In any case, I don't understand these points.  Certainly not the one
about subtraction.  The only reasonable expectation is that the
implementation should produce results that conform to the standard to
which it is implemented.  Whether addition is done by complement
followed by subtract, or subtract is done by complement and add, or
neither is true, is irrelevant so long as the answer is right.
>>

YOu missed my point.

First, what do we mean by "answer is right". Well the standard does not
define this. At one level, doing addition by complement followed by
subtract is obviously wrong since it does not give proper IEEE minus
zero semantics. At the other end, doing a subtraction *instead* of
an addition (which is what I was referring to) gives obviously drastically
wrong results. But the standard has little to say about what is right or
wrong, so we really can't turn to the standard in judging what is 
reasonably conformant in this respect.

In the case of equality, it is reasonable to think that 

a) you should be able to compare any valid fpt numbers
b) you should get true if and only if the numbers are the same

doing comparisons by subtraction violates both these expectations, so
at least on a machine where it is reasonably possible to do a) and b)
then any compiler which does not do a) and b) is in my view an incorrect
implementation.

Note that it is often possible to do floating-point comparisons in the
informal sense of a) and b) with integer comparison operations.
(the only glitch being signed zeros)

^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: Comparing doubles
@ 2002-07-07  9:20 Robert Dewar
  0 siblings, 0 replies; 16+ messages in thread
From: Robert Dewar @ 2002-07-07  9:20 UTC (permalink / raw)
  To: dewar, pkoning; +Cc: gcc, geoffk, lars

<<As for compare, I don't claim to know the intricacies of IEEE NaN and
all that, but surely on older float formats at least, compare is
indeed internally just a subtract and test for zero?  Or a normalize
(if applicable) and bitwise compare?  (The latter is definitely the
case on the CDC 6600, to use an old example.)
>>

No, because a subtract and test for zero is subject to problems with both
underflow and overflow and can therefore result in considering unequal
numbers to be equal.

^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: Comparing doubles
@ 2002-07-07 21:37 Robert Dewar
  2002-07-07 22:27 ` Tim Hollebeek
  0 siblings, 1 reply; 16+ messages in thread
From: Robert Dewar @ 2002-07-07 21:37 UTC (permalink / raw)
  To: pkoning, tim; +Cc: dewar, gcc, geoffk, lars

<<Truncation before compare allows {a==b, b==c, c!=a}, and all other
sorts of unreasonable things.  Regardless of whether the standard
strictly requires it or not, it is reasonable to expect equality to be
an equivalence operation.
>>

But it is not necessarily the case that a==a (in the case of a NaN) so
your equivalence relation does not cover NaN's. That's still reasonable,
but it is worth noticing the exception.

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

end of thread, other threads:[~2002-07-08  7:03 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-01 11:16 Comparing doubles Lars Brinkhoff
2002-07-01 11:55 ` Geoff Keating
2002-07-01 23:24   ` Lars Brinkhoff
2002-07-02  7:48     ` Lars Brinkhoff
2002-07-01 12:13 ` Tim Hollebeek
2002-07-01 12:18 ` Alan Lehotsky
2002-07-06 10:28 Robert Dewar
2002-07-06 10:58 Robert Dewar
2002-07-07  9:09 ` Paul Koning
2002-07-07 18:06   ` Tim Hollebeek
2002-07-08  2:19     ` Lars Brinkhoff
2002-07-07  9:17 Robert Dewar
2002-07-08  3:10 ` Kai Henningsen
2002-07-07  9:20 Robert Dewar
2002-07-07 21:37 Robert Dewar
2002-07-07 22:27 ` Tim Hollebeek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).