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. Error in IF function regarding comparison with decimal value
Forum Updated to NodeBB v4.3 + New Features

Error in IF function regarding comparison with decimal value

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 6 Posters 683 Views 2 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.
  • A Offline
    A Offline
    ALEX_1
    wrote on 13 Jul 2020, 00:07 last edited by
    #1

    Hello everyone, my question arises from the following error:

    float pp=0.7;
    if(pp==0.7){
            qDebug()<<"Correcto";
        }else{
            qDebug()<<"Incorrecto";
        }
    

    The error is that the compilation throws me "Incorrect" even though the condition is true, making me think that the float value was not saved properly, is there any solution for this?

    P P K A 4 Replies Last reply 13 Jul 2020, 02:44
    0
    • A ALEX_1
      13 Jul 2020, 00:07

      Hello everyone, my question arises from the following error:

      float pp=0.7;
      if(pp==0.7){
              qDebug()<<"Correcto";
          }else{
              qDebug()<<"Incorrecto";
          }
      

      The error is that the compilation throws me "Incorrect" even though the condition is true, making me think that the float value was not saved properly, is there any solution for this?

      P Offline
      P Offline
      Pl45m4
      wrote on 13 Jul 2020, 02:44 last edited by Pl45m4
      #2

      @ALEX_1

      Since float var pp (= 0.7) and your hard coded float 0.7 could be different when using == for comparison, better use qFuzzyCompare(float, float) instead
      https://doc.qt.io/qt-5/qtglobal.html#qFuzzyCompare-1

      It should work then... If not, I don't know what's going on there :)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      9
      • A ALEX_1
        13 Jul 2020, 00:07

        Hello everyone, my question arises from the following error:

        float pp=0.7;
        if(pp==0.7){
                qDebug()<<"Correcto";
            }else{
                qDebug()<<"Incorrecto";
            }
        

        The error is that the compilation throws me "Incorrect" even though the condition is true, making me think that the float value was not saved properly, is there any solution for this?

        P Offline
        P Offline
        Pablo J. Rogina
        wrote on 14 Jul 2020, 17:14 last edited by
        #3

        @ALEX_1 you may want to look at articles (like this one or this other one) talking about float number comparison, which is not an straight forward task.

        You may want to think of comparing float point numbers using some tolerance value (i.e. epsilon).

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        1
        • A ALEX_1
          13 Jul 2020, 00:07

          Hello everyone, my question arises from the following error:

          float pp=0.7;
          if(pp==0.7){
                  qDebug()<<"Correcto";
              }else{
                  qDebug()<<"Incorrecto";
              }
          

          The error is that the compilation throws me "Incorrect" even though the condition is true, making me think that the float value was not saved properly, is there any solution for this?

          K Offline
          K Offline
          KroMignon
          wrote on 15 Jul 2020, 09:11 last edited by
          #4

          @ALEX_1 said in Error in IF function regarding comparison with decimal value:

          The error is that the compilation throws me "Incorrect" even though the condition is true

          Float or Double values are not precise, they are splitted in to fraction and exponent (cf. https://en.wikipedia.org/wiki/Floating-point_arithmetic).
          if you want to compare if float or double values are equal, you have to take care about value "imprecisions".

          The best/easiest way is to use qFuzzyCompare() (cf https://doc.qt.io/qt-5/qtglobal.html#qFuzzyCompare)

          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
          • A ALEX_1
            13 Jul 2020, 00:07

            Hello everyone, my question arises from the following error:

            float pp=0.7;
            if(pp==0.7){
                    qDebug()<<"Correcto";
                }else{
                    qDebug()<<"Incorrecto";
                }
            

            The error is that the compilation throws me "Incorrect" even though the condition is true, making me think that the float value was not saved properly, is there any solution for this?

            A Offline
            A Offline
            ALEX_1
            wrote on 20 May 2022, 13:40 last edited by
            #5

            @ALEX_1 said in Error in IF function regarding comparison with decimal value:

            Hello everyone, my question arises from the following error:

            float pp=0.7;
            if(pp==0.7){
                    qDebug()<<"Correcto";
                }else{
                    qDebug()<<"Incorrecto";
                }
            

            The error is that the compilation throws me "Incorrect" even though the condition is true, making me think that the float value was not saved properly, is there any solution for this?

            C 1 Reply Last reply 20 May 2022, 14:40
            0
            • A ALEX_1
              20 May 2022, 13:40

              @ALEX_1 said in Error in IF function regarding comparison with decimal value:

              Hello everyone, my question arises from the following error:

              float pp=0.7;
              if(pp==0.7){
                      qDebug()<<"Correcto";
                  }else{
                      qDebug()<<"Incorrecto";
                  }
              

              The error is that the compilation throws me "Incorrect" even though the condition is true, making me think that the float value was not saved properly, is there any solution for this?

              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 20 May 2022, 14:40 last edited by
              #6

              @ALEX_1 said in Error in IF function regarding comparison with decimal value:

              if(pp==0.7){

              You compare a float with a double here. So 0.7f != 0.7 as @KroMignon and @Pablo-J-Rogina properly explained

              This will evaluate to true

              if (p == 0.7f) {

              But even then it will not work when you do calculations before.

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

              JonBJ 1 Reply Last reply 20 May 2022, 14:51
              1
              • C Christian Ehrlicher
                20 May 2022, 14:40

                @ALEX_1 said in Error in IF function regarding comparison with decimal value:

                if(pp==0.7){

                You compare a float with a double here. So 0.7f != 0.7 as @KroMignon and @Pablo-J-Rogina properly explained

                This will evaluate to true

                if (p == 0.7f) {

                But even then it will not work when you do calculations before.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 20 May 2022, 14:51 last edited by
                #7

                @Christian-Ehrlicher said in Error in IF function regarding comparison with decimal value:

                if (p == 0.7f) {

                Up-voted your answer despite spending minutes unable to find any p variable in any of the code posted. What is it with people's typing accuracy today...! :(

                C 1 Reply Last reply 20 May 2022, 15:33
                0
                • JonBJ JonB
                  20 May 2022, 14:51

                  @Christian-Ehrlicher said in Error in IF function regarding comparison with decimal value:

                  if (p == 0.7f) {

                  Up-voted your answer despite spending minutes unable to find any p variable in any of the code posted. What is it with people's typing accuracy today...! :(

                  C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 20 May 2022, 15:33 last edited by
                  #8

                  @JonB said in Error in IF function regarding comparison with decimal value:

                  What is it with people's typing accuracy today

                  Please blame my keyboard and not me. My 'p' key don't work well but I don't know why. Even with this knowledge I sometimes miss the 'p' somewhere. Go through my latest posts and I'm sure you will find much more missing 'p's :)

                  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
                  1

                  • Login

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