Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
Get HideMyAss! VPN, PC Mag's Top 10 VPNs of 2016 for 55% off for a Limited Time ×
Programming

Submission + - gcc says -10==10 (lkml.org) 6

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 ;
}

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.
    • #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!
    ...

A man is known by the company he organizes. -- Ambrose Bierce

Working...