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

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 4 Posters 860 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.
  • J Offline
    J Offline
    JAHT
    wrote on 31 May 2024, 08:28 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
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 31 May 2024, 08:30 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

      J 1 Reply Last reply 31 May 2024, 08:39
      0
      • C Christian Ehrlicher
        31 May 2024, 08:30

        So no Qt problem here.

        J Online
        J Online
        JonB
        wrote on 31 May 2024, 08:39 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 31 May 2024, 08:52 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

          J 1 Reply Last reply 31 May 2024, 08:53
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 31 May 2024, 08:53 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
              31 May 2024, 08:52

              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

              J Online
              J Online
              JonB
              wrote on 31 May 2024, 08:53 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 31 May 2024, 09:08 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
                • J Online
                  J Online
                  JonB
                  wrote on 31 May 2024, 09:15 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 31 May 2024, 09:24 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

                    C 1 Reply Last reply 31 May 2024, 09:35
                    1
                    • J JAHT
                      31 May 2024, 09:24

                      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

                      C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 31 May 2024, 09:35 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
                      • J Online
                        J Online
                        JonB
                        wrote on 31 May 2024, 09:41 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 3 Jun 2024, 06:46
                        0
                        • J JonB
                          31 May 2024, 09:41

                          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 3 Jun 2024, 06:46 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.

                          J 1 Reply Last reply 3 Jun 2024, 07:02
                          1
                          • S SimonSchroeder
                            3 Jun 2024, 06:46

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

                            J Online
                            J Online
                            JonB
                            wrote on 3 Jun 2024, 07:02 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

                            15/18

                            31 May 2024, 09:35

                            • Login

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