gpt4 book ai didi

linux - 为什么我的数值算法在两台不同的机器上表现不同?

转载 作者:太空宇宙 更新时间:2023-11-04 11:27:16 25 4
gpt4 key购买 nike

我正在 Fortran 90 中编写用于多维优化的 BFGS 算法版本。我已经编写了代码,并在我的笔记本电脑(运行 Windows Vista)上使用 gfortran 对其进行了编译,并且运行良好。但是当我尝试在运行 Linux 6 的服务器上使用相同的代码时,它给出了错误的结果。它是完全相同的代码并且编译得很好,它产生的结果是错误的。也就是说,它似乎在算法的某个早期阶段出现了浮点算术错误,并且在开始生成 NaN 之前只生成一行数值结果。我只能假设它与服务器环境有关,但我如何找出它是什么以及如何相应地更正我的代码?

最佳答案

问题在此处描述 (CERT.org): FLP00-C. Understand the limitations of floating point numbers

The reason for this behavior is that Linux uses the internal extended precision mode of the x87 floating-point unit (FPU) on IA-32 machines for increased accuracy during computation. When the result is stored into memory by the assignment to c, the FPU automatically rounds the result to fit into a double. The value read back from memory now compares unequally to the internal representation, which has extended precision. Windows does not use the extended precision mode, so all computation is done with double precision, and there are no differences in precision between values stored in memory and those internal to the FPU. For GCC, compiling at optimization level 1 or higher eliminates the unnecessary store into memory, so all computation happens within the FPU with extended precision

解决方案描述如下: FLP02-C. Avoid using floating point numbers when precise computation is needed 有两个示例,一个显示问题的错误示例,以及一个没有问题的更正示例:

"This code may be fixed by replacing the floating-point numbers with integers for the internal additions. Floats are used only when printing results and when doing the division to compute the mean."

关于linux - 为什么我的数值算法在两台不同的机器上表现不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13872325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com