1. 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 19:27.

2. Guru
Join Date
Jan 2006
Location
Warsaw, Poland
Posts
5,372
Thanks
28
Thanked 976 Times in 912 Posts
Qt products
Platforms

Re: qRound problem!

It looks like that "up" shouldn't be there.
Qt Code:
`inline int qRound(qreal d){ 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. Intermediate user
Join Date
Jan 2006
Location
Lincoln, NE USA
Posts
177
Thanks
3
Thanked 7 Times in 6 Posts
Qt products
Platforms

Re: qRound problem!

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:
`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:
`float homestead::Round(float Value, int NumPlaces) {	int k;	float Factor;	float Temp;	Factor = 1.0;	for (k = 0; k < NumPlaces; k++)		Factor = Factor * 10.0; 	Temp = Value * Factor + 0.5;	return Temp / Factor;}`
To copy to clipboard, switch view to plain text mode
because it allows me to specifiy the number of roundoff places.

4. Re: qRound problem!

Hi thanks both. Finally what I've done is:
Qt Code:
`int roundUp(double v)int aux = (int) v;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 09:30.

5. Re: qRound problem!

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

6. Re: qRound problem!

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

7. 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. Guru
Join Date
Jan 2006
Location
Warsaw, Poland
Posts
5,372
Thanks
28
Thanked 976 Times in 912 Posts
Qt products
Platforms

Re: qRound problem!

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. Re: qRound problem!

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 17:13.

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.