Incorrect runtime behavior involving comparison

Matt Austern austern@isolde.engr.sgi.com
Mon Jan 10 14:21:00 GMT 2000


SYSTEM:
i686 Linux 2.2.5, libc 2.1.1.

COMPILER VERSION:
austern@pizzelle% /home/austern/bin/g++ -v
Reading specs from
/home/austern/bin/../lib/gcc-lib/i686-pc-linux-gnu/2.96/specs
gcc version 2.96 20000110 (experimental)

SYMPTOM:
The correct output from the following program is:
t1 is false
t2 is false

When compiled -DBUG, however, the output we get instead is:
t1 is true
t2 is true

austern@pizzelle% cat cbug.cc
#include <stdio.h>

struct zone {
  int profit;
  int size;
  int code_expansion() {
    return size;
  }
  double priority() {
    return ((double)profit)/code_expansion();
  }
};

main()
{
 struct zone z1;
 struct zone z2;
 z1.profit = 100;
 z1.size = 3;
 z2.profit = 200;
 z2.size = 6;

#ifdef BUG
 bool t1 = z1.priority() > z2.priority();
 bool t2 = z2.priority() > z1.priority();
#else
 double d1 = z1.priority();
 double d2 = z2.priority();
 bool t1 = d1 > d2;
 bool t2 = d2 > d1;
#endif

 printf("t1 is %s\n", t1 ? "true" : "false");
 printf("t2 is %s\n", t2 ? "true" : "false");
}
austern@pizzelle% /home/austern/bin/g++ cbug.cc
austern@pizzelle% a.out
t1 is false
t2 is false
austern@pizzelle% /home/austern/bin/g++ -DBUG cbug.cc
austern@pizzelle% a.out
t1 is true
t2 is true
austern@pizzelle%



More information about the Gcc-bugs mailing list