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

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

Wavelet
Guest
Posts: n/a

 11-20-2007
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;
}

Walter Roberson
Guest
Posts: n/a

 11-20-2007
In article <(E-Mail Removed)>,
Wavelet <(E-Mail Removed)> wrote:

>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.
--
"Any sufficiently advanced bug is indistinguishable from a feature."
-- Rich Kulawiec

somenath
Guest
Posts: n/a

 11-20-2007
On Nov 20, 8:27 am, (E-Mail Removed)-cnrc.gc.ca (Walter Roberson)
wrote:
> In article <(E-Mail Removed)>,
>
> Wavelet <(E-Mail Removed)> wrote:
> >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.

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

William Hughes
Guest
Posts: n/a

 11-20-2007
On Nov 19, 10:27 pm, (E-Mail Removed)-cnrc.gc.ca (Walter Roberson)
wrote:
> In article <(E-Mail Removed)>,
>
> Wavelet <(E-Mail Removed)> wrote:
> >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.

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

CBFalconer
Guest
Posts: n/a

 11-20-2007
Walter Roberson wrote:
> Wavelet <(E-Mail Removed)> wrote:
>
>> 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.

Data point: No problem with gcc 3.2.1 under DJGPP.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Porkling
Guest
Posts: n/a

 11-20-2007
This bug cause by GCC compiler.

Because of the compiler optimazing the sentanse:
-10 * abs( i - 1 )
to
abs( -10 * i + 10 )

So it cause a big trouble.

This is a "Const Foldering" bug.
See patch "Fix-PR34130,-extract_muldiv-broken".

Following website:
http://www.nabble.com/-PATCH--Fix-PR...-t4826688.html

On Mon, 19 Nov 2007 18:34:07 -0800 (PST), Wavelet
<(E-Mail Removed)> wrote:

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

Keith Thompson
Guest
Posts: n/a

 11-20-2007
Wavelet wrote:
> 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".)

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

cipher
Guest
Posts: n/a

 11-20-2007
I've got the same buggy result using gcc 3.4.3 running on Solaris 10.

jacob navia
Guest
Posts: n/a

 11-20-2007
Wavelet wrote:
> 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)

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

Spoon
Guest
Posts: n/a

 11-20-2007
Wavelet wrote:

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

For the record, the subject line was:
"Why (-10) * abs(i-1) == 10 in 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's a bug in GCC which has now been fixed.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34130