简体   繁体   English

从VB6和C#调用DLL在双精度中给出略微不同的结果

[英]Calling DLL From VB6 and C# Give Slightly Different Results in Double Precision

I have a proprietary library in a DLL (I don't have the code) that has been used for years from within VB6. 我有一个DLL中的专有库(我没有代码)已经在VB6中使用了多年。 I'm trying to upgrade the VB6 code to C#, and hope to make the C# code exactly replicate the VB6 behavior. 我正在尝试将VB6代码升级到C#,并希望使C#代码完全复制VB6行为。 I'm having trouble making the double precision results of some calculations done in the DLL match exactly when called from each environment. 我无法在从每个环境调用时精确地完成DLL中的某些计算的双精度结果。

In VB6 I have something like this (note file reading and writing is to make sure exact same values are used and generated): 在VB6中我有这样的东西(注意文件的读写是为了确保使用和生成完全相同的值):

Dim a As Double, b As Double, c As Double, d As Double
Open "C:\input.txt" For Binary As #1
Get #1, , a
Get #1, , b
Get #1, , c
Get #1, , d
Close #1
Dim t As New ProprietaryLib.Transform
t.FindLine a, b, c, d
Open "C:\output.txt" For Binary As #1
Put #1, , t.Slope
Put #1, , t.Intercept
Close #1

In C# I have something like this: 在C#我有这样的事情:

System.IO.BinaryReader br = new System.IO.BinaryReader(System.IO.File.Open(@"C:\input.txt", System.IO.FileMode.Open));
double a, b, c, d;
a = br.ReadDouble();
b = br.ReadDouble();
c = br.ReadDouble();
d = br.ReadDouble();
br.Close();
ProprietaryLib.Transform t = new ProprietaryLib.Transform();
t.FindLIne(a, b, c, d);
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(System.IO.File.Open(@"C:\output2.txt", System.IO.FileMode.Create));
bw.Write(t.Slope);
bw.Write(t.Intercept);
bw.Close();

I have verified that the input is being read identically (verified by re-writing binary values to files), so identical double precision numbers are being fed to the DLL. 我已经验证输入的读取方式相同(通过将二进制值重写到文件来验证),因此将相同的双精度数字输入到DLL中。 The output values are very similar, but not identical (values are sometimes off in the least significant parts of the numbers, out in the noise of the 15th-17 decimal place, and binary write out to file verifies that they are different binary values). 输出值非常相似,但不相同(值有时在数字的最低有效部分关闭,在15-17小数点的噪声中,二进制写入文件验证它们是不同的二进制值) 。 Does anyone have any advice on why these values might be calculated not quite identically or how I might fix or debug this? 有没有人对为什么这些值的计算方法不完全相同或者我如何修复或调试它有任何建议?

This probably happens because of the different standards used for double precision 这可能是因为用于双精度的不同标准

  • VB6 uses a less precise internal standard by default for (back then) performance reasons. 默认情况下,VB6使用不太精确的内部标准(当时)性能原因。
  • .NET complies with the IEEE 754 standard for binary floating-point arithmetic .NET符合用于二进制浮点运算的IEEE 754标准

You can compile the VB6 application using the /OP option to improve float consistency. 您可以使用/OP选项编译VB6应用程序以提高浮点一致性。

By default, the compiler uses the coprocessor's 80-bit registers to hold the intermediate results of floating-point calculations. 默认情况下,编译器使用协处理器的80位寄存器来保存浮点计算的中间结果。 This increases program speed and decreases program size. 这会提高程序速度并减少程序大小。 However, because the calculation involves floating-point data types that are represented in memory by less than 80 bits, carrying the extra bits of precision (80 bits minus the number of bits in a smaller floating-point type) through a lengthy calculation can produce inconsistent results. 但是,由于计算涉及在内存中表示的浮点数据类型少于80位,因此通过冗长的计算可以产生额外的精度位(80位减去较小浮点类型中的位数)结果不一致。 (Source: MSDN ) (来源: MSDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM