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. Bug in conversion from double to integer (only some value)

Bug in conversion from double to integer (only some value)

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 4 Posters 1.1k 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.
  • Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    This is working fine for me:

    QString s16 = "1.6";
    QString s17 = "1.7";
    QString s18 = "1.8";
    auto i16 = uint8_t(s16.toDouble() * 10);
    auto i17 = uint8_t(s17.toDouble() * 10);
    auto i18 = uint8_t(s18.toDouble() * 10);
    qDebug() << int(i16) << int(i17) << int(i18);
    

    Please provide a minimal, compilable example of your problem - no QLineEdit needed as I showed you.
    Also we don't care what QtCreator version you are using but the used Qt version.

    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
    • J Offline
      J Offline
      JAHT
      wrote on last edited by
      #3

      I have made a program as you said and the result is 16,16,18.

      Sorry the version is Qt 5.15.2 MinGW_32_bit
      I think is a bug from this version Qt 5.15.2 MinGW_32_bit

      A colleague have tested with Qt 6.6.0 MinGW_32_bit has the right value.
      16,17,18.

      JonBJ 1 Reply Last reply
      0
      • J JAHT

        I have made a program as you said and the result is 16,16,18.

        Sorry the version is Qt 5.15.2 MinGW_32_bit
        I think is a bug from this version Qt 5.15.2 MinGW_32_bit

        A colleague have tested with Qt 6.6.0 MinGW_32_bit has the right value.
        16,17,18.

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

        @JAHT said in Bug in conversion from double to integer (only some value):

        I think is a bug from this version Qt 5.15.2 MinGW_32_bit

        Rather worrying!....

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #5

          I dobut there is a bug in such a simple thing.
          Please provide the output of the three toDouble() without multiplication.

          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
          • J Offline
            J Offline
            JAHT
            wrote on last edited by
            #6

            The double value are right.

            QString s16 = "1.6";
            QString s17 = "1.7";
            QString s18 = "1.8";
            auto i16 = uint8_t(s16.toDouble() * 10);
            auto i17 = uint8_t(s17.toDouble() * 10);
            auto i18 = uint8_t(s18.toDouble() * 10);
            qDebug() << s16.toDouble() << s17.toDouble() << s18.toDouble();
            qDebug() << int(i16) << int(i17) << int(i18);
            

            10:27:17: Starting D:\TrabajoJose\DoubleConversion\build-DoubleConversion-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\DoubleConversion.exe ...
            1.6 1.7 1.8
            16 16 18

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #7

              So no Qt problem here.

              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
              0
              • Christian EhrlicherC Christian Ehrlicher

                So no Qt problem here.

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

                @Christian-Ehrlicher , @JAHT
                uin8_t(double) does floor not any kind of round, right? So a tiny difference where what shows as 1.7 is actually 1.6999999999999 and * 10 gives 16.999999999 would be enough for uint_t() to produce 16 rather than 17? Still not sure why 32-bit worse than 64-bit, are doubles not 64-bit in both (try printing sizeof(double) in each)? I think this code should not directly use uint_t(double), some sort of math round() first? Or add 0.5 like uint8_t(s17.toDouble() * 10 + 0.5);? I always add 0.5 (or 0.4999 depending on what you want to happen to x.5) before inting a double.

                1 Reply Last reply
                2
                • J Offline
                  J Offline
                  JAHT
                  wrote on last edited by
                  #9

                  Yes, it seems that that is the issue, I was wrong when saying the version used by my colleague that it worked, it is Qt 6.6.0 MinGW_64_bit

                  JonBJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by Christian Ehrlicher
                    #10

                    Again: this has nothing to do with Qt but maybe with the compiler you're using (mingw version whatever). And @JonB is correct - you should round your number before otherwise it's truncated.

                    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
                    • J JAHT

                      Yes, it seems that that is the issue, I was wrong when saying the version used by my colleague that it worked, it is Qt 6.6.0 MinGW_64_bit

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

                      @JAHT
                      I am a bit lost as to whether you now say it does or does not work correctly in 64-bit MinGW. But in any case if you think about it I suggest you always add 0.5/0.4999 to a double before you int it if you want it rounded not floored.

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        JAHT
                        wrote on last edited by
                        #12

                        @Christian-Ehrlicher @JonB

                        Thank you for your suggestion.

                        With 32-bit MinGW it doesn´t work and with 64-bit MinGW it works

                        1 Reply Last reply
                        0
                        • JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #13

                          If 32-bit MinGW says unit_t(1.7 * 10 + 0.5) == 16 I would be surprised/shocked, and worry about anything (at least with floating point) compiled with it, but I cannot test....

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            JAHT
                            wrote on last edited by JAHT
                            #14

                            Yes with:

                            QString s16 = "1.6";
                             QString s17 = "1.7";
                             QString s18 = "1.8";
                             auto i16 = uint8_t(s16.toDouble() * 10+0.5);
                             auto i17 = uint8_t(s17.toDouble() * 10+0.5);
                             auto i18 = uint8_t(s18.toDouble() * 10+0.5);
                             qDebug() << s16.toDouble() << s17.toDouble() << s18.toDouble();
                             qDebug() << int(i16) << int(i17) << int(i18);
                            

                            The result is as expected with MingW_32

                            11:22:25: Starting D:\TrabajoJose\DoubleConversion\build-DoubleConversion-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\DoubleConversion.exe ...
                            1.6 1.7 1.8
                            16 17 18

                            Thank you very much @Christian-Ehrlicher @JonB

                            Christian EhrlicherC 1 Reply Last reply
                            1
                            • J JAHT

                              Yes with:

                              QString s16 = "1.6";
                               QString s17 = "1.7";
                               QString s18 = "1.8";
                               auto i16 = uint8_t(s16.toDouble() * 10+0.5);
                               auto i17 = uint8_t(s17.toDouble() * 10+0.5);
                               auto i18 = uint8_t(s18.toDouble() * 10+0.5);
                               qDebug() << s16.toDouble() << s17.toDouble() << s18.toDouble();
                               qDebug() << int(i16) << int(i17) << int(i18);
                              

                              The result is as expected with MingW_32

                              11:22:25: Starting D:\TrabajoJose\DoubleConversion\build-DoubleConversion-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\DoubleConversion.exe ...
                              1.6 1.7 1.8
                              16 17 18

                              Thank you very much @Christian-Ehrlicher @JonB

                              Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              @JAHT said in Bug in conversion from double to integer (only some value):

                              +0.5

                              use a proper round function or you will get wrong results with negative numbers.

                              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
                              • JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #16

                                Great that is working now. But can you please write s16.toDouble() * 10 + 0.5 rather than * 10+0.5 as that just encourages mis-reading :)

                                @Christian-Ehrlicher said in Bug in conversion from double to integer (only some value):

                                use a proper round function or you will get wrong results with negative numbers.

                                Never thought about that as I don't have negative numbers! What would you suggest? Because I am worried that round() from the math library delivers its result as a double not an int. Then if the conversion of that from double to int still has some tiny floating point precision issue, as your tests claim to show, I am wondering if that still might produce 16 where it ought be 17...?

                                S 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  Great that is working now. But can you please write s16.toDouble() * 10 + 0.5 rather than * 10+0.5 as that just encourages mis-reading :)

                                  @Christian-Ehrlicher said in Bug in conversion from double to integer (only some value):

                                  use a proper round function or you will get wrong results with negative numbers.

                                  Never thought about that as I don't have negative numbers! What would you suggest? Because I am worried that round() from the math library delivers its result as a double not an int. Then if the conversion of that from double to int still has some tiny floating point precision issue, as your tests claim to show, I am wondering if that still might produce 16 where it ought be 17...?

                                  S Offline
                                  S Offline
                                  SimonSchroeder
                                  wrote on last edited by
                                  #17

                                  @JonB said in Bug in conversion from double to integer (only some value):

                                  Because I am worried that round() from the math library delivers its result as a double not an int.

                                  Well, there are also lround and llround (https://en.cppreference.com/w/cpp/numeric/math/round). Hopefully, these two functions don't provide any problems with corner cases.

                                  JonBJ 1 Reply Last reply
                                  1
                                  • S SimonSchroeder

                                    @JonB said in Bug in conversion from double to integer (only some value):

                                    Because I am worried that round() from the math library delivers its result as a double not an int.

                                    Well, there are also lround and llround (https://en.cppreference.com/w/cpp/numeric/math/round). Hopefully, these two functions don't provide any problems with corner cases.

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

                                    @SimonSchroeder I didn't know about std::lround(), that should be acceptable if preferred to adding 0.5 and then taking int.

                                    1 Reply Last reply
                                    0

                                    • Login

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