Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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;
}
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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
the standard says about this behavior ?

 
Reply With Quote
 
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


 
Reply With Quote
 
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>
Try the download section.


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

 
Reply With Quote
 
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;
>}

 
Reply With Quote
 
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"
 
Reply With Quote
 
cipher
Guest
Posts: n/a
 
      11-20-2007
I've got the same buggy result using gcc 3.4.3 running on Solaris 10.



 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments