调用DLL从VB6和C#提供的双精度稍有不同的结果 [英] Calling DLL From VB6 and C# Give Slightly Different Results in Double Precision

查看:228
本文介绍了调用DLL从VB6和C#提供的双精度稍有不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DLL(我没有的代码),已使用了多年从VB6中的专有文库。我想升级VB6代码到C#,并希望使C#代码完全复制VB6的行为。我无法使得从每个环境称为什么时候该DLL的比赛做了一些计算的双精度结果。

I have a proprietary library in a DLL (I don't have the code) that has been used for years from within VB6. I'm trying to upgrade the VB6 code to C#, and hope to make the C# code exactly replicate the VB6 behavior. I'm having trouble making the double precision results of some calculations done in the DLL match exactly when called from each environment.

在VB6我有这样的事情(注文件阅读和写作是要确保完全一样的价值观的使用和产生的):

In VB6 I have something like this (note file reading and writing is to make sure exact same values are used and generated):

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

在C#我有这样的事情:

In C# I have something like this:

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();



我已经验证,输入被相同的读取(通过验证重新写入二进制值文件) ,所以相同的双精度数被馈送到该DLL。输出值非常相似,但不完全相同(值有时关中的数字的至少显著份,这些在第15 -17小数位的噪声,和二进制写到文件验证它们是不同的二进制值) 。有没有人对为什么这些值可能会不太相同计算的任何意见或我如何可能会解决或调试呢?

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. 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). 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默认使用不太精确的内部标准(当时)的性能的原因,不同的标准。

  • .NET与二进制浮点运算的IEEE 754标准要求

您可以编译使用 / OP 选项来提高浮点一致性VB6应用程序。

You can compile the VB6 application using the /OP option to improve float consistency.

通过默认情况下,编译器使用协处理器的80位寄存器来保存浮点计算的中间结果。这增加了程序的速度并减少程序的大小。然而,因为计算通过冗长的计算涉及在内存由小于80位表示浮点数据类型,承载的精度的额外位(80位减去比特在一个较小的浮点类型的数目)可以产生不一致的结果。 (来源: MSDN

By default, the compiler uses the coprocessor’s 80-bit registers to hold the intermediate results of floating-point calculations. 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. (Source: MSDN)

这篇关于调用DLL从VB6和C#提供的双精度稍有不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆