This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Incorrect runtime behavior involving comparison
- To: austern at sgi dot com
- Subject: Re: Incorrect runtime behavior involving comparison
- From: "Martin v. Loewis" <martin at loewis dot home dot cs dot tu-berlin dot de>
- Date: Tue, 18 Jan 2000 01:47:33 +0100
- CC: gcc-bugs at gcc dot gnu dot org, vonbrand at sleipnir dot valparaiso dot cl, austern at sgi dot com, lo at cthulhu dot engr dot sgi dot com
- References: <200001102331.UAA30679@sleipnir.valparaiso.cl> <10001101540.ZM70875@isolde.engr.sgi.com>
> No. Floating-point arithmetic obeys different rules than the arithmetic
> of real numbers, but it is neither magical nor (on most architectures)
> nondeterministic.
>
> It's consistent for 'x > y' and 'y > x' both to be false. (It
> means that x and y are equal, or else that either x or y is NaN.)
> It is not consistent for 'x > y' and 'y > x' both to be true,
> which is the behavior we're seeing.
This is not a bug in gcc. gcc uses the default settings of the FPU on
the operating system, which means that in-register temporaries have 80
bits precision, and in-memory values have 64 bit precision. If both x
and y are temporaries obtained by function calls, g++ has to move one
of them out of memory, causing rounding.
In your expressions, it is always the same (e.g. always the first
expression) that maintains full precision, and the other is
rounded. Therefore, it is consistent that both comparisons are true.
If you want consistency in the way you expect things to work, you
could force the CPU to use the same representation all the time.
To do that, include <fpu_control.h>, and add
fpu_control_t fpu = (_FPU_DEFAULT & ~_FPU_EXTENDED) | _FPU_DOUBLE;
_FPU_SETCW(fpu);
to your program. This results in the FPU always using 64bit precision,
and gives you twice the value 'false'.
If you still think that g++ is misbehaving, please point to the
wording in the C++ standard that says this is
incorrect. Alternatively, you may want to complain to Intel, for using
80bit IEEE extended representation internally, and to the Linux and
glibc authors, for not changing the _FPU_DEFAULT.
Regards,
Martin