This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Incorrect runtime behavior involving comparison


> 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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]