签名零 Linux 与 Windows
我正在 Windows 和 Linux 上运行 C++ 程序。 输出应该是相同的。 我试图确保唯一的差异是真正的差异,而不是工作环境差异。 到目前为止,我已经处理了可能由 \r\n 差异引起的所有差异 但有一件事我似乎无法弄清楚。
在windows中输出是0.000,在linux中是-0.000
有人知道是什么造成了差异吗?
谢谢
i am running a program in c++ on windows and on linux.
the output is meant to be identical.
i am trying to make sure that the only differences are real differences oppose to working inviorment differences.
so far i have taken care of all the differences that can be caused by \r\n differences
but there is one thing that i can't seem to figure out.
in the windows out put there is a 0.000 and in linux it is -0.000
does any one know what can it be that is making the difference?
thanx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能是由于优化器优化某些 FP 计算的方式不同(可以配置 - 请参阅例如 此处);在一种情况下,您得到的值略小于 0,而在另一种情况下,您得到的值略高。输出中的两者均四舍五入为
0.000
,但它们保留其“真实”符号。Probably it comes from differences in how the optimizer optimizes some FP calculations (that can be configurable - see e.g. here); in one case you get a value slightly less than 0, in the other slightly more. Both in output are rounded to a
0.000
, but they keep their "real" sign.由于在 IEEE 浮点格式中,符号位与值是分开的,因此有两个不同的 0 值:正值和负值。在大多数情况下,这没有什么区别;两个零比较相等,并且它们确实描述相同的数学值(从数学上讲,0 和 -0 是相同的)。当您出现下溢并且需要知道下溢是由正值还是负值发生时,差异可能会很显着。另外,如果除以 0,得到的无穷大的符号取决于 0 的符号(即 1/+0.0 给出 +Inf,但 1/-0.0 给出 -Inf)。换句话说,它很可能不会对你产生任何影响。
但请注意,不同的输出并不一定意味着数字本身不同。 Windows 中的值很可能也是 -0.0,但 Windows 上的输出例程不区分 +0.0 和 -0.0(毕竟它们比较相等)。
Since in the IEEE floating point format the sign bit is separate from the value, you have two different values of 0, a positive and a negative one. In most cases it doesn't make a difference; both zeros will compare equal, and they indeed describe the same mathematical value (mathematically, 0 and -0 are the same). Where the difference can be significant is when you have underflow and need to know whether the underflow occurred from a positive or from a negative value. Also if you divide by 0, the sign of the infinity you get depends on the sign of the 0 (i.e. 1/+0.0 give +Inf, but 1/-0.0 gives -Inf). In other words, most probably it won't make a difference for you.
Note however that the different output does not necessarily mean that the number itself is different. It could well be that the value in Windows is also -0.0, but the output routine on Windows doesn't distinguish between +0.0 and -0.0 (they compare equal, after all).
除非使用像
-ffast-math
这样的(不安全)标志,否则编译器在“优化”IEEE-754 算法时可以做出的假设受到限制。首先检查两个平台是否使用相同的舍入。另外,如果可能的话,检查它们是否使用相同的浮点单元。即 x86 上的 SSE 与 FPU。后者可能是数学库函数实现的问题 - 特别是三角函数/超越函数。
Unless using (unsafe) flags like
-ffast-math
, the compiler is limited in the assumptions it can make when 'optimizing' IEEE-754 arithmetic. First check that both platforms are using the same rounding.Also, if possible, check they are using the same floating-point unit. i.e., SSE vs FPU on x86. The latter might be an issue with math library function implementations - especially trigonometric / transcendental functions.