C#舍入MidpointRounding.ToEven与MidpointRounding.AwayFromZero [英] C# Rounding MidpointRounding.ToEven vs MidpointRounding.AwayFromZero

查看:1499
本文介绍了C#舍入MidpointRounding.ToEven与MidpointRounding.AwayFromZero的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,两个小数舍入策略MidpointRounding.ToEvenMidpointRounding.AwayFromZero的准确性是否存在差异?我的意思是,这两者都可以确保四舍五入后的数字之间的平均分配,还是一个舍入策略可以更好地代表四舍五入的数字呢?

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?

推荐答案

从MSDN:

默认情况下,Math.Round使用MidpointRounding.ToEven.大多数人不熟悉四舍五入"作为替代方法,在学校更常教授从零开始四舍五入". .NET默认使用四舍五入",因为它在统计上是优越的,因为它不倾向于四舍五入"而不是四舍五入"的趋势(假设四舍五入的数字趋于正数). )

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.)

根据数据集,对称算术舍入会引入较大的偏差,因为它总是将中点值向上舍入.举一个简单的例子,假设我们要确定三个值1.5、2.5和3.5的平均值,但是我们想先将它们四舍五入为最接近的整数,然后再计算它们的平均值.请注意,这些值的真实平均值是2.5.使用对称算术舍入,这些值更改为2、3和4,其平均值为3.使用银行家舍入,这些值更改为2、2和4,其平均值为2.67.由于后一种四舍五入方法更接近三个值的真实均值,因此它提供的数据丢失最少.

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

这篇关于C#舍入MidpointRounding.ToEven与MidpointRounding.AwayFromZero的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆