When a floating point value is converted to an integer, it's truncated. That means that all digits after the decimal point is simply removed. E.g. 12.34
becomes 12
, and 12.99
also becomes 12
. There is no rounding done.
That is what the + 0.5
part does in the initializations. It's simply a way to convert a floating point value to integer with rounding. For example, 0.0299 * 128
is 3.8272
which truncated would be 3
. But with the + 0.5
it's 4.3272
which truncated becomes 4
, which is the rounded value of 3.8272
.
There may however still be problems, and you might want to read What Every Computer Scientist Should Know About Floating-Point Arithmetic to learn more.