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

Discussion in 'C Programming' started by Wavelet, Nov 20, 2007.

  1. Wavelet

    Wavelet Guest

    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;
    }
     
    Wavelet, Nov 20, 2007
    #1
    1. Advertising

  2. In article <>,
    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;
    >}


    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
     
    Walter Roberson, Nov 20, 2007
    #2
    1. Advertising

  3. Wavelet

    somenath Guest

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

    On Nov 20, 8:27 am, -cnrc.gc.ca (Walter Roberson)
    wrote:
    > In article <>,
    >
    > 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;
    > >}

    >
    > 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 ?
     
    somenath, Nov 20, 2007
    #3
  4. Re: Why (-10) * abs(i-1) == 10 in Linux??

    On Nov 19, 10:27 pm, -cnrc.gc.ca (Walter Roberson)
    wrote:
    > In article <>,
    >
    > 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;
    > >}

    >
    > 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
     
    William Hughes, Nov 20, 2007
    #4
  5. Wavelet

    CBFalconer Guest

    Walter Roberson wrote:
    > 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;
    >>}

    >
    > 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
     
    CBFalconer, Nov 20, 2007
    #5
  6. Wavelet

    Porkling Guest

    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-PR34130,-extract_muldiv-broken-t4826688.html

    On Mon, 19 Nov 2007 18:34:07 -0800 (PST), 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;
    >}
     
    Porkling, Nov 20, 2007
    #6
  7. 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) <>
    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"
     
    Keith Thompson, Nov 20, 2007
    #7
  8. Wavelet

    cipher Guest

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

    I've got the same buggy result using gcc 3.4.3 running on Solaris 10.
     
    cipher, Nov 20, 2007
    #8
  9. Wavelet

    jacob navia Guest

    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
     
    jacob navia, Nov 20, 2007
    #9
  10. Wavelet

    Spoon Guest

    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
     
    Spoon, Nov 20, 2007
    #10
  11. Wavelet

    user1 Guest

    jacob navia wrote:
    > 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)
    >
    >


    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 ...
     
    user1, Nov 20, 2007
    #11
  12. 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.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Nov 20, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-15?Q?Daniel_Sch=FCle?=

    why no arg, abs methods for comlex type?

    =?ISO-8859-15?Q?Daniel_Sch=FCle?=, Aug 5, 2005, in forum: Python
    Replies:
    18
    Views:
    506
    Bengt Richter
    Aug 6, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,008
    Smokey Grindel
    Dec 2, 2006
  3. Klaas Vantournhout

    f2c's abs conflicts with <complex> abs

    Klaas Vantournhout, Oct 31, 2006, in forum: C++
    Replies:
    3
    Views:
    388
    Victor Bazarov
    Oct 31, 2006
  4. fred
    Replies:
    3
    Views:
    284
    Zifud
    Mar 17, 2005
  5. Replies:
    5
    Views:
    891
Loading...

Share This Page