这个VB6操作的等效C#语句正在出现问题。

21

我在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*源代码相似的值?


4
这是VB6还是VB.NET?我无法在VB.NET (.NET 4.5)中复现,两次得到相同的结果(9.6462056414874979)。 - Dirk Vollmar
4
以下是有关VB6和.NET之间不同数据表示的背景介绍:https://dev59.com/yWLVa4cB1Zd3GeqPyqxI。 - FloatingKiwi
2
就像@FloatingKiwi所说,这只是VB6和.Net之间的差异。 .Net代码更准确,所以不要担心这个问题。(如果差异给您带来了问题,那么您一定做错了其他事情...因为工程/科学计算通常不应该担心这样微小的差异) - Matthew Watson
6
更正:工程/科学计算通常不应使用Visual Basic - cat
@MatthewWhited 这是来自 Excel 的答案,如果你使用 VBA,那么结果是 9.6462056414875。基本上是一样的。 - Brad
显示剩余3条评论
2个回答

32

在VB6和.NET双精度类型之间存在精度差异。两者都是IEEE 64位双精度类型,但.NET CLR在内部使用80位扩展精度,即在.NET中计算的结果更加准确。

如果您需要与VB6精度保持向后兼容,则可以强制FPU(浮点数单元)使用(不太准确的)64位值。这可以使用本地_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 
}

这会降级所有使用了 double 的地方吗? - Apoorv
您还可以通过将FloatingPoint64BitPrecision构造函数中的代码放置到应用程序启动路由中来全局降级。 - Dirk Vollmar
我需要将Sqrt根函数放在using()语句中吗? - Apoorv
让我查一下然后回复您,先生。感谢您的帮助。 - Apoorv
这个实现会导致我的软件冻结 :( - Apoorv
显示剩余2条评论

5

不需要使用Math类,只需按照以下方式编写计算:

sqrt = Radius * Radius - (CenterX - x) * (CenterX - x);

这个问题在于 CenterX - X 被计算了两次。对于这个问题,由于只有一次计算,所以可能不会有影响,但是如果这个计算更加复杂的话,为了提高性能,最好将这个计算提取到单独的一行中,这样就不会重复发生。同样,这里没有问题,因为减法不是很耗费资源,但如果是某些耗费资源的操作,最好将其拆分出来。 - Nzall
3
@Nzall:实际上,在这种情况下,编译器应该能够优化表达式,以便CenterX - X只计算一次。不过,对于更加复杂的表达式,你的建议仍然适用——即使这可能只是为了提高可读性。 - hoffmale
1
避免使用Math类也能解决Dirk所指出的精确性问题吗? - GER
@GER:不,无论是否使用 Math.Pow,精度问题仍然存在。 - Dirk Vollmar
通过对这个计算式进行因式分解:double res2 = (Radius - (CenterX - x)) * (Radius + (CenterX - x));,我得到的结果是:9.6462056414875015。 - romulus001

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接