c# – 强制执行特定小数精度级别的单元测试函数
发布时间:2020-12-15 23:33:45 所属栏目:百科 来源:网络整理
导读:我正在编写计算优惠多席选举的软件.一个常见的要求是固定精度.这意味着必须对具有固定指定精度的值执行所有数学运算,并且结果必须具有相同的精度.固定精度表示小数点后的设定位数.之后的任何数字都将被丢弃. 因此,如果我们假设5位数的精度: 42/139 变为: 4
我正在编写计算优惠多席选举的软件.一个常见的要求是固定精度.这意味着必须对具有固定指定精度的值执行所有数学运算,并且结果必须具有相同的精度.固定精度表示小数点后的设定位数.之后的任何数字都将被丢弃.
因此,如果我们假设5位数的精度: 42/139 变为: 42.00000/139.00000 = 0.30215 我在为此编写单元测试时遇到问题.到目前为止,我已经为大小数字写了这两个测试. public void TestPrecisionBig() { PRECISION = 5; decimal d = Precision(1987.7845263487169386183643876m); Assert.That(d == 1987.78452m); } public void TestPrecisionSmall() { PRECISION = 5; decimal d = Precision(42); Assert.That(d == 42.00000m); } 但它评估为42 == 42.00000m 我该如何测试?我想我可以做一个d.ToString,但这是一个很好的“正确”测试吗? 编辑:我被要求显示我的Precision方法的实现.它不是很优雅,但它有效. public static decimal Precision(decimal d) { if (d == 0) return 0.00000m; decimal output = Math.Round(d,6); string s = output.ToString(CurrentCulture); char c = char.Parse(CurrentCulture.NumberFormat.NumberDecimalSeparator); if (s.Contains(c)) { output = decimal.Parse(s.Substring(0,s.Length - 1)); return output; } s += c; for (int i = 0; i <= Constants.PRECISION; i++) s += '0'; output = decimal.Parse(s.Substring(0,s.IndexOf(c) + Constants.PRECISION + 1)); return output; } 现在我可能会看到我是否不能直接设置指数. 编辑2:新的bit-jugling精度方法 public static decimal Precision(decimal d) { if (d == 0) return 0.00000m; string exponent = System.Convert.ToString(Constants.PRECISION,2); exponent = exponent.PadLeft(8,'0'); int positive = Convert.ToInt32("00000000" + exponent + "0000000000000000",2); int negative = Convert.ToInt32("10000000" + exponent + "0000000000000000",2); int preScaler = (int)Math.Pow(10,Constants.PRECISION); d *= preScaler; d = decimal.Truncate(d); int[] bits = decimal.GetBits(d); bits[3] = (bits[3] & 0x80000000) == 0 ? positive : negative; return new decimal(bits); } 解决方法
您可以使用此函数来确定小数的精度:
public int GetPrecision(decimal d) { return (Decimal.GetBits(d)[3] >> 16) & 0x000000FF; // bits 16-23 } 那么你的测试将是这样的: public void TestPrecisionSmall() { PRECISION = 5; decimal d = Precision(42); Assert.That(GetPrecision(d) == PRECISION); // or >= if that's more appropriate } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |