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. round() function rounds off .5 values
Qt 6.11 is out! See what's new in the release blog

round() function rounds off .5 values

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

    Hey,
    I wrote this code:

    void InputNodeInstance::changeSize(QString side, float mult){
        double newWidth = width * mult;
        width = round(newWidth);
        double newHeight = height * mult;
        height = round(newHeight);
        double newCornerRadius = cornerRadius * mult;
        cornerRadius = round(newCornerRadius);
        qDebug() << "newWidth:" << newWidth;
        qDebug() << "width: " << width;
        qDebug() << "cornerRadius: " << cornerRadius;
    }
    

    When I call the function with 0.9 as mult, I get this output:

    newWidth: 49.5
    width:  49
    cornerRadius:  15
    

    When I add this line to the code:

    qDebug() << round(newWidth);
    

    it outputs 49. Why does round() function round 49.5 to 49 !?
    Thanks for answers!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      because that is how it works?
      http://www.cplusplus.com/reference/cmath/round/

      Sorry should be 50 :) according to example.

      So that i dont know :)

      qDebug() << round(5.5); shows 6
      qDebug() << round(49.5); show 50

      const char * format = "%.1f \t%.1f \t%.1f \t%.1f \t%.1f\n";
      printf (format, 5.5,round( 5.5),floor( 5.5),ceil( 5.5),trunc( 5.5));
      does as expected.
      5.5 6.0 5.0 6.0 5.0

      NiagarerN 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        because that is how it works?
        http://www.cplusplus.com/reference/cmath/round/

        Sorry should be 50 :) according to example.

        So that i dont know :)

        qDebug() << round(5.5); shows 6
        qDebug() << round(49.5); show 50

        const char * format = "%.1f \t%.1f \t%.1f \t%.1f \t%.1f\n";
        printf (format, 5.5,round( 5.5),floor( 5.5),ceil( 5.5),trunc( 5.5));
        does as expected.
        5.5 6.0 5.0 6.0 5.0

        NiagarerN Offline
        NiagarerN Offline
        Niagarer
        wrote on last edited by Niagarer
        #3

        @mrjj
        Hm, but as you can see, here it rounds 49.5 to 49...
        I don't understand that.
        And it gets even weirder: when I do

        qDebug() << width * mult + 0.5;
        width = width * mult + 0.5;
        qDebug() << width;
        

        it outputs
        50
        49
        ???

        1 Reply Last reply
        0
        • artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          Hi,
          following this thread I would assume it is float to double conversion that you can blame. Would it be possible for you to (just for the test) use one single type throughout the code to check if problem persists?

          For more information please re-read.

          Kind Regards,
          Artur

          NiagarerN 1 Reply Last reply
          3
          • artwawA artwaw

            Hi,
            following this thread I would assume it is float to double conversion that you can blame. Would it be possible for you to (just for the test) use one single type throughout the code to check if problem persists?

            NiagarerN Offline
            NiagarerN Offline
            Niagarer
            wrote on last edited by
            #5

            @artwaw
            Yes, I changed it to double in the parameter list and now, I get 50... ok.... I want to understand that... How is this possible...
            Now, that's completely blowing my mind
            Really big thank you, I would not have thought, this is a problem

            artwawA 1 Reply Last reply
            0
            • NiagarerN Niagarer

              @artwaw
              Yes, I changed it to double in the parameter list and now, I get 50... ok.... I want to understand that... How is this possible...
              Now, that's completely blowing my mind
              Really big thank you, I would not have thought, this is a problem

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #6

              @Niagarer My pleasure to help.

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              2
              • NiagarerN Offline
                NiagarerN Offline
                Niagarer
                wrote on last edited by
                #7

                The problem is explained in detail here:
                https://stackoverflow.com/questions/46389221/c-round49-5-returns-49-when-called-with-variable-in-qt/

                1 Reply Last reply
                2

                • Login

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