round() function rounds off .5 values
-
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! -
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 50const 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 -
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? -
@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 -
The problem is explained in detail here:
https://stackoverflow.com/questions/46389221/c-round49-5-returns-49-when-called-with-variable-in-qt/