Forgot your password?
typodupeerror
Programming

+ - gcc says -10==10-> 6

Submitted by Anonymous Coward
An anonymous reader writes "A bug in gcc causes it to think -10*abs(x) and 10*abs(x) have the same value. Just compile the following small program:

int main( void )
{
    int i=2;
    if( -10*abs (i-1) == 10*abs(i-1) )
        printf ("OMG,-10==10 in linux!\n");
    else
        printf ("nothing special here\n") ;
    return 0 ;
}"

Link to Original Source
This discussion was created for logged-in users only, but now has been archived. No new comments can be posted.

gcc says -10==10

Comments Filter:
  • In my configuration anyway. Ran the supplied sample using GCC 3.4.5, mingw, on XP-64, and yes, it does evaluate that "if" wrong.
  • It's not the if that's wrong. If you have "int i = 2, j = -11 * abs(i - 1);", j holds the value 11.
    • by tolomea (1026104)
      #include
      int main(int argc, char **argv){
      int i = 2;
      int j = -11 * abs(i - 1);
      printf("i=%d, j=%d\n", i, j);
      return 0;
      }

      functions as advertised

      this however is even more special

      #include
      int main(int argc, char **argv){
      float j = abs(1.0);
      printf("j=%f\n", j);
      return 0;
      }

      am I doing something wrong?
  • broken.

    How can this have been broken for so long? Insane.

  • simplified:

    $ cat foo.c
    #include <stdio.h>
    #include <stdlib.h>

    int main (int argc, char *argv[]) {
    int i=2;
    int n;

    n=-1 * abs(i-1);

    printf ("n==%d\n", n);

    return 0;
    }
    $ gcc -S foo.c -o foo.S
    $ cat foo.S
    ...
    movl $2, -8(%ebp) // i=2
    movl -8(%ebp), %edx // %edx = 2
    movl $0, %eax // %eax = 0
    subl %edx, %eax // %eax = -2
    addl $1, %eax // %eax = -1
    cltd // %edx = -1
    xorl %edx, %eax // %eax = 0
    movl %eax, -4(%ebp) // n = 0
    leal -4(%ebp), %eax // %eax = &n
    subl %edx, (%eax) // n = 1!
    ...

The reason that every major university maintains a department of mathematics is that it's cheaper than institutionalizing all those people.

Working...