Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Rounding MidpointRounding.ToEven vs MidpointRounding.AwayFromZero

Tags:

In C# Is there any difference in the accuracy of the two decimal rounding strategies MidpointRounding.ToEven and MidpointRounding.AwayFromZero? I mean do both ensure an even distribution amongst the numbers that are rounded to, or is one rounding strategy over representing the rounded numbers compared to the other?

like image 567
Carlo V. Dango Avatar asked Sep 09 '11 10:09

Carlo V. Dango


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


1 Answers

From MSDN:

By default, Math.Round uses MidpointRounding.ToEven. Most people are not familiar with "rounding to even" as the alternative, "rounding away from zero" is more commonly taught in school. .NET defaults to "Rounding to even" as it is statistically superior because it doesn't share the tendency of "rounding away from zero" to round up slightly more often than it rounds down (assuming the numbers being rounded tend to be positive.)

Depending on the data set, symmetric arithmetic rounding can introduce a major bias, since it always rounds midpoint values upward. To take a simple example, suppose that we want to determine the mean of three values, 1.5, 2.5, and 3.5, but that we want to first round them to the nearest integer before calculating their mean. Note that the true mean of these values is is 2.5. Using symmetic arithmetic rounding, these values change to 2, 3, and 4, and their mean is 3. Using bankers rounding, these values change to 2, 2, and 4, and their mean is 2.67. Because the latter rounding method is much closer to the true mean of the three values, it provides the least loss of data.

http://msdn.microsoft.com/en-us/library/system.math.round.aspx

like image 156
Carlo V. Dango Avatar answered Oct 06 '22 01:10

Carlo V. Dango