使用 C++ 的 VS6 和 VS2008 之间的数值精度差异? [英] Numerical precision differences between VS6 and VS2008 using C++?

查看:22
本文介绍了使用 C++ 的 VS6 和 VS2008 之间的数值精度差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直致力于将一个遗留项目从 Visual Studio 6 移植到 2008.在跳过一些障碍之后,我现在可以构建和执行新项目.但是,我注意到两个版本的程序的输出略有不同,尽管代码相同,但浮点计算似乎并不等效.

I've been working on porting a legacy project from Visual Studio 6 to 2008. After jumping a few hurdles I now have the new project building and executing. However, I've noticed that the output from the two versions of the program are very slightly different, as though the floating-point calculations are not equivalent, despite the fact that the code is the same.

这些差异开始时通常很小 (<1.0E-6),但经过多次计算累积到它们开始对输出产生实质性影响的程度.作为一个例子,在计算的第一步之后,我查看了关键变量在内存中的精确双精度存储,并看到:

These differences usually start quite small (<1.0E-6) but accumulate over many calculations to the point where they start to have a material impact on the output. As one example, I looked at the exact double-precision storage in memory of a key variable after one of the first steps of the calculation and saw:

Visual Studio 6 表示:0x4197D6CC85AC68D9
十进制等效:99988257.4183687120676040649414

Visual Studio 6 representation: 0x4197D6CC85AC68D9
Decimal equivalent: 99988257.4183687120676040649414

Visual Studio 2008 表示:0x4197D6CC85AC68EB
十进制等效:99988257.4183689802885055541992

Visual Studio 2008 representation: 0x4197D6CC85AC68EB
Decimal Equivalent: 99988257.4183689802885055541992

我试图调试它以追踪差异的开始位置,但输出来自迭代数值求解器,因此以如此高的精度追踪这个过程将是一个耗时的过程.

I've tried to debug this to track down where differences start, but the output is from an iterative numerical solver, so it will be a time-consuming process to trace through this at such a high-level of precision.

有没有人知道两个版本的编译器的双精度算术运算之间的任何预期差异?(或者关于可能导致这种情况的任何其他想法?)

Is anyone aware of any expected differences between double-precision arithmetic operations of the two versions of the compiler? (Or any other ideas about what might be causing this?)

现在我的下一步可能是尝试创建一个简单的演示应用程序,以显示问题并且可以更轻松地检查.

For now my next step will probably be to try to create a simple demo app that shows the issue and can be more easily examined.

谢谢!

推荐答案

这只是一个猜测,但大多数现代 Intel/AMD CPU 都有两个独立的 FPU 模型:旧式 i386 FPU 和更新的基于 SSE/SSE2 的模型.后者具有更灵活的编程模型,通常是首选.

This is just a guess, but most modern Intel/AMD CPUs have two seperate FPU models: The old-style i386 FPU and the newer SSE/SSE2 based model. The latter is has a more flexible programming model and is usually preferred.

您应该检查一下,VS6 和 VS2008 是否都为同一模型生成代码,因为老式 FPU 具有 80 位中间结果,这可能会导致舍入较少并可能获得更好的结果,但实际结果取决于优化器可以.顺便说一句,这是科学界人士真正讨厌的东西.例如,如果操作数溢出到内存中,那么它们会被截断为 64 位,并且会丢失额外的精度.

You should check, if both VS6 and VS2008 generate code for the same model, because the old-school FPU has 80 bit intermediate results, which could lead to less rounding and potentially better results, but the actual results depend on what the optimizer does. Which is something that the science people really hate btw.. For example, if operands are spilled to memory, then they're truncated to 64 bit and the extra precision is lost.

IIRC 然后 VS6 无法生成 SSE/SSE2 代码,但它有一个/fp:precise 选项可以将所有中间结果四舍五入为其声明的大小.我认为 VS 2008 也有这个标志.所以我建议你对两个编译器都尝试/fp:precise 并再次比较结果.

IIRC then VS6 could not generate SSE/SSE2 code, but it had a /fp:precise option to round all intermediate results to their declared size. VS 2008 has this flag too, I think. So I'd suggest that you try /fp:precise for both compilers and compare the result again.

这篇关于使用 C++ 的 VS6 和 VS2008 之间的数值精度差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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