Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How mathematical operations done correctly in qtCreator?
Forum Updated to NodeBB v4.3 + New Features

How mathematical operations done correctly in qtCreator?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 6 Posters 569 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.
  • WaseeW Offline
    WaseeW Offline
    Wasee
    wrote on last edited by VRonin
    #1

    Hi everyone;
    when I put the from GUI my gap is 400 at this my first if condition should execute but in current code my all if conditions executing why?

                 gap=((y)-(x));
         qDebug()<<"gap:"<<gap;
    
         if(gap<=448){
             if(392<=gap<=448)
             {
                 QProcess p1 ;
                 p1.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/echo_example.sh");
             }
             if (336<=gap<=392)
             {
                 QProcess p2 ;
                 p2.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/comment_example.sh");
             }
             if (280<=gap<=336)
             {
                 QProcess p3 ;
                 p3.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/for_example.sh");
             }
             if (224<=gap<=280)
             {
                 QProcess p4 ;
                 p4.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/multiline_example.sh");
             }
             if (168<=gap<=280)
             {
                 QProcess p5 ;
                 p5.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/script.sh");
             }
    
         }
    
    
    }
    

    thanks

    jsulmJ KroMignonK 2 Replies Last reply
    0
    • WaseeW Wasee

      Hi everyone;
      when I put the from GUI my gap is 400 at this my first if condition should execute but in current code my all if conditions executing why?

                   gap=((y)-(x));
           qDebug()<<"gap:"<<gap;
      
           if(gap<=448){
               if(392<=gap<=448)
               {
                   QProcess p1 ;
                   p1.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/echo_example.sh");
               }
               if (336<=gap<=392)
               {
                   QProcess p2 ;
                   p2.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/comment_example.sh");
               }
               if (280<=gap<=336)
               {
                   QProcess p3 ;
                   p3.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/for_example.sh");
               }
               if (224<=gap<=280)
               {
                   QProcess p4 ;
                   p4.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/multiline_example.sh");
               }
               if (168<=gap<=280)
               {
                   QProcess p5 ;
                   p5.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/script.sh");
               }
      
           }
      
      
      }
      

      thanks

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Wasee First: this has nothing to do with Qt or QtCreator. This is pure C++.

      Did you verify that gap is in fact 400?
      What does your qDebug print?
      How often is this code executed?
      How did you verify that all if conditions are executed?

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

      WaseeW 1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Wasee said in How mathematical operations done correctly in qtCreator?:

        if(392<=gap<=448)

        And to add @jsulm - this is not what you think it is doing...

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        3
        • jsulmJ jsulm

          @Wasee First: this has nothing to do with Qt or QtCreator. This is pure C++.

          Did you verify that gap is in fact 400?
          What does your qDebug print?
          How often is this code executed?
          How did you verify that all if conditions are executed?

          WaseeW Offline
          WaseeW Offline
          Wasee
          wrote on last edited by
          #4

          @jsulm Hi;
          Thanks to your valuable reply!

          1. Yes I verify my gap value is correct
          2. qDebug to see the gap value
          3. Code is executed when gap value x and y value will be inserted
          4. All conditions are executed even with no effect of gap value

          I want below all if conditions should be dependent on gap value now which is not.
          thanks

          jsulmJ 1 Reply Last reply
          0
          • WaseeW Wasee

            @jsulm Hi;
            Thanks to your valuable reply!

            1. Yes I verify my gap value is correct
            2. qDebug to see the gap value
            3. Code is executed when gap value x and y value will be inserted
            4. All conditions are executed even with no effect of gap value

            I want below all if conditions should be dependent on gap value now which is not.
            thanks

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Wasee Valid C++ syntax (which you should learn) is:

            if(392 <= gap && gap <= 448)
            

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

            S 1 Reply Last reply
            3
            • WaseeW Wasee

              Hi everyone;
              when I put the from GUI my gap is 400 at this my first if condition should execute but in current code my all if conditions executing why?

                           gap=((y)-(x));
                   qDebug()<<"gap:"<<gap;
              
                   if(gap<=448){
                       if(392<=gap<=448)
                       {
                           QProcess p1 ;
                           p1.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/echo_example.sh");
                       }
                       if (336<=gap<=392)
                       {
                           QProcess p2 ;
                           p2.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/comment_example.sh");
                       }
                       if (280<=gap<=336)
                       {
                           QProcess p3 ;
                           p3.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/for_example.sh");
                       }
                       if (224<=gap<=280)
                       {
                           QProcess p4 ;
                           p4.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/multiline_example.sh");
                       }
                       if (168<=gap<=280)
                       {
                           QProcess p5 ;
                           p5.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/script.sh");
                       }
              
                   }
              
              
              }
              

              thanks

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

              @Wasee said in How mathematical operations done correctly in qtCreator?:

              if(gap<=448){
              if(392<=gap<=448)
              {
              QProcess p1 ;
              p1.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/echo_example.sh");
              }
              if (336<=gap<=392)
              {
              QProcess p2 ;
              p2.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/comment_example.sh");
              }
              if (280<=gap<=336)
              {
              QProcess p3 ;
              p3.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/for_example.sh");
              }
              if (224<=gap<=280)
              {
              QProcess p4 ;
              p4.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/multiline_example.sh");
              }
              if (168<=gap<=280)
              {
              QProcess p5 ;
              p5.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/script.sh");
              }

              What is this? this not valid C++ code.

              You should change your code to something like:

              if(gap<=448){
                   if(gap >= 392)
                   {
                       QProcess p1 ;
                       p1.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/echo_example.sh");
                   }
                   else if (gap >= 336)
                   {
                       QProcess p2 ;
                       p2.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/comment_example.sh");
                   }
                   else if (gap >= 280)
                   {
                       QProcess p3 ;
                       p3.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/for_example.sh");
                   }
                   else if (gap >= 224)
                   {
                       QProcess p4 ;
                       p4.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/multiline_example.sh");
                   }
                   else if (gap >= 168)
                   {
                       QProcess p5 ;
                       p5.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/script.sh");
                   }
               }
              

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

              J.HilkJ 1 Reply Last reply
              0
              • KroMignonK KroMignon

                @Wasee said in How mathematical operations done correctly in qtCreator?:

                if(gap<=448){
                if(392<=gap<=448)
                {
                QProcess p1 ;
                p1.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/echo_example.sh");
                }
                if (336<=gap<=392)
                {
                QProcess p2 ;
                p2.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/comment_example.sh");
                }
                if (280<=gap<=336)
                {
                QProcess p3 ;
                p3.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/for_example.sh");
                }
                if (224<=gap<=280)
                {
                QProcess p4 ;
                p4.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/multiline_example.sh");
                }
                if (168<=gap<=280)
                {
                QProcess p5 ;
                p5.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/script.sh");
                }

                What is this? this not valid C++ code.

                You should change your code to something like:

                if(gap<=448){
                     if(gap >= 392)
                     {
                         QProcess p1 ;
                         p1.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/echo_example.sh");
                     }
                     else if (gap >= 336)
                     {
                         QProcess p2 ;
                         p2.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/comment_example.sh");
                     }
                     else if (gap >= 280)
                     {
                         QProcess p3 ;
                         p3.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/for_example.sh");
                     }
                     else if (gap >= 224)
                     {
                         QProcess p4 ;
                         p4.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/multiline_example.sh");
                     }
                     else if (gap >= 168)
                     {
                         QProcess p5 ;
                         p5.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/script.sh");
                     }
                 }
                
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #7

                @KroMignon now that we're on the topic of correcting code in general let me change it so, that it doesn't hurt my eyes :P

                if(gap <= 448 && gab >= 168) {
                     QStringList args ("-c"); 
                     if(gap >= 392) {
                         args << "/home/ijaz/bashScripts/echo_example.sh";
                     } else if (gap >= 336) {
                         args << "/home/ijaz/bashScripts/comment_example.sh");
                     } else if (gap >= 280) {
                         args << "/home/ijaz/bashScripts/for_example.sh");
                     } else if (gap >= 224) {
                         args << "/home/ijaz/bashScripts/multiline_example.sh");
                     } else {
                        args << "/home/ijaz/bashScripts/script.sh");
                     }
                     Process::startDetached("/bin/bash", args);
                 }
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                KroMignonK 1 Reply Last reply
                2
                • J.HilkJ J.Hilk

                  @KroMignon now that we're on the topic of correcting code in general let me change it so, that it doesn't hurt my eyes :P

                  if(gap <= 448 && gab >= 168) {
                       QStringList args ("-c"); 
                       if(gap >= 392) {
                           args << "/home/ijaz/bashScripts/echo_example.sh";
                       } else if (gap >= 336) {
                           args << "/home/ijaz/bashScripts/comment_example.sh");
                       } else if (gap >= 280) {
                           args << "/home/ijaz/bashScripts/for_example.sh");
                       } else if (gap >= 224) {
                           args << "/home/ijaz/bashScripts/multiline_example.sh");
                       } else {
                          args << "/home/ijaz/bashScripts/script.sh");
                       }
                       Process::startDetached("/bin/bash", args);
                   }
                  
                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #8

                  @J-Hilk said in How mathematical operations done correctly in qtCreator?:

                  now that we're on the topic of correcting code in general let me change it so, that it doesn't hurt my eyes :P

                  This was not my idea to give to "cleanest" code, it was just about the test, which were wrong.
                  And yes, the start code and my little correction are horrible ;)

                  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
                  • jsulmJ jsulm

                    @Wasee Valid C++ syntax (which you should learn) is:

                    if(392 <= gap && gap <= 448)
                    
                    S Offline
                    S Offline
                    SimonSchroeder
                    wrote on last edited by
                    #9

                    @jsulm said in How mathematical operations done correctly in qtCreator?:

                    Valid C++ syntax (which you should learn) is:
                    if(392 <= gap && gap <= 448)

                    Maybe we can help him improve on his C++ skills: If you write if(392<=gap<=448) what C++ does is first evaluate 392<=gap which is true. It looks like true is then implicitly casted to an int. This int is now 1 and that is compared to 448. Since in all your conditionals 400 is larger than your lower bound, you always compare 1 to the upper bound. That is the entire reason why all conditions are executed.

                    The compiler might even optimize your checks away because it can figure out that you are always comparing either true or false to the upper bound. And in every condition both 1 and 0 are less than your upper bounds.

                    1 Reply Last reply
                    6
                    • WaseeW Offline
                      WaseeW Offline
                      Wasee
                      wrote on last edited by
                      #10

                      Hi everyone!
                      I am adding/subtracting two string values but giving me wrong behavior.

                      QString t="400000000";
                      QString p="0x208050";
                      QString result = t+p;
                      QString result=t-p;
                      qDebug()<<"Value:"<<result;
                      

                      Value: "4000000000x208050" for adding
                      value: error for subtraction no matching operator - operands are strings

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • WaseeW Wasee

                        Hi everyone!
                        I am adding/subtracting two string values but giving me wrong behavior.

                        QString t="400000000";
                        QString p="0x208050";
                        QString result = t+p;
                        QString result=t-p;
                        qDebug()<<"Value:"<<result;
                        

                        Value: "4000000000x208050" for adding
                        value: error for subtraction no matching operator - operands are strings

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Wasee said in How mathematical operations done correctly in qtCreator?:

                        I am adding/subtracting two string values

                        You can not add/substract string, only numbers... see QString::toInt() and similar QString functions.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        6

                        • Login

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