gpt4 book ai didi

.net - 在 .NET 中模拟 VBA 算法

转载 作者:行者123 更新时间:2023-12-04 21:03:23 27 4
gpt4 key购买 nike

免责声明:我知道 0.025 不能在 IEEE 浮点变量中精确表示,因此,舍入可能不会返回预期的结果。那不是我的问题!

是否可以在 .NET 中模拟 VBA 算术运算符的行为?

例如,在 VBA 中,以下表达式产生 3 :

Dim myInt32 As Long
myInt32 = CLng(0.025 * 100) ' yields 3

然而,在 VB.NET 中,下面的表达式产生 2 :
Dim myInt32 As Integer
myInt32 = CInt(0.025 * 100) ' yields 2

根据规范,两者都应该返回相同的值:
  • Long (VBA) 和 Integer (VB.NET) 是 32 位整数类型。
  • 根据the VBA specification , CLng 对 Long 执行 Let-coercion,数字类型之间的 Let-coercion 使用 Banker 的舍入。 The same is true对于 VB.NET 的 CInt。
  • 0.025在这两种情况下都是 double IEEE 浮点常数。

  • 因此,浮点乘法运算符或整数转换运算符的某些实现细节发生了变化。但是,出于与旧版 VBA 系统兼容的原因,我需要在 .NET 应用程序中复制 VBA 的数学行为(无论它可能是错误的)。

    有没有办法做到这一点?有人写了 Microsoft.VBA.Math图书馆?或者是否在某处记录了精确的 VBA 算法,以便我自己做?

    最佳答案

    VBA 和 VB.NET 的行为不同,因为 VBA 使用 80-bit "extended" precision用于中间浮点计算(即使 Double 是 64 位类型),而 VB.NET 始终使用 64 位精度。使用80位精度时,0.025 * 100的值略大于2.5,所以CLng(0.025 * 100)四舍五入到 3。

    不幸的是,VB.NET 似乎不提供 80 位精度的算术。作为一种解决方法,您可以使用 Visual C++ 创建 native Win32 DLL 并通过 P/Invoke 调用它。例如:

    #include <cmath>
    #include <float.h>

    #pragma comment(linker, "/EXPORT:MultiplyAndRound=_MultiplyAndRound@16")

    extern "C" __int64 __stdcall MultiplyAndRound(double x, double y)
    {
    unsigned int cw = _controlfp(0, 0);
    _controlfp(_PC_64, _MCW_PC); // use 80-bit precision (64-bit significand)
    double result = floor(x * y + 0.5);
    if (result - (x * y + 0.5) == 0 && fmod(result, 2))
    result -= 1.0; // round down to even if halfway between even and odd
    _controlfp(cw, _MCW_PC); // restore original precision
    return (__int64)result;
    }

    在 VB.NET 中:
    Declare Function MultiplyAndRound Lib "FPLib.dll" (ByVal x As Double, ByVal y As Double) As Long

    Console.WriteLine(MultiplyAndRound(2.5, 1)) ' 2
    Console.WriteLine(MultiplyAndRound(0.25, 10)) ' 2
    Console.WriteLine(MultiplyAndRound(0.025, 100)) ' 3
    Console.WriteLine(MultiplyAndRound(0.0025, 1000)) ' 3

    关于.net - 在 .NET 中模拟 VBA 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31139739/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com