Please comment on my integer to string code.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dietmar Kuehl

    #46
    Re: Please comment on my integer to string code.

    manoj1978@gmail .com wrote:[color=blue][color=green]
    >> For some definition of "it works" but clearly not mine: the result
    >> of using the modulo operator with negative operands is implementation
    >> defined in C++, i.e. it may work depending on the choice of the
    >> implementation. Essentially, the sign of the result is not defined.[/color]
    >
    > I have a doubt here. which among these causes undefined behavior?
    > Because i was relying on these :-((
    >
    > negetive % negetive.
    > negetive % positive.
    > positive % negetive[/color]

    Well, they are not "undefined" but "implementa tion defined". There
    is a subtle difference: for "undefined" the implementation does not
    have to state what it does and actually may not even detect the
    situation at all. For "implementa tion defined" the implementation
    is supposed to tell somewhere how it handles the situation.

    The relevant quote from the standard is this (5.6, expr.mul,
    paragraph 4):

    If the second operand of / or % is zero the behavior is undefined;
    otherwise (a/b)*b + a%b is equal to a. If both operands are
    nonnegative then the remainder is nonnegative; if not, the sign of
    the remainder is implementation-defined.
    --
    <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence

    Comment

    • manoj1978@gmail.com

      #47
      Re: Please comment on my integer to string code.


      Dietmar Kuehl wrote:[color=blue]
      > The relevant quote from the standard is this (5.6, expr.mul,
      > paragraph 4):
      >
      > If the second operand of / or % is zero the behavior is undefined;
      > otherwise (a/b)*b + a%b is equal to a. If both operands are
      > nonnegative then the remainder is nonnegative; if not, the sign of
      > the remainder is implementation-defined.
      >[/color]
      I posted my message before seeing this.Sorry.

      Regards,
      manoj.

      Comment

      • manoj1978@gmail.com

        #48
        Re: Please comment on my integer to string code.


        manoj1978@gmail .com wrote:[color=blue]
        > Dietmar Kuehl wrote:[/color]
        [color=blue][color=green]
        > > For some definition of "it works" but clearly not mine: the result
        > > of using the modulo operator with negative operands is implementation
        > > defined in C++, i.e. it may work depending on the choice of the
        > > implementation. Essentially, the sign of the result is not defined.[/color]
        >
        > I have a doubt here. which among these causes undefined behavior?
        > Because i was relying on these :-((
        >
        > negetive % negetive.
        > negetive % positive.
        > positive % negetive[/color]

        I have the following assumption about % and /

        When b != 0.

        1. abs(a/b) == abs(a) / abs(b)
        2. sign of a/b is positive if a and b have the same sign.negative
        otherwise.
        3. abs(a%b) == abs(a) % abs(b)
        4. sign of a %b == sign of a.
        5. (a/b) * b + a%b == a
        [color=blue]
        >From C Standard May 6,2005 draft Page 82[/color]

        section 6.5.5 Mulplicative operators

        5 The result of the / operator is the quotient from the division of the
        first operand by the
        second; the result of the % operator is the remainder. In both
        operations, if the value of
        the second operand is zero, the behavior is undefined.
        6 When integers are divided, the result of the / operator is the
        algebraic quotient with any
        fractional part discarded(88) If the quotient a/b is representable, the
        expression
        (a/b)*b + a%b shall equal a.

        foot note 88) This is often called ''truncation toward zero''.

        How many of my assumptions are valid? Also what the C++ standard says
        about this?

        Comment

        • manoj1978@gmail.com

          #49
          Re: Please comment on my integer to string code.


          manoj1978@gmail .com wrote:[color=blue]
          > Dietmar Kuehl wrote:[/color]
          [color=blue][color=green]
          > > For some definition of "it works" but clearly not mine: the result
          > > of using the modulo operator with negative operands is implementation
          > > defined in C++, i.e. it may work depending on the choice of the
          > > implementation. Essentially, the sign of the result is not defined.[/color]
          >
          > I have a doubt here. which among these causes undefined behavior?
          > Because i was relying on these :-((
          >
          > negetive % negetive.
          > negetive % positive.
          > positive % negetive[/color]

          I have the following assumption about % and /

          When b != 0.

          1. abs(a/b) == abs(a) / abs(b)
          2. sign of a/b is positive if a and b have the same sign.negative
          otherwise.
          3. abs(a%b) == abs(a) % abs(b)
          4. sign of a %b == sign of a.
          5. (a/b) * b + a%b == a
          [color=blue]
          >From C Standard May 6,2005 draft Page 82[/color]

          section 6.5.5 Mulplicative operators

          5 The result of the / operator is the quotient from the division of the
          first operand by the
          second; the result of the % operator is the remainder. In both
          operations, if the value of
          the second operand is zero, the behavior is undefined.
          6 When integers are divided, the result of the / operator is the
          algebraic quotient with any
          fractional part discarded(88) If the quotient a/b is representable, the
          expression
          (a/b)*b + a%b shall equal a.

          foot note 88) This is often called ''truncation toward zero''.

          How many of my assumptions are valid? Also what the C++ standard says
          about this?

          Comment

          • manoj1978@gmail.com

            #50
            Re: Please comment on my integer to string code.


            Dietmar Kuehl wrote:[color=blue]
            > manoj1978@gmail .com wrote:[color=green][color=darkred]
            > >> For some definition of "it works" but clearly not mine: the result
            > >> of using the modulo operator with negative operands is implementation
            > >> defined in C++, i.e. it may work depending on the choice of the
            > >> implementation. Essentially, the sign of the result is not defined.[/color]
            > >
            > > I have a doubt here. which among these causes undefined behavior?
            > > Because i was relying on these :-((
            > >
            > > negetive % negetive.
            > > negetive % positive.
            > > positive % negetive[/color]
            >
            > Well, they are not "undefined" but "implementa tion defined". There
            > is a subtle difference: for "undefined" the implementation does not
            > have to state what it does and actually may not even detect the
            > situation at all. For "implementa tion defined" the implementation
            > is supposed to tell somewhere how it handles the situation.
            >
            > The relevant quote from the standard is this (5.6, expr.mul,
            > paragraph 4):
            >
            > If the second operand of / or % is zero the behavior is undefined;
            > otherwise (a/b)*b + a%b is equal to a. If both operands are
            > nonnegative then the remainder is nonnegative; if not, the sign of
            > the remainder is implementation-defined.
            >[/color]

            A quick search of comp.lang.c shows that what you say is true for C90
            also.but differs in C99.
            Regards,
            manoj

            Comment

            • Dietmar Kuehl

              #51
              Re: Please comment on my integer to string code.

              manoj1978@gmail .com wrote:[color=blue]
              > A quick search of comp.lang.c shows that what you say is true for C90
              > also.but differs in C99.[/color]

              I know that this was addressed in C99 and it is likely to work in
              C++, too. However, this does not really help if you want to have
              your code portable.
              --
              <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
              <http://www.eai-systems.com> - Efficient Artificial Intelligence

              Comment

              • manoj1978@gmail.com

                #52
                Re: Please comment on my integer to string code.


                Dietmar Kuehl wrote:[color=blue]
                > manoj1978@gmail .com wrote:[color=green]
                > > A quick search of comp.lang.c shows that what you say is true for C90
                > > also.but differs in C99.[/color]
                >
                > I know that this was addressed in C99 and it is likely to work in
                > C++, too. However, this does not really help if you want to have
                > your code portable.[/color]
                Yes.Especially since I dont work with that many C99 compilers also.
                I have taken care of that now.
                Regards,
                manoj

                Comment

                • BobR

                  #53
                  Re: Please comment on my integer to string code.


                  manoj1978@gmail .com wrote in message ...[color=blue]
                  >
                  >BobR wrote:
                  >[color=green]
                  >> // ------------------------------------
                  >> class Manoj{ [snip]
                  >> }; //class Manoj
                  >> // ------------------------------------
                  >> int main(){
                  >> Manoj Man;
                  >> Man.execute( 10, std::cout);
                  >> Man.execute( 16 );
                  >>
                  >> ofstream File("MyTest.tx t");
                  >> if( !File ){
                  >> std::cerr<<"ofs tream File FAILURE!!"<<std ::endl;
                  >> return EXIT_FAILURE;
                  >> }
                  >> for(int i( -36 ); i <= 36; ++i){ Man.execute( i, File );} // for(i)
                  >> return 0;
                  >> } // main()end
                  >> // ------------------------------------
                  >> --
                  >> Bob R
                  >> POVrookie[/color]
                  >
                  >Hi Bob,
                  > Thank you very much for the code.
                  > I changed ofstream File("MyTest.tx t"); to std::ofstream
                  >File("MyTest.t xt");[/color]

                  Oooops. It was very late and I got in a hurry to get it posted. Glad you
                  figured it out.
                  [color=blue]
                  > Changed charTable into character array and added step to handle
                  >INT_MIN.
                  > It works fine.Once again thanks.
                  >Regards,
                  > manoj.[/color]

                  I didn't know if you knew 'class', but, thought it would be easier to test
                  that way (and I wouldn't have to transfer/translate code from the form I test
                  in ( that class is inside another class of same design, in my 'testbench'
                  wxWidgets program)).

                  So, is it *all* working to your satisfaction now?

                  --
                  Bob R
                  POVrookie


                  Comment

                  • manoj1978@gmail.com

                    #54
                    Re: Please comment on my integer to string code.


                    BobR wrote:[color=blue]
                    > manoj1978@gmail .com wrote in message ...
                    > Oooops. It was very late and I got in a hurry to get it posted. Glad you
                    > figured it out.[/color]
                    No probs :-)[color=blue]
                    >
                    > I didn't know if you knew 'class', but, thought it would be easier to test
                    > that way (and I wouldn't have to transfer/translate code from the form I test
                    > in ( that class is inside another class of same design, in my 'testbench'
                    > wxWidgets program)).[/color]
                    Yeah,I know class.Not used in the code,since i initially wrote this in
                    c.I just modified it so as to post here :-)[color=blue]
                    >
                    > So, is it *all* working to your satisfaction now?[/color]
                    Yes.I thought it may need much work to handle % and / truncating to
                    infinity.
                    But on testing with the following functions,found that code needed
                    change only in INT_MAX handler,since "if (i % base >= 0)" take care of
                    this change automatically.

                    int divToInf(int a,int b)
                    {
                    bool sign = true;
                    int add = 0;
                    if (a < 0)
                    {
                    a = -a;
                    sign = !sign;
                    add = 1;
                    }
                    if (b <0)
                    {
                    b = -b;
                    sign = !sign;
                    }

                    if (sign)
                    {
                    return a/b + add;
                    } else {
                    return -( a/b + add);
                    }
                    }

                    int modToInf(int a,int b)
                    {
                    return a - b * divToInf(a,b);
                    }

                    Once again thanks.
                    Regards,
                    manoj.

                    Comment

                    • Richard Herring

                      #55
                      Re: Please comment on my integer to string code.

                      In message <1141916757.291 734.248560@p10g 2000cwp.googleg roups.com>,
                      manoj1978@gmail .com writes[color=blue]
                      >Hi All,
                      > I wrote the following to print an integer in its string
                      >representati on for base -36 to 36.
                      > Please comment on this code.
                      >
                      >#include <iostream>
                      >#include <string>
                      >
                      >using std::abs;
                      >using std::cout;
                      >using std::endl;
                      >using std::reverse;
                      >using std::string;
                      >
                      >char charTable[] = "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZ";
                      >
                      >void printBase(int i, int base)
                      >{
                      > if ((abs(base) <= 1) || (abs(base) > 36) || (i == 0))
                      > {
                      > cout << "0" << endl;
                      > return;
                      > }
                      >[/color]
                      [snip rest of code]

                      You've had lots of feedback on the other stuff, but I haven't noticed
                      anybody commenting on this combination of two completely unrelated
                      tests:

                      If the argument is zero, output "0". Fine; that's the expected result.

                      If the base is out of bounds, output "0". Do you really want an input
                      error to produce the same result as valid data?

                      (Incidentally, what have you got against base-1 representation? ;-)

                      --
                      Richard Herring

                      Comment

                      • manoj1978@gmail.com

                        #56
                        Re: Please comment on my integer to string code.


                        Richard Herring wrote:
                        I havent seen this post due to google's interface.Sorry about the
                        delay.[color=blue]
                        > You've had lots of feedback on the other stuff, but I haven't noticed
                        > anybody commenting on this combination of two completely unrelated
                        > tests:
                        >
                        > If the argument is zero, output "0". Fine; that's the expected result.
                        >
                        > If the base is out of bounds, output "0". Do you really want an input
                        > error to produce the same result as valid data?[/color]

                        I got that comment also :-)
                        I have seperated them.[color=blue]
                        >
                        > (Incidentally, what have you got against base-1 representation? ;-)[/color]
                        Nothing.Somebod y asked about negative base numbers in a group.I
                        searched in math forum about it and wrote the code.[color=blue]
                        >
                        > --
                        > Richard Herring[/color]
                        Regards,
                        manoj.

                        Comment

                        • Richard Herring

                          #57
                          Re: Please comment on my integer to string code.

                          In message <1142427423.041 431.225670@u72g 2000cwu.googleg roups.com>,
                          manoj1978@gmail .com writes[color=blue]
                          >
                          >Richard Herring wrote:
                          >I havent seen this post due to google's interface.Sorry about the
                          >delay.[color=green]
                          >> You've had lots of feedback on the other stuff, but I haven't noticed
                          >> anybody commenting on this combination of two completely unrelated
                          >> tests:
                          >>
                          >> If the argument is zero, output "0". Fine; that's the expected result.
                          >>
                          >> If the base is out of bounds, output "0". Do you really want an input
                          >> error to produce the same result as valid data?[/color]
                          >
                          >I got that comment also :-)
                          >I have seperated them.[color=green]
                          >>
                          >> (Incidentally, what have you got against base-1 representation? ;-)[/color]
                          >Nothing.Somebo dy asked about negative base numbers in a group.I
                          >searched in math forum about it and wrote the code.[/color]

                          Base *1*, not general non-negative bases. It's very easy: 1, 11, 111,
                          1111, 11111, ...

                          --
                          Richard Herring

                          Comment

                          • manoj1978@gmail.com

                            #58
                            Re: Please comment on my integer to string code.


                            Richard Herring wrote:[color=blue]
                            > Base *1*, not general non-negative bases. It's very easy: 1, 11, 111,
                            > 1111, 11111, ...[/color]

                            :-)

                            Comment

                            • Ben Pope

                              #59
                              Re: Please comment on my integer to string code.

                              Richard Herring wrote:[color=blue]
                              >
                              > Base *1*, not general non-negative bases. It's very easy: 1, 11, 111,
                              > 1111, 11111, ...[/color]

                              Looks like a classic off by one bug to me!

                              Surely it's:
                              0, 00, 000, 0000, ...

                              At least, in a C++ newsgroup.

                              Ben Pope
                              --
                              I'm not just a number. To many, I'm known as a string...

                              Comment

                              Working...