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 converting double to int

Error converting double to int

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.0k 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.
  • H Offline
    H Offline
    HB76
    wrote on last edited by
    #1

    Hi everyone !

    I am facing a strange issue converting a double to int.
    Here is what I do :

    qDebug() << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
    

    And the result I have is :

    225 224

    It gives me the same result (224) with :

    qDebug() << int(0.04*9/0.0016);
    

    I first thought that the double value of 225 was an approximation of the real value that may be closer to 224 but when I print it, why would it print exactly 225 and not 224,3257 for example ?

    Moreover, if the debugger chose to round the double value to 225, why is it rounded to 224 when converting to int then ?

    When a test this computation on my calculator it gives me exactly 225...

    Thanks for your help !

    JonBJ K 2 Replies Last reply
    0
    • H HB76

      Hi everyone !

      I am facing a strange issue converting a double to int.
      Here is what I do :

      qDebug() << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
      

      And the result I have is :

      225 224

      It gives me the same result (224) with :

      qDebug() << int(0.04*9/0.0016);
      

      I first thought that the double value of 225 was an approximation of the real value that may be closer to 224 but when I print it, why would it print exactly 225 and not 224,3257 for example ?

      Moreover, if the debugger chose to round the double value to 225, why is it rounded to 224 when converting to int then ?

      When a test this computation on my calculator it gives me exactly 225...

      Thanks for your help !

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @HB76
      All doubles (or floats) in C/C++ are only approximate values. You cannot guarantee that any number, whole/integer or with fraction/decimal places has any prefect representation, other than the number 0.

      int(doubleValue) always rounds down. Use another method for control over rounding, or lazily int(doubleValue + 0.5). Don't rely on the output of qDebug() to output doubles to any particular decimal places, use something like QString::number(double n, char format = 'g', int precision = 6) to explicitly pick your output format.

      Moreover, if the debugger chose to round the double value to 225

      qDebug() is not "the debugger" :) Examine the value inside the actual debugger for an accurate value.

      H 1 Reply Last reply
      2
      • H HB76

        Hi everyone !

        I am facing a strange issue converting a double to int.
        Here is what I do :

        qDebug() << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
        

        And the result I have is :

        225 224

        It gives me the same result (224) with :

        qDebug() << int(0.04*9/0.0016);
        

        I first thought that the double value of 225 was an approximation of the real value that may be closer to 224 but when I print it, why would it print exactly 225 and not 224,3257 for example ?

        Moreover, if the debugger chose to round the double value to 225, why is it rounded to 224 when converting to int then ?

        When a test this computation on my calculator it gives me exactly 225...

        Thanks for your help !

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        @HB76

        You are simply dealing with rounding issues on the output.

        qDebug() is an output channel which is intended for debugging purposes, but it is not the debugger.

        @HB76 said in Error converting double to int:

        why would it print exactly 225 and not 224,3257 for example ?

        How do come with this example? It has nothing to do with the actual numbers you are using.

        Try

        #include <iostream> 
        ...
        qDebug() << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
        cout << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
        cerr << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
        

        It should show the same as qDebug() output

        Vote the answer(s) that helped you to solve your issue(s)

        H 1 Reply Last reply
        2
        • JonBJ JonB

          @HB76
          All doubles (or floats) in C/C++ are only approximate values. You cannot guarantee that any number, whole/integer or with fraction/decimal places has any prefect representation, other than the number 0.

          int(doubleValue) always rounds down. Use another method for control over rounding, or lazily int(doubleValue + 0.5). Don't rely on the output of qDebug() to output doubles to any particular decimal places, use something like QString::number(double n, char format = 'g', int precision = 6) to explicitly pick your output format.

          Moreover, if the debugger chose to round the double value to 225

          qDebug() is not "the debugger" :) Examine the value inside the actual debugger for an accurate value.

          H Offline
          H Offline
          HB76
          wrote on last edited by HB76
          #4

          @JonB I know that double values are approximated but normaly all 32 bits integers values should be perfectly represented by 64 bits double, and when I try :

          double a = 0.04*double(9)/0.0016;
          qDebug() << QString::number(a,'g',6);
          

          it returns me "225"...

          And the most amazing fact is when I try :

          qDebug() << QString::number(a,'g',6) << QString::number(a,'g',6).toInt();
          

          It returns "225" 225

          JonBJ 1 Reply Last reply
          0
          • K koahnig

            @HB76

            You are simply dealing with rounding issues on the output.

            qDebug() is an output channel which is intended for debugging purposes, but it is not the debugger.

            @HB76 said in Error converting double to int:

            why would it print exactly 225 and not 224,3257 for example ?

            How do come with this example? It has nothing to do with the actual numbers you are using.

            Try

            #include <iostream> 
            ...
            qDebug() << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
            cout << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
            cerr << 0.04*double(9)/0.0016 << int(0.04*double(9)/0.0016);
            

            It should show the same as qDebug() output

            H Offline
            H Offline
            HB76
            wrote on last edited by
            #5

            @koahnig Yes I just invented these number to illustrate my point

            1 Reply Last reply
            0
            • H HB76

              @JonB I know that double values are approximated but normaly all 32 bits integers values should be perfectly represented by 64 bits double, and when I try :

              double a = 0.04*double(9)/0.0016;
              qDebug() << QString::number(a,'g',6);
              

              it returns me "225"...

              And the most amazing fact is when I try :

              qDebug() << QString::number(a,'g',6) << QString::number(a,'g',6).toInt();
              

              It returns "225" 225

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @HB76 said in Error converting double to int:

              double a = 0.04*double(9)/0.0016;

              I told you to try this in the debugger if you really want to know what the double value is. If you had bothered to try, you would have found 224.99999999999997. And what is int(224.99999999999997), or to 6 decimal places? Try qDebug() << QString::number(a,'g',20).

              normaly all 32 bits integers values should be perfectly represented by 64 bits double

              How does that have any relevance to the code you show? It doesn't.

              1 Reply Last reply
              0
              • H Offline
                H Offline
                HB76
                wrote on last edited by
                #7

                Ok I tried to run this computation on python and it gaves me :

                a = 0.04 * 9 / 0.0016
                a
                

                224.99999999999997

                int (a)
                

                224

                round(a)
                

                225

                It was just that Qt didn't showed the real value of a even with this output format

                QString::number(a,'g',16)
                

                Thanks for your help !

                JonBJ 1 Reply Last reply
                0
                • H HB76

                  Ok I tried to run this computation on python and it gaves me :

                  a = 0.04 * 9 / 0.0016
                  a
                  

                  224.99999999999997

                  int (a)
                  

                  224

                  round(a)
                  

                  225

                  It was just that Qt didn't showed the real value of a even with this output format

                  QString::number(a,'g',16)
                  

                  Thanks for your help !

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @HB76 said in Error converting double to int:

                  It was just that Qt didn't showed the real value of a even with this output format
                  QString::number(a,'g',16)

                  FGS. Count the number of digits. Yes, that is all digits, not just the ones after the decimal place, as per the documentation:

                  For the 'g' and 'G' formats, the precision represents the maximum number of significant digits (trailing zeroes are omitted).

                  Is 16 enough?

                  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