等效表达式可以产生不同的浮点结果吗?
对这个答案的讨论让我思考浮点数的相等和等价。我知道浮点数并不总是能够准确表示。问题是,使用浮点运算时是否存在数学上等效的表达式会产生不同的结果?你能举个例子吗?
编辑:让我说得更清楚一些。我知道相同的代码使用不同的编译器或不同的机器可能会返回不同的结果。我正在寻找的是两个数学上等价的表达式,我可以在我的Python解释器/C++程序/无论如何中比较它们并得到意想不到的结果。
The discussion to this answer just got me thinking about equality and equivalence of floating point numbers. I am aware that floating point numbers can not always be represented accurately. The question is, are there mathematically equivalent expressions that will yield different results when using floating point arithmetic? Can you provide an example?
Edit: Let me be more clear. I am aware that the same code with different compilers or different machines can return different results. What I am looking for are two mathematically equivalent expressions that I can compare in my Python interpreter/C++ program/Whatever and get an unexpected result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
绝对地。事实上,您应该预料到这种情况会经常发生。
即使相同的代码也可能产生不同的结果结果在不同的机器或编译器上。
当然。此 Java 代码应该重复产生两个不同的结果:
Absolutely. In fact, you should expect this to happen more often than not.
Even the same code can yield different results on different machines or compilers.
Sure. This Java code should repeatably yield two different results:
是的,比较例如
,
这里有一个 C 语言的示例,它演示了这一点:
对我来说,这给出了以下结果:
(Core i7,gcc 4.0.1,Mac OS X 10.6)
请注意,通常您可能会使用任何给定的表达式得到不同的结果,并且不同的CPU、编译器、编译器开关、数学库等。
Yes, compare e.g.
with
Here's an example in C which demonstrates this:
For me this gives the following results:
(Core i7, gcc 4.0.1, Mac OS X 10.6)
Note that in general you may get different results with any given expression and different CPU, compiler, compiler switches, math library, etc.
我会尝试使用乘法和加法:
在伪 C 代码中
I'd try with multiplication and addition:
in pseudo-c code