Re:Fast midpoint between two integers without overflow
by eloff on Wednesday December 07, 2022 @08:31AM
(#33894112)
It's mathematically correct, but integers use truncating (floor) division. So you can lose a fractional 0.5 from both a/2 and b/2, which would have added up to 1, and now your result is off by one from (a+b)/2.
In general, even with floating point numbers you can get different results by rounding implicitly or intentionally the intermediate values versus the end result.
In general, even with floating point numbers you can get different results by rounding implicitly or intentionally the intermediate values versus the end result.