TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Incorrect optimization in 1963

52 点作者 StylifyYourBlog超过 10 年前

5 条评论

StefanKarpinski超过 10 年前
&gt; Floating-point users today are accustomed (or resigned, sometimes) to compilers that make invalid optimizations by assuming all arithmetic is mathematically correct instead of rounding.<p>Huh? Modern compilers don&#x27;t do that. They will only reorder or re-associate operations if they know that it will produce the exact same result. Anything else is considered a severe compiler bug. The exception is if you explicitly ask for such sketchy optimizations with the `-ffast-math` flag, of which the GCC man page says: &quot;This option is not turned on by any -O option besides -Ofast since it can result in incorrect output for programs.&quot; This option is off by default. The rest of the article is interesting, if correct – unfortunately, it&#x27;s a little hard not to be skeptical when the first sentence is so flagrantly wrong.
评论 #8918645 未加载
评论 #8920240 未加载
Someone超过 10 年前
Undesirable and surprising, yes, but incorrect? The manual clearly describes the behaviour and AFAIK, there wasn&#x27;t a fortran standard yet.<p>I guess the reordering was done to simplify later compiler passes, in particular the common subexpression elimination one.
评论 #8918180 未加载
kazinator超过 10 年前
The &quot;not grouped by parentheses&quot; is already where things go wrong. Parentheses should determine the syntactic parse of the expression, not the order of evaluation. Optimization works with an abstract representation of the program where the parenthesis tokens are long gone.<p>That is to say (a + b) + c should not have any influence relative to a + b + c, at least if addition is already left associative. Because then a + b + c <i>already means</i> (a + b) + c. Adding the parentheses to make (a + b) + c doesn&#x27;t change the parse whatsoever.<p>If parentheses are needed to establish the precise meaning, it means that the compiler writer has neglected to document, and commit to, an associativity for the operator; the compiler writer has not specified whether a + b + c means (a + b) + c or a + (b + c). That is a mistake: a gratuitous ambiguity in the syntax of the programming language.<p>If there is such a mistake in the design, a stray ambiguity, then you cannot call the optimization wrong. If it is not specified whether a + b + c means (a + b) + c, or a + (b + c) or something else like add3_in_any_order(a, b, c), then the compiler is in fact free to use any of the possible parses as a guide for the operation order.
评论 #8920481 未加载
InfiniteRand超过 10 年前
If you want really accurate math, the best idea for a program is maintain a representation of an expression until it absolutely needs to, since every time the math is condensed into actual numbers there is usually some loss of precision. Even as humans, it makes sense, we rarely write 0.33333 instead of 1&#x2F;3 unless we have to by circumstance (or at least you souldn&#x27;t)
hamburglar超过 10 年前
They say never optimize too early. 1963 is definitely too early in my experience.