为什么是 Round(Fix(x))? [英] Why 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
到结果.(特别是在 DateAdd
和 DateDiff
中;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
.)
推荐答案
然后将所有这些引用转换为 Integer
或 Long
:
All of these references are then cast to Integer
or Long
:
CInt
和 CLng
在它们对应的 IL conv.ovf.i4/8
之前显式调用 Math.Round
从 Single
和 Double
转换时转换.
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
相同(对于适合 Integer
或 conv.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 提供了参考源,因此我能够确认它们都已附上在 CInt
或 CLng
中,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屋!