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 ?
Forum Updated to NodeBB v4.3 + New Features

why below logic give warning ?

Scheduled Pinned Locked Moved Solved C++ Gurus
7 Posts 5 Posters 759 Views 1 Watching
  • 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 7 Jan 2021, 12:37 last edited by
    #1

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

    if((newsum-oldsum < 10) && ((newsum-oldsum) > -10))
    
    J M 2 Replies Last reply 7 Jan 2021, 12:39
    0
    • Q Qt embedded developer
      7 Jan 2021, 12:48

      @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))
      .
      .
      .

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 7 Jan 2021, 12:53 last edited by jsulm 1 Jul 2021, 12:55
      #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
        7 Jan 2021, 12:37

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

        if((newsum-oldsum < 10) && ((newsum-oldsum) > -10))
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 7 Jan 2021, 12:39 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 7 Jan 2021, 12:48
        1
        • J jsulm
          7 Jan 2021, 12:39

          @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 7 Jan 2021, 12:48 last edited by Qt embedded developer 1 Jul 2021, 12:52
          #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))
          .
          .
          .

          K J 2 Replies Last reply 7 Jan 2021, 12:52
          0
          • Q Qt embedded developer
            7 Jan 2021, 12:37

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

            if((newsum-oldsum < 10) && ((newsum-oldsum) > -10))
            
            M Offline
            M Offline
            mvuori
            wrote on 7 Jan 2021, 12:52 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
              7 Jan 2021, 12:48

              @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))
              .
              .
              .

              K Offline
              K Offline
              KroMignon
              wrote on 7 Jan 2021, 12:52 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
                7 Jan 2021, 12:48

                @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))
                .
                .
                .

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 7 Jan 2021, 12:53 last edited by jsulm 1 Jul 2021, 12:55
                #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
                • K Offline
                  K Offline
                  Kent-Dorfman
                  wrote on 8 Jan 2021, 07:06 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

                  4/7

                  7 Jan 2021, 12:52

                  • Login

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