Reputation: 113
I'm looking at the MSDN documentation for System::Convert::ToUInt32(Double), and the return value is stated "value rounded to the nearest 32-bit unsigned integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6." This seems like a really odd way to round numbers, and actually seems to make the function unsuitable for most situations I would want to use it in. Any reason it should be this way?
Upvotes: 0
Views: 43
Reputation: 2788
The question does not seem to be related to C++ programming language (despite the tags), but about C#.
This is the up-to-date official documentation for Convert.Uint32(double), that implements what is commonly known as "round half to even" or "bankers rounding", see reference on wikipedia.
Rounding rules for floating point numbers (such as double) are described by the IEEE 754 standard (wikipedia reference here). Microsoft just followed those rules.
As an addition, the "bankers rounding" does not suffer from negative or positive bias as much as the "round half away from zero" method, at least over most reasonable distributions. Thus it is considered a "better" rounding method.
Upvotes: 2