Why (-10) * abs(i-1) == 10 in Linux??

W

Wavelet

Why the following code get that result as title under Linux??

#include <stdio.h>
#include <stdlib.h>

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

Walter Roberson

Wavelet said:
Why the following code get that result as title under Linux??
#include <stdio.h>
#include <stdlib.h>
int main () {
int i=2;
if(((-10)*(abs(i-1))) == 10)
printf ("OMG,-10==10 in linux!\n");
else
printf ("nothing special here\n");
return 0;
}

Although platform-specific questions are not really on-topic here,
I tried the above, and I reproduce the problem on
gcc version 3.4.4 on 2.6.10-1.770_FC3smp.

Breaking down the expressions, if the abs(i-1) is calculated
seperately and that multiplied by the -10 then everything works
as expected, but (-10)*(abs(i-1))) is calculated as +10 on
the above compiler.
 
S

somenath

Although platform-specific questions are not really on-topic here,
I tried the above, and I reproduce the problem on
gcc version 3.4.4 on 2.6.10-1.770_FC3smp.

Breaking down the expressions, if the abs(i-1) is calculated
seperately and that multiplied by the -10 then everything works
as expected, but (-10)*(abs(i-1))) is calculated as +10 on
the above compiler.

Is it a bug of the compiler ? Or this is expected behavior ?
When I compiled and executed in different compiler I got
"nothing special here" as output . Could you please tell me what is
the standard says about this behavior ?
 
W

William Hughes

Although platform-specific questions are not really on-topic here,
I tried the above, and I reproduce the problem on
gcc version 3.4.4 on 2.6.10-1.770_FC3smp.

Breaking down the expressions, if the abs(i-1) is calculated
seperately and that multiplied by the -10 then everything works
as expected, but (-10)*(abs(i-1))) is calculated as +10 on
the above compiler.

Similar results for gcc 3.2.2

The printf is deceptive however. The bug (in the compiler, not in
Linux)
is that (-10)*(abs(i-1)) is calculated as 10, rather than -10. The
comparison
is 10 == 10, true; not -10 == 10, true.

- William Hughes
 
C

CBFalconer

Walter said:
Although platform-specific questions are not really on-topic
here, I tried the above, and I reproduce the problem on
gcc version 3.4.4 on 2.6.10-1.770_FC3smp.

Breaking down the expressions, if the abs(i-1) is calculated
seperately and that multiplied by the -10 then everything works
as expected, but (-10)*(abs(i-1))) is calculated as +10 on
the above compiler.

Data point: No problem with gcc 3.2.1 under DJGPP.
 
K

Keith Thompson

Wavelet said:
Why the following code get that result as title under Linux??

#include <stdio.h>
#include <stdlib.h>

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

It looks like a gcc bug. (It doesn't seem to have anything to do with
Linux; I get the same result with gcc on Solaris, and with Sun's
compiler on Solaris it prints "nothing special here".)
 
J

jacob navia

Wavelet said:
Why the following code get that result as title under Linux??

#include <stdio.h>
#include <stdlib.h>

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

I got this bug in gcc 2.96 from July 31th, 2000
up to gcc 4.0.2 from August 8th, 2005. (linux)

The 64 bit version gcc 3.4.4 from July 21st, 2005
is also affected. (x86 AMD64 version)
 
U

user1

jacob said:
I got this bug in gcc 2.96 from July 31th, 2000
up to gcc 4.0.2 from August 8th, 2005. (linux)

The 64 bit version gcc 3.4.4 from July 21st, 2005
is also affected. (x86 AMD64 version)

The bug is also in Mingw, and present in various gcc versions for a long
time it would seem. A workaround seems to be to compile with the
"-fno-builtin" option. Now, if I could just figure out how to change the
"specs" file so that -fno-builtin is always added ...
 
R

Richard Heathfield

Wavelet said:
Why the following code get that result as title under Linux??

#include <stdio.h>
#include <stdlib.h>

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

The above program works correctly under my gcc 2.95.3 implementation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,010
Latest member
MerrillEic

Latest Threads

Top