Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. why below logic give warning ?

why below logic give warning ?

Scheduled Pinned Locked Moved Solved C++ Gurus
7 Posts 5 Posters 737 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #1

    warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

    if((newsum-oldsum < 10) && ((newsum-oldsum) > -10))
    
    jsulmJ M 2 Replies Last reply
    0
    • Q Qt embedded developer

      @jsulm but the summation result possible to comes negative.

      to avoid warning what i need to write.

      i.e.
      unsigned int x,y;

      x=2000;
      y=3000;

      if( (y-x) > (-10))
      .
      .
      .

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #6

      @Qt-embedded-developer If I'm not mistaken the result type of "newsum-oldsum" will be unsigned if both are unsigned. Try this:

      unsigned a = 1;
      unsigned b = 2;
      qDebug() << a - b;
      qDebug() << signed(a - b);
      

      You will be surprised :-)
      You need either to cast the result to signed or cast at least one of both variables to signed.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • Q Qt embedded developer

        warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

        if((newsum-oldsum < 10) && ((newsum-oldsum) > -10))
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Qt-embedded-developer Well, I guess newsum and oldsum are unsigned? And you compare it with -10, which is signed...

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        Q 1 Reply Last reply
        1
        • jsulmJ jsulm

          @Qt-embedded-developer Well, I guess newsum and oldsum are unsigned? And you compare it with -10, which is signed...

          Q Offline
          Q Offline
          Qt embedded developer
          wrote on last edited by Qt embedded developer
          #3

          @jsulm but the summation result possible to comes negative.

          to avoid warning what i need to write.

          i.e.
          unsigned int x,y;

          x=2000;
          y=3000;

          if( (y-x) > (-10))
          .
          .
          .

          KroMignonK jsulmJ 2 Replies Last reply
          0
          • Q Qt embedded developer

            warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

            if((newsum-oldsum < 10) && ((newsum-oldsum) > -10))
            
            M Offline
            M Offline
            mvuori
            wrote on last edited by
            #4

            You asking in a wrong place. This has nothing special to do with mobile & embedded systems - or even Qt.

            1 Reply Last reply
            1
            • Q Qt embedded developer

              @jsulm but the summation result possible to comes negative.

              to avoid warning what i need to write.

              i.e.
              unsigned int x,y;

              x=2000;
              y=3000;

              if( (y-x) > (-10))
              .
              .
              .

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #5

              @Qt-embedded-developer said in why below logic give warning ?:

              to avoid warning what i need to write.

              for example:

              if(qAbs(int(newsum)-oldsum) < 10) 
              {
              }
              or 
              if((qMax(newsum, oldsum) - qMin(newsum, oldsum) ) < 10) 
              {
              }
              
              

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              1
              • Q Qt embedded developer

                @jsulm but the summation result possible to comes negative.

                to avoid warning what i need to write.

                i.e.
                unsigned int x,y;

                x=2000;
                y=3000;

                if( (y-x) > (-10))
                .
                .
                .

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #6

                @Qt-embedded-developer If I'm not mistaken the result type of "newsum-oldsum" will be unsigned if both are unsigned. Try this:

                unsigned a = 1;
                unsigned b = 2;
                qDebug() << a - b;
                qDebug() << signed(a - b);
                

                You will be surprised :-)
                You need either to cast the result to signed or cast at least one of both variables to signed.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                4
                • Kent-DorfmanK Offline
                  Kent-DorfmanK Offline
                  Kent-Dorfman
                  wrote on last edited by
                  #7

                  The problem would be avoided by thinking about the safety of the operation. storing the difference of the values in a temporary is more readable than the original expression, and has the desirable side effect of making the type of the result explicit. Furthermore, putting the lower bound conditional after the upper bound conditional is goofy.

                  int diff = (x - y);
                  if ((diff > -10) && ((diff < 10)) { true; }

                  I suppose you could implement the conditional expression as a lambda if you're into such things, but IMHO that too would make it less readable.

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved