basic question on *=

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanctus
    New Member
    • Mar 2007
    • 84

    #1

    basic question on *=

    I'm now since a couple of weeks debugging a program and am now at the point where I don't know anymore what could be wrong (actually it compiles but does not do what I want)...
    Anyway, if i want to write a=b*(c+d) and write the following code:
    Code:
    a=b;
    a*=c+d;
    does the program what I want or does it see a=b*c+d?
    I really hope it is the latter possibility, because that risks to solve my problem... thanks in advance either way
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by sanctus
    I'm now since a couple of weeks debugging a program and am now at the point where I don't know anymore what could be wrong (actually it compiles but does not do what I want)...
    Anyway, if i want to write a=b*(c+d) and write the following code:
    Code:
    a=b;
    a*=c+d;
    does the program what I want or does it see a=b*c+d?
    I really hope it is the latter possibility, because that risks to solve my problem... thanks in advance either way
    It's same as a=a*(c+d) and becaue of a=b it is same as a=b*(c+d);

    Savage

    Comment

    • Silent1Mezzo
      New Member
      • Feb 2007
      • 208

      #3
      Originally posted by sanctus
      I'm now since a couple of weeks debugging a program and am now at the point where I don't know anymore what could be wrong (actually it compiles but does not do what I want)...
      Anyway, if i want to write a=b*(c+d) and write the following code:
      Code:
      a=b;
      a*=c+d;
      does the program what I want or does it see a=b*c+d?
      I really hope it is the latter possibility, because that risks to solve my problem... thanks in advance either way
      Code:
      a=b;
      a*=c+d;
      Does what you want it a = b * (c + d)
      If you want it to be more readable you could just assign c+d to another variable and then do a *= var; That would produce the same results but maybe be a little more easy to read.

      PS Savage beat me to it.
      Last edited by Silent1Mezzo; Jun 11 '07, 05:04 PM. Reason: Savage beat me to it.

      Comment

      • sanctus
        New Member
        • Mar 2007
        • 84

        #4
        Originally posted by Savage
        It's same as a=a*(c+d) and becaue of a=b it is same as a=b*(c+d);

        Savage
        So a*=c+d is equivalent to a*=(c+d) ?

        EDIT: and you beat me to my answer to savage :)

        Thanks very much both of you.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          This code:
          Originally posted by sanctus
          a=b;
          a*=c+d;
          is equivalent to:
          [code=cpp]
          a = b*(c+d);
          [/code]

          This is a precedence issue. The *= operator has a precedence of 16 whereas addition has a precedence of 6. That means the addition is done before the *=.

          Personally, I like your original method:

          [code=cpp]
          a = b*(c+d);
          [/code]

          because it's easier to read.

          Comment

          • ssorower
            New Member
            • May 2007
            • 23

            #6
            Just thought that this precedence list

            http://www.cppreferenc e.com/operator_preced ence.html

            might help to justify why a*=c+d is equivalent to a=a*(b+c)

            --Sorower

            Comment

            • sanctus
              New Member
              • Mar 2007
              • 84

              #7
              Originally posted by weaknessforcats
              T

              Personally, I like your original method:

              [code=cpp]
              a = b*(c+d);
              [/code]

              because it's easier to read.
              I agree that it easier to read, but if the b,c,d terms are very long (about one line) and the program is huge (260 MB of code) and you haven't written the program just modified it, which means that you might not know all the variables' names defined elsewhere and inherited to where you are, makes it difficult and risky to define for every b,c,d term a variables. So it leaves you no choice.
              I would though never do it if it would be easily avoidable...

              Also thanks about the explication of why via the precedences.

              Comment

              • sanctus
                New Member
                • Mar 2007
                • 84

                #8
                Originally posted by ssorower
                Just thought that this precedence list

                http://www.cppreferenc e.com/operator_preced ence.html

                might help to justify why a*=c+d is equivalent to a=a*(b+c)

                --Sorower

                Thanks, such a list clears up also some other things, bookm arked it right away.

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #9
                  Originally posted by Silent1Mezzo
                  Code:
                  a=b;
                  a*=c+d;
                  Does what you want it a = b * (c + d)
                  If you want it to be more readable you could just assign c+d to another variable and then do a *= var; That would produce the same results but maybe be a little more easy to read.

                  PS Savage beat me to it.
                  Muhahahahahah

                  Savage

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #10
                    Originally posted by sanctus
                    So a*=c+d is equivalent to a*=(c+d) ?

                    EDIT: and you beat me to my answer to savage :)

                    Thanks very much both of you.
                    We are more than happy to help u.

                    Savage

                    Comment

                    Working...