Visual Studio 2010 的精度问题

发布于 2024-12-21 12:10:21 字数 756 浏览 0 评论 0原文

我有一个用 Microsoft Visual C++ 6.0 编写的应用程序。现在我已经使用 C# 在 Visual Studio 2010 中重写了应用程序,但由于精度问题,结果不匹配。此类精度问题之一如下。

float a = 1.0f;

float b = 3.0f;

float c = a / b;

这是在 Visual Studio 2010 中运行时的 C# 代码,给出 c value = 0.333333343

但在 Visual C++ 6.0 上运行时,相同的代码,在值定义中的值后面删除 f给出 c 值 = 0.333333

任何人都可以整理并解释如何在 Visual Studio 和 Visual C++ 6.0 中使 c 具有相同的值吗?


实际上这些值是从监视窗口获取的。我发现不同版本的 Visual Studio 在浮点格式表示上可能有所不同。因此,watch 中的值可能没有用。这就是为什么我在两个 Visual Studio 版本中都打印了这些值的原因,结果如下。 使用 Visual C++ 语言的 Visual Studio 6.0 为 0.333333(六个 3),

但使用 C# 语言的 Visual Studio 10 为 0.3333333(七个 3)

那么任何人都可以帮助我使我的 C# 程序产生与 Visual C++ 相同的结果吗? ? (即我如何进行浮动操作以在两个版本上产生相同的结果???)

I have an application written in Microsoft Visual C++ 6.0. Now I have rewritten the application in Visual Studio 2010 using C#, but the results are not matching because of precision problems. One of such precision issues is the following one.

float a = 1.0f;

float b = 3.0f;

float c = a / b;

This is C# code when run in Visual studio 2010 gives c value = 0.333333343

But the same code, removing f after the value in the value definition, when run on Visual C++ 6.0 gives c value = 0.333333.

Can anybody sort it out and explain the way to have the same value for c in visual Studio as well as in Visual C++ 6.0??


Actually the values are taken from the watch window. I came to know that different versions of visual studio may differ in floating point format representation. Hence the values in watch may not be useful. This is the reason why I have printed the values in both visual studio versions and the results are as follows.
with visual studio 6.0 using visual c++ language it is 0.333333(six 3's)

but with visual studio 10 using C# language it is 0.3333333(seven 3's)

So can anybody help me to make my C# program to produce the same result as visual C++???
(i.e how can i make floating operations to produce the same results on both the versions???)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心房敞 2024-12-28 12:10:21

鉴于精确值是 0.3 重复出现,它们都不是“正确的” - 如果您试图匹配二进制浮点计算的精确结果,这通常不是一个好主意,因为他们的工作方式。 (有关更多信息,请参阅我关于.NET 中的二进制浮点的文章。

) em>可能您一开始就不应该使用二进制浮点数(例如,如果您的值代表精确的、人为的金额,例如金钱)。或者,您可能应该只使用特定容差进行相等比较。

C# 和 C 也可能生成完全相同的位模式 - 但由于这些值的格式化方式,您会看到不同的结果。同样,我不会使用数字的文本表示形式进行比较。

Given that the exact value is 0.3 recurring, neither of them is "correct" - and if you're trying to match exact results of binary floating point calculations, that's generally a bad idea to start with due to the way they work. (See my article on binary floating point in .NET for some more information.)

It's possible that you shouldn't be using binary floating point in the first place (e.g. if your values represent exact, artificial amounts such as money). Alternatively, it's possible that you should only be doing equality comparisons with a particular tolerance.

It's also possible that C# and C are producing the exact same bit pattern - but you're seeing different results because of how those values are being formatted. Again, I wouldn't use the text representation of numbers for comparisons.

半暖夏伤 2024-12-28 12:10:21

C# 只是显示较少的小数位。 0.333333343 四舍五入到六位有效数字为 0.333333c 的底层值是相同的。

当然,如果您想要更高的精度,您可以随时使用 double 变量。

C# is simply displaying fewer decimal places. 0.333333343 rounded to six significant figures is 0.333333. The underlying value of c is the same.

Of course, if you want more precision, you can always use double variables.

幼儿园老大 2024-12-28 12:10:21

C# 浮点可以通过 floatdouble 类型来处理。 float 类型的精度为 7 位,double 类型的精度为 16 位。

我相信 C++ 标准精度约为 16 位(平均 15.9!)。

无论如何,这两种表示形式在算术上都不正确,因为 1/3 是 0.333 的循环。

我认为这只是您所看到的值的表示形式(请注意,调试器会将值转换为字符串以供显示。如果您检查每个值的内存位置,您可能会发现这些值是相同的。

C# floating point can be handled by float and double types. A float type has a precision of 7 digits and a double 16.

I believe that the C++ standard precision is around 16 digits (15.9 on average!).

In any case neither representation is arithmetically correct as 1/3 is 0.333 recurring.

I think that it's merely the representation of the value that you are seeing (be aware that the debugger will convert the value to a string for display. If you check the memory locations for each you'll probably find that the values are the same.

烙印 2024-12-28 12:10:21

快速检查后发现,是的,这些数字是完全相同的。所以你的问题的答案是,当你想要相同的输出时,你需要做的就是确保显示方法兼容。
例如,C 中的 printf("%9.7f", result) 和 C# 中的 string.Format("{0:0.0000000}", result)。就这样。

A quick check reveals that yes, the numbers are exactly the same. So the answer to your question is, when you want the same output, all you need to do is make sure the display methods are compatible.
For instance, printf("%9.7f", result) in C and string.Format("{0:0.0000000}", result) in C#. That's all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文