为什么是 Round(Fix(x))? [英] Why Round(Fix(x))?

查看:71
本文介绍了为什么是 Round(Fix(x))?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft.VisualBasic 的 Reflector 摘录中,它引用了 Microsoft.VisualBasic.Conversion.Fix 在 3 个地方不包括 Fix(Object) 重载.

In the Reflector extract for Microsoft.VisualBasic it references Microsoft.VisualBasic.Conversion.Fix in 3 places not including the Fix(Object) overload.

在每种情况下都适用 Math.Round 到结果.(特别是在 DateAddDateDiff 中;Choose 中的第三次使用确实减去 1,并冗余地转换为 Round 之前再次 >Double.)

In each case it applies Math.Round to the result. (Especially in DateAdd and DateDiff; the third use in Choose does subtract 1, and redundantly cast to Double again before applying Round.)

什么时候可以 Math.Round(Conversion.Fix(x)) <>双x的Conversion.Fix(x)?

When can Math.Round(Conversion.Fix(x)) <> Conversion.Fix(x) for Double x?

(我会自己检查参考源,但我找不到可以下载的下载em> 包括 Microsoft.VisualBasic.) 参考源现在可用 在线.

推荐答案

然后将所有这些引用转换为 IntegerLong:

All of these references are then cast to Integer or Long:

CIntCLng 在它们对应的 IL conv.ovf.i4/8 之前显式调用 Math.RoundSingleDouble 转换时转换.

CInt and CLng explicitly call Math.Round before their corresponding IL conv.ovf.i4/8 cast when casting from Single and Double.

这会强制执行 Banker's Rounding,这是 VB.NET 已知的怪癖".

This enforces the Banker's Rounding that is a VB.NET known "quirk".

conv.ovf.i4 单独向零截断,这恰好与 Fix 相同(对于适合 Integerconv.ovf.i8 的长).

conv.ovf.i4 alone truncates towards zero, which happens to be the same as Fix (for the numbers that fit in an Integer, or a Long for conv.ovf.i8).

(当我最初输入问题时,我以为我已经检查过这不是原因.)

有一段时间,Microsoft 提供了参考源,因此我能够确认它们都已附上在 CIntCLng 中,Math.Round 由编译器添加.

For a while, Microsoft made the Reference Source available, so I was able to confirm they were all enclosed in CInt or CLng and the Math.Round is added by the compiler.

目前在上面链接中可用的 VB.NET源代码"现在只提供没有代码的参考程序集":-(

The VB.NET "source" currently available at the above link now only provides the "reference assembly" which has no code :-(

这篇关于为什么是 Round(Fix(x))?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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