0

I saw this part in some Programming Language Book:

Understanding the default rounding rules In British primary schools

for children aged 5 to 11, pupils are taught to round up if the decimal part is .5 or higher and round down if the decimal part is less. Let's explore if C# follows the same primary school rule:

  1. At the bottom of the Main method, add statements to declare and assign an array of double values, convert each of them to an integer, and then write the result to the console, as shown in the following code:
double[] doubles = new[] 
 { 9.49, 9.5, 9.51, 10.49, 10.5, 10.51 };
foreach (double n in doubles)
{
 WriteLine($"ToInt({n}) is {ToInt32(n)}");
}
  1. Run the console application and view the result, as shown in the following output:
ToInt(9.49) is 9
ToInt(9.5) is 10
ToInt(9.51) is 10
ToInt(10.49) is 10
ToInt(10.5) is 10
ToInt(10.51) is 11

We have shown that the rule for rounding in C# is subtly different from the primary school rule: • It always rounds down if the decimal part is less than the midpoint .5. • It always rounds up if the decimal part is more than the midpoint .5. • It will round up if the decimal part is the midpoint .5 and the non-decimal part is odd, but it will round down if the non-decimal part is even.

This rule is known as Banker's Rounding, and it is preferred because it reduces bias by alternating when it rounds up or down. Sadly, other languages such as JavaScript use the primary school rule...

I haven't found what the following section means:

and it is preferred because it reduces bias by alternating when it rounds up or down

Would anybody explain it to me?

1
  • Do you know the meaning of "bias" and "alternating"?
    – gotube
    Feb 6, 2022 at 8:23

2 Answers 2

1

Bias is the property (of a system, of a computer program, of a person) to favor one thing over another. Really the word means an imbalance. It is not necessarily a bad thing; the materials that make up computer chips must be properly biased in order to work, for example.

In this case the "primary school" method is claimed to be biased because the value x.5, which is precisely the midpoint between x and x+1, is always rounded up. Over a large data set this can increase the overall values and drive the average higher than it really is. "Banker's rounding" alternates whether x.5 is rounded up or down, which causes this bias to go away—the average should not be artificially increased.

1

and it is preferred because it reduces bias by alternating when it rounds up or down

bias in the above fragment takes the following meaning in Cambridge Dictionary.

the fact that information is not correct because of the method used in collecting or presenting it:

There is a need to build in safeguards against statistical bias.

As a 0.5 increment from an integer leads to the mid point between that integer and the next, consistent rounding up would lead to inaccuracies in subsequent results.

it reduces bias by alternating means this method alternates between rounding up and rounding down and hence reduces inaccuracies in subsequent results.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .