Results 1 to 9 of 9

Thread: qRound problem!

  1. #1
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation qRound problem!

    Hi all, this time i have a little problem with qRound. Based on the tutorial:
    int qRound ( double value )
    Rounds value up to the nearest integer.
    So, I think that the result of, for example, 256*2.22 (=568.32) should be 569 but the result that qRound gives me is 568. In my point of view this is not rounded up to the next integer. Anybody could explain me why? Thanks
    Last edited by SkripT; 19th January 2006 at 18:27.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qRound problem!

    It looks like that "up" shouldn't be there.
    Qt Code:
    1. inline int qRound(qreal d)
    2. { return d >= 0.0 ? int(d + 0.5) : int(d - int(d-1) + 0.5) + int(d-1); }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qRound problem!

    Quote Originally Posted by SkripT
    Hi all, this time i have a little problem with qRound. Based on the tutorial:

    So, I think that the result of, for example, 256*2.22 (=568.32) should be 569 but the result that qRound gives me is 568. In my point of view this is not rounded up to the next integer. Anybody could explain me why? Thanks
    I ran into similar problems and discovered, after searching the topic in Google, that there is an entire sub-branch of C++ devoted to rounding algorithms. Someone offered this:
    Qt Code:
    1. round(x) = floor( x + 0.5f);
    To copy to clipboard, switch view to plain text mode 
    I borrowed this method from one of the many offerings:
    Qt Code:
    1. float homestead::Round(float Value, int NumPlaces) {
    2. int k;
    3. float Factor;
    4. float Temp;
    5. Factor = 1.0;
    6. for (k = 0; k < NumPlaces; k++)
    7. Factor = Factor * 10.0;
    8.  
    9. Temp = Value * Factor + 0.5;
    10. return Temp / Factor;
    11. }
    To copy to clipboard, switch view to plain text mode 
    because it allows me to specifiy the number of roundoff places.

  4. #4
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qRound problem!

    Hi thanks both. Finally what I've done is:
    Qt Code:
    1. int roundUp(double v)
    2. int aux = (int) v;
    3. return v - aux > 0 ? aux + 1 : aux;
    To copy to clipboard, switch view to plain text mode 
    Last edited by SkripT; 20th January 2006 at 08:30.

  5. #5
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qRound problem!

    If I understand correctly you are looking for http://www.cppreference.com/stdmath/ceil.html

  6. #6
    Join Date
    Jan 2006
    Posts
    109
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: qRound problem!

    This is indeed a bug either in the documentation or the Qt code. It will be fixed.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qRound problem!

    As far as I understand "round up" is an english expression (phrasal verb) which doesn't mean "round upwards" (just like "look up sth in the dictionary" doesn't tell you to look in the sky right?), but it can be misleading

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qRound problem!

    Quote Originally Posted by wysota
    As far as I understand "round up" is an english expression (phrasal verb) which doesn't mean "round upwards" (just like "look up sth in the dictionary" doesn't tell you to look in the sky right?)
    My dictionary says that when it comes to maths "to round up" means "to round upwards", in other contexts it behaves like you wrote.

    but it can be misleading
    Documentation shouldn't be misleading in any way.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qRound problem!

    Quote Originally Posted by jacek
    Documentation shouldn't be misleading in any way.
    Sure, I don't say it should.

    Edit: and I found "round up" in math meaning in my dictionary too
    Last edited by wysota; 21st January 2006 at 16:13.

Similar Threads

  1. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 12:48
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  4. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.