PDA

View Full Version : qRound problem!

SkripT
19th January 2006, 19:24
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

jacek
19th January 2006, 21:37
It looks like that "up" shouldn't be there.

inline int qRound(qreal d)
{ return d >= 0.0 ? int(d + 0.5) : int(d - int(d-1) + 0.5) + int(d-1); }

GreyGeek
19th January 2006, 21:43
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:

round(x) = floor( x + 0.5f);

I borrowed this method from one of the many offerings:

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;
}

because it allows me to specifiy the number of roundoff places.

SkripT
20th January 2006, 09:25
Hi thanks both. Finally what I've done is:

int roundUp(double v)
int aux = (int) v;
return v - aux > 0 ? aux + 1 : aux;

yop
20th January 2006, 10:07
If I understand correctly you are looking for http://www.cppreference.com/stdmath/ceil.html

dimitri
21st January 2006, 10:07
This is indeed a bug either in the documentation or the Qt code. It will be fixed.

wysota
21st January 2006, 10:58
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 :)

jacek
21st January 2006, 14:30
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.

wysota
21st January 2006, 17:10
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 :)