roundToInt() rounding .5 down


#1

Hi, roundToInt() is rounding .5 values down whereas I would expect them to be going up. I’m pretty sure this used to be correct.

thx


#2

Confirmed:

roundToInt(0.5f) == 0
(int)round(0.5f) == 1


#3
/** Fast floating-point-to-integer conversion.

    This is faster than using the normal c++ cast to convert a float to an int, and
    it will round the value to the nearest integer, rather than rounding it down
    like the normal cast does.

    Note that this routine gets its speed at the expense of some accuracy, and when
    rounding values whose floating point component is exactly 0.5, odd numbers and
    even numbers will be rounded up or down differently.
*/

#4

ok, thx for the clarity - the only reason I raised is that a recent pull seems to have changed the behaviour.


#5

I don’t remember seeing any changes to it for a long time, but it’s always been a “fast-and-dirty” converter, not a function that you should use if you care about exact values.


#6

understood, thx


#7

I just checked the git log for this file, it was a change from 9 January this year (develop, I did not check master)


#8

I see rather 3 years for that function, doing git blame:


#9

Sorry, my mistake, I interpreted the diff incorrectly :frowning: