c# - 此 VB6 操作产生问题的等效 C# 语句

标签 c# vb.net vb6 interop backwards-compatibility

我在 VB 中有这样的代码行:

Dim Sqrt As Double
Sqrt = Radius ^ 2 - (CenterX - X) ^ 2

上面语句中的参数传递的值如下:

X=  -7.3725025845036161 Double
CenterX =0.0            Double
Radius= 8.0             Double

在执行上面的语句时,Sqrt 的值如下:

Sqrt    9.646205641487505   Double

现在我使用 Math 类编写了类似的 C# 逻辑:

double Sqrt = 0;
Sqrt = Math.Pow(Radius, 2) - Math.Pow((CenterX - X), 2);

使用相同的一组值,C# 代码的输出为:

Sqrt    9.6462056414874979  double

由于 C# 代码中的这一单一更改,我需要帮助,我的所有值都受到影响。我能做些什么来获得与 *VB* 源代码相似的值吗?

最佳答案

有一个difference in the precision between the VB6 and the .NET double type .两者都是 IEEE 64 位 double 类型,但 .NET CLR 在内部使用 80 位扩展精度,即您的计算在 .NET 中会更准确。

如果您必须向后兼容 VB6 精度,您可以强制 FPU(浮点单元)使用(精度较低的)64 位值。这可以使用 native _controlfp_s 来实现功能。

下面是一个代码片段,您可以使用它暂时“降级”浮点精度以实现向后兼容性。你可以像这样使用它:

用法

// default floating point precision 

using (new FloatingPoint64BitPrecision())
{
    // floating-point precision is set to 64 bit
}

// floating-point precision is reset to default

代码片段

/// <summary>
/// This class changes floating-point precision to 64 bit
/// </summary>
internal class FloatingPoint64BitPrecision : IDisposable
{
    private readonly bool _resetRequired;

    public FloatingPoint64BitPrecision()
    {
        int fpFlags;
        var errno = SafeNativeMethods._controlfp_s(out fpFlags, 0, 0);
        if (errno != 0)
        {
            throw new Win32Exception(
                errno, "Unable to retrieve floating-point control flag.");
        }

        if ((fpFlags & SafeNativeMethods._MCW_PC) != SafeNativeMethods._PC_64)
        {
            Trace.WriteLine("Change floating-point precision to 64 bit");
            errno = SafeNativeMethods._controlfp_s(
                out fpFlags, SafeNativeMethods._PC_64, SafeNativeMethods._MCW_PC);

            if (errno != 0)
            {
                throw new Win32Exception(
                    errno, "Unable to change floating-point precision to 64 bit.");
            }

            _resetRequired = true;
        }
    }

    public void Dispose()
    {
        if (_resetRequired)
        {
            Trace.WriteLine("Resetting floating-point precision to default");
            SafeNativeMethods._fpreset();
        }
    }
}

internal static class SafeNativeMethods
{
    [DllImport("msvcr120.dll")]
    public static extern void _fpreset();

    [DllImport("msvcr120.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int _controlfp_s(
        out int currentControl, int newControl, int mask);

    public static int _CW_DEFAULT = 
        (_RC_NEAR | _PC_53 | _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW 
        | _EM_UNDERFLOW | _EM_INEXACT | _EM_DENORMAL);

    public const int _MCW_EM = 0x0008001f;          // interrupt Exception Masks 
    public const int _EM_INEXACT = 0x00000001;      //   inexact (precision) 
    public const int _EM_UNDERFLOW = 0x00000002;    //   underflow 
    public const int _EM_OVERFLOW = 0x00000004;     //   overflow 
    public const int _EM_ZERODIVIDE = 0x00000008;   //   zero divide 
    public const int _EM_INVALID = 0x00000010;      //   invalid 
    public const int _EM_DENORMAL = 0x00080000;     // denormal exception mask 
                                                    // (_control87 only) 

    public const int _MCW_RC = 0x00000300;          // Rounding Control 
    public const int _RC_NEAR = 0x00000000;         //   near 
    public const int _RC_DOWN = 0x00000100;         //   down 
    public const int _RC_UP = 0x00000200;           //   up 
    public const int _RC_CHOP = 0x00000300;         //   chop 

    public const int _MCW_PC = 0x00030000;          // Precision Control 
    public const int _PC_64 = 0x00000000;           //    64 bits 
    public const int _PC_53 = 0x00010000;           //    53 bits 
    public const int _PC_24 = 0x00020000;           //    24 bits 

    public const int _MCW_IC = 0x00040000;          // Infinity Control 
    public const int _IC_AFFINE = 0x00040000;       //   affine 
    public const int _IC_PROJECTIVE = 0x00000000;   //   projective 
}

关于c# - 此 VB6 操作产生问题的等效 C# 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39740935/

相关文章:

vb.net - 如何在长文本中查找所有出现的特定字符串

mysql - 连接vb6到mysql :[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

c# - 按类型选择时,NEST错误

c# - 使用 C++/CLI 而不是 C# 来包装(第三方)C 库 (dll) 有什么优势?

c# - 如何将 EntityCollection 属性分配给另一个属性?

mysql - 从两个表中检索数据时如何填充文本框?

vb6 - 使用记录集填充 VB6 中的 DBCOMBO

ms-access - 在 VB6 中使用查询来使用 MS Access VBA 函数

c# - 设置 xaml 代码 ItemsSource ="{Binding}"代码隐藏

c# - 如何在代码中重新排序 ToolStrip 项