Comparing two numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Nilsson

    #16
    Re: Comparing two numbers

    Nick Austin wrote:[color=blue]
    > On 16 Jan 2005 16:01:42 -0800, "Peter Nilsson" <airia@acay.com .au>
    > wrote:
    >
    > [...][color=green]
    > >The following uses strtol() to convert arguments on the command
    > >line, and printf() to output the maximum. Otherwise, it doesn't
    > >use any conditional, logical, equality or relational operators,
    > >nor does it use any selection or iteration statements.[/color]
    >
    > [...][color=green]
    > >typedef unsigned long q1;
    > >q1 q2(q1 q3,q1 q4,q1 q5,q1 >q6)
    > >{
    > > return(q5-q6)*(((((q3-q4)&(q4-q3)[/color][/color]
    )+2)/(((q3-q4)&(q4-q3))+1))-1)+q6;[color=blue][color=green]
    > >}[/color]
    >
    > I don't think that's portable, namely the use of the bitwise[/color]
    operator:[color=blue]
    >
    > ( q3 - q4 ) & ( q4 - q3 )
    >
    > It seems to me that it assumes twos compliment and could give the
    > wrong answer on other architectures.[/color]

    "Two's complement" applies to _signed_ integers. The quoted expression
    operates on unsigned integers, for which there is only one choice
    of representation, namely, pure binary, and for which the behaviour on
    overflow is well defined.

    --
    Peter

    Comment

    • CBFalconer

      #17
      Re: Comparing two numbers

      Bonj wrote:[color=blue]
      >
      > People always like to think that the help they're giving is of
      > commercial value. If they think otherwise, they'll jump on the
      > 'homework' bandwagon. Happens every time.[/color]

      Please don't toppost. As you can see, doing so has lost all
      context here. As a matter of fact the 'homework' rejection is
      designed to avoid creating a class of useless programmers who have
      never learned to do anything themselves. Those that make efforts
      but run into difficulties, and demonstrate it by posting their
      work, usually get help.

      --
      "If you want to post a followup via groups.google.c om, don't use
      the broken "Reply" link at the bottom of the article. Click on
      "show options" at the top of the article, then click on the
      "Reply" at the bottom of the article headers." - Keith Thompson

      Comment

      • Peter Nilsson

        #18
        Re: Comparing two numbers

        À̱â¿î wrote:

        Please don't top post in comp.lang.c.
        [color=blue]
        > Hi~ all
        >
        > i think that this is the problem about getting sign bit of numbers...
        > this is very easy... see follow code lines...
        > you can simplfy multiple lines to a line...[/color]

        Missing inclusion of <stdio.h> or prototype for printf.
        The behaviour of printf is therefore undefined.
        [color=blue]
        > int main(int argc, char *argv[])
        > {
        > int a = 2.0;
        > int b = -23.1;[/color]

        This is not _wrong_ wrong, just wrong. ;)
        [color=blue]
        > int lagestNumber = 0;
        > int signBitCount = (sizeof(int) * 8) - 1;[/color]

        A byte may have more than 8 bits. An int may have padding bits.
        So, your shift count can be wrong in two ways.
        [color=blue]
        > int signBitMask = 0x1 << signBitCount;[/color]

        Potential undefined behaviour. Even if the behaviour is defined for
        some implementations , it may still be the wrong bit mask.
        [color=blue]
        > int a_minus_b = a - b;
        > int b_minus_a = b - a;[/color]

        Potential overflow for arbitrary a and b.
        [color=blue]
        > int signBitA = 0;
        > int signBitB = 0;
        >
        > signBitA = ((unsigned)a_mi nus_b & signBitMask) >> signBitCount;
        > signBitB = ((unsigned)b_mi nus_a & signBitMask) >> signBitCount;[/color]

        An unsigned int need not use the corresponding int sign bit as a
        value bit. So your cast may produce zero if a_minus_b is INT_MIN.
        [color=blue]
        > x = (signBitA ^ 0x1) * a + (signBitB ^ 0x1) * b;
        > printf("lagestN umber = %d\n", lagestNumber);[/color]
        lagestNumber is 0 here.
        [color=blue]
        > return 0;
        > }[/color]

        --
        Peter

        Comment

        • Peter Nilsson

          #19
          Re: Comparing two numbers

          Nick Austin wrote:[color=blue]
          > On 16 Jan 2005 16:01:42 -0800, "Peter Nilsson" <airia@acay.com .au>
          > wrote:
          >
          > [...][color=green]
          > >The following uses strtol() to convert arguments on the command
          > >line, and printf() to output the maximum. Otherwise, it doesn't
          > >use any conditional, logical, equality or relational operators,
          > >nor does it use any selection or iteration statements.[/color]
          >
          > [...][color=green]
          > >typedef unsigned long q1;
          > >q1 q2(q1 q3,q1 q4,q1 q5,q1 >q6)
          > >{
          > > return(q5-q6)*(((((q3-q4)&(q4-q3)[/color][/color]
          )+2)/(((q3-q4)&(q4-q3))+1))-1)+q6;[color=blue][color=green]
          > >}[/color]
          >
          > I don't think that's portable, namely the use of the bitwise[/color]
          operator:[color=blue]
          >
          > ( q3 - q4 ) & ( q4 - q3 )
          >
          > It seems to me that it assumes twos compliment and could give the
          > wrong answer on other architectures.[/color]

          If unsigned integers looks like two's complement (signed) integers,
          it's because two's complement integers look like unsigned integers. ;)

          Unsigned types must use 'pure binary', and the behaviour on overflow
          is well defined.

          --
          Peter

          Comment

          • Lawrence Kirby

            #20
            Re: Comparing two numbers

            On Mon, 17 Jan 2005 08:02:08 +0000, Bonj wrote:
            [color=blue]
            > Easy - use inline assembly.[/color]

            The question was to write a program in C, and C has no inline assembly.
            [color=blue]
            > Or subtract one from the other and the test the sign bit.[/color]

            Something along those lines may work although making it portable would be
            a challenge.

            Lawrence

            Comment

            • Mike Wahler

              #21
              Re: Comparing two numbers

              "Bonj" <a@b.com> wrote in message news:351a1pF4fa vtgU1@individua l.net...[color=blue]
              > People always like to think that the help they're giving is of commercial
              > value. If they think otherwise, they'll jump on the 'homework' bandwagon.
              > Happens every time.[/color]

              No, I disagree. Look again at the original question. It's
              so obviously a contrived question, and its solution is never
              needed in a C program (except for perhaps as a diversionary
              'puzzle', but OP didn't label it as such). So I for one,
              took it as a general 'problem solving' challenge which might
              be a useful learning device (in the context of problem solving,
              but imo not for learning C).

              Also see Keith T.'s reply, which imo is the best answer to
              OP so far.

              -Mike


              Comment

              • Mike Wahler

                #22
                Re: Comparing two numbers


                "Peter Nilsson" <airia@acay.com .au> wrote in message
                news:1105955633 .401667.92250@f 14g2000cwb.goog legroups.com...
                À̱â¿î wrote:

                Please don't top post in comp.lang.c.
                [color=blue]
                > Hi~ all
                >
                > i think that this is the problem about getting sign bit of numbers...
                > this is very easy... see follow code lines...
                > you can simplfy multiple lines to a line...[/color]

                Missing inclusion of <stdio.h> or prototype for printf.
                The behaviour of printf is therefore undefined.
                [color=blue]
                > int main(int argc, char *argv[])
                > {
                > int a = 2.0;
                > int b = -23.1;[/color]

                This is not _wrong_ wrong, just wrong. ;)
                [color=blue]
                > int lagestNumber = 0;
                > int signBitCount = (sizeof(int) * 8) - 1;[/color]

                A byte may have more than 8 bits. An int may have padding bits.
                So, your shift count can be wrong in two ways.
                [color=blue]
                > int signBitMask = 0x1 << signBitCount;[/color]

                Potential undefined behaviour. Even if the behaviour is defined for
                some implementations , it may still be the wrong bit mask.
                [color=blue]
                > int a_minus_b = a - b;
                > int b_minus_a = b - a;[/color]

                Potential overflow for arbitrary a and b.
                [color=blue]
                > int signBitA = 0;
                > int signBitB = 0;
                >
                > signBitA = ((unsigned)a_mi nus_b & signBitMask) >> signBitCount;
                > signBitB = ((unsigned)b_mi nus_a & signBitMask) >> signBitCount;[/color]

                An unsigned int need not use the corresponding int sign bit as a
                value bit. So your cast may produce zero if a_minus_b is INT_MIN.
                [color=blue]
                > x = (signBitA ^ 0x1) * a + (signBitB ^ 0x1) * b;
                > printf("lagestN umber = %d\n", lagestNumber);[/color]
                lagestNumber is 0 here.
                [color=blue]
                > return 0;
                > }[/color]

                Also, À̱â¿î:

                Your errors aside, please don't try to provide solutions
                here to folks who've given no evidence of their own efforts
                first. This issue may or may not apply to the OP, but here
                we like to try to protect ourselves from cheating students
                getting a foothold in the ranks of the professionals[*], making
                our work unnecessarily more difficult.

                [*] by passing a CS course without really learning.

                -Mike




                --
                Peter


                Comment

                • purifier

                  #23
                  Re: Comparing two numbers

                  Enough of those insults... I'm sorry for bothering you guys... I guess this
                  forum is filled with all time intellectuals.. . I already explained that I'm
                  an Electronics Engineer just beginning to learn C... Now why would I lie
                  about my profession? Come on...there is no reason to... Ok...if it was one
                  post insulting me then it's ok but it seems that everyone who posts a reply
                  just insults in some or the other way... It is a pity that you guys are so
                  rude to beginners like me... Now, how do you want me to prove that i'm not
                  asking you to do my homework...? Do you want me to sign a bond or what?
                  The only reason i came here was thinking that this is an open forum... and
                  thought i could ask for some help... Now, how do i show you my work if I
                  didn't even understand the problem... Anyway, I won't bother you guys
                  again... You can talk all the hi fi stuff...

                  And Admin Sir, Let me finally congratulate you in having such a wonderful
                  forum... All the above things were my personal opinions because i was hurt
                  a lot after posting here but your board is definitely helpful for many
                  experienced programmers to interact... Keep it up Sir...

                  purifier

                  Comment

                  • Mike Wahler

                    #24
                    Re: Comparing two numbers


                    "purifier" <cnbctv18@gmail .com> wrote in message
                    news:5c7ae7523d 10f03532a993af9 5730ce4@localho st.talkaboutpro gramming.com...[color=blue]
                    > Enough of those insults... I'm sorry for bothering you guys... I guess[/color]
                    this[color=blue]
                    > forum is filled with all time intellectuals.. . I already explained that[/color]
                    I'm[color=blue]
                    > an Electronics Engineer just beginning to learn C... Now why would I lie
                    > about my profession?[/color]

                    A much better question is why would you want to avoid
                    using a language feature specifically designed to do
                    what you asked about (comparing values). It also has
                    a name which lends to code readability (which is important):
                    'if'.

                    [color=blue]
                    > Come on...there is no reason to... Ok...if it was one
                    > post insulting me then it's ok but it seems that everyone who posts a[/color]
                    reply[color=blue]
                    > just insults in some or the other way... It is a pity that you guys are so
                    > rude to beginners like me... Now, how do you want me to prove that i'm not
                    > asking you to do my homework...?[/color]

                    No need to prove that. But please prove that what you're asking
                    has any practical use in a C program.
                    [color=blue]
                    > Do you want me to sign a bond or what?
                    > The only reason i came here was thinking that this is an open forum... and
                    > thought i could ask for some help... Now, how do i show you my work if I
                    > didn't even understand the problem...[/color]


                    What you seem to be missing is that their is NO problem.
                    You want to compare two values, use 'if'.
                    [color=blue]
                    >Anyway, I won't bother you guys
                    > again... You can talk all the hi fi stuff...[/color]

                    OK, bye then.
                    [color=blue]
                    >
                    > And Admin Sir, Let me finally congratulate you in having such a wonderful
                    > forum...[/color]

                    This is a public, unmoderated newsgroup. There is no 'admin'.
                    [color=blue]
                    >All the above things were my personal opinions because i was hurt
                    > a lot after posting here[/color]

                    IMO you need to develop a thicker skin.
                    [color=blue]
                    >but your board is definitely helpful for many
                    > experienced programmers to interact...[/color]

                    It's also been proven to be very helpful to beginners.
                    But it's not a 'gimme some code' or 'gimme the answer'
                    forum. We *help* people with what *they* are doing,
                    we don't simply do it *for* them. I (and others) did
                    give you some useful hints.


                    If you really want to figure out the answer to the proposed
                    (imo ridiculous) question, study some C books, and *give it
                    a try*. Then I'm sure you'll be pleased with the help you
                    get.

                    -Mike


                    Comment

                    • Default User

                      #25
                      Re: Comparing two numbers

                      purifier wrote:
                      [color=blue]
                      > And Admin Sir, Let me finally congratulate you in having such a[/color]
                      wonderful[color=blue]
                      > forum... All the above things were my personal opinions because i was[/color]
                      hurt[color=blue]
                      > a lot after posting here but your board is definitely helpful for[/color]
                      many[color=blue]
                      > experienced programmers to interact... Keep it up Sir...[/color]


                      Someone needs a Usenet 101 primer. There's no Admin. There's no
                      moderator. There's no board. There's no "here" here. There's just a
                      bunch of heres here and there, connected up into a loose confederation.
                      And we like it that way.

                      I see that you post from talkaboutprogra mming.com, that's fooled you
                      into thinking it's a web forum with a small community of participants.
                      In fact, that's just a portal into a far-flung distributed network
                      encompassing thousands of servers around the world.

                      Very few, if any other posters are coming in through your service. Most
                      ISPs have their own news servers (although some do not). Another
                      popular way to access usenet is through Google, as I am doing until
                      such time as they fix our newsfeed.



                      Brian

                      Comment

                      • Mike Wahler

                        #26
                        Re: Comparing two numbers

                        "Default User" <defaultuserbr@ yahoo.com> wrote in message
                        news:1105996610 .353017.316640@ z14g2000cwz.goo glegroups.com.. .[color=blue]
                        > ISPs have their own news servers (although some do not).[/color]

                        [color=blue]
                        > Another
                        > popular way to access usenet is through Google, as I am doing until
                        > such time as they fix our newsfeed.[/color]

                        My condolences. :-)

                        -Mike


                        Comment

                        • Default User

                          #27
                          Re: Comparing two numbers

                          Mike Wahler wrote:[color=blue]
                          > "Default User" <defaultuserbr@ yahoo.com> wrote in message
                          > news:1105996610 .353017.316640@ z14g2000cwz.goo glegroups.com.. .[color=green]
                          > > ISPs have their own news servers (although some do not).[/color]
                          >
                          >[color=green]
                          > > Another
                          > > popular way to access usenet is through Google, as I am doing until
                          > > such time as they fix our newsfeed.[/color]
                          >
                          > My condolences. :-)[/color]


                          It's certainly made me more sympathetic towards those struggling with
                          using grouple. They sure made a lot of bad decisions. It's hard to
                          believe that the developers working on the interface could have
                          actually used usenet much at all.

                          There's a guy who developed a web-based news service that works much
                          better than google's. http://www.recgroups.com/?force=1#

                          It's not perfect, but sure is a lot better interface than google.[color=blue]
                          >[/color]



                          Brian

                          Comment

                          • Mike Wahler

                            #28
                            Re: Comparing two numbers


                            "Default User" <defaultuserbr@ yahoo.com> wrote in message
                            news:1106001316 .221306.3040@z1 4g2000cwz.googl egroups.com...[color=blue]
                            > Mike Wahler wrote:[color=green]
                            > > "Default User" <defaultuserbr@ yahoo.com> wrote in message
                            > > news:1105996610 .353017.316640@ z14g2000cwz.goo glegroups.com.. .[color=darkred]
                            > > > ISPs have their own news servers (although some do not).[/color]
                            > >
                            > >[color=darkred]
                            > > > Another
                            > > > popular way to access usenet is through Google, as I am doing until
                            > > > such time as they fix our newsfeed.[/color]
                            > >
                            > > My condolences. :-)[/color]
                            >
                            >
                            > It's certainly made me more sympathetic towards those struggling with
                            > using grouple. They sure made a lot of bad decisions. It's hard to
                            > believe that the developers working on the interface could have
                            > actually used usenet much at all.[/color]

                            They probably didn't. They probably take the view of Usenet
                            as "just another data source". And perhaps they're stripping
                            whitespace in a misguided attempt to conserve bandwitdth.
                            But this is just speculation of course.

                            -Mike

                            [color=blue]
                            >
                            > There's a guy who developed a web-based news service that works much
                            > better than google's. http://www.recgroups.com/?force=1#
                            >
                            > It's not perfect, but sure is a lot better interface than google.[/color]

                            There are also public news servers that work with 'real'
                            news clients (but I realize some organizations limit access
                            to 'unknown' sources in the interest of security).

                            -Mike


                            Comment

                            • Default User

                              #29
                              Re: Comparing two numbers

                              Mike Wahler wrote:
                              [color=blue]
                              > There are also public news servers that work with 'real'
                              > news clients (but I realize some organizations limit access
                              > to 'unknown' sources in the interest of security).[/color]


                              That's our case. The firewall won't allow use of non-approved NNTP
                              servers. I have an account with the German server which I'd be happy to
                              use all the time, but no.

                              Google and ones like I mentioned work because they're actually HTTP. If
                              I knew of a decent news operation with a good web interface that wasn't
                              too expensive, I would be interested.



                              Brian

                              Comment

                              • Bonj

                                #30
                                Re: Comparing two numbers

                                Sorry - I only top-post because of the fact that it avoids excessive
                                scrolling to read just the newest addition, especially in messages that
                                contain a lot of code. Just a personal preference, that's all.
                                I see the point about not answering homework, and I agree. But I am quite
                                sceptical of the notion that as many people as there are that chastise
                                homework-posters are actually making a valiant attempt to change the world
                                by implicitly teaching the youth of today to use a fishing rod rather than
                                give them a fish.


                                Comment

                                Working...