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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Ask HN: Inconsistent expression calculation between programming languages

83 点作者 mingodad将近 4 年前
This expression shows that we have several disagreements between programming languages about what is the end result:<p>=====<p>EXPR = (((((((788)*(8.46))))+8342*1.803-1))*4186.4*(15))*(((22%284&#x2F;((7530&#x2F;((2)*(((((25))-421))))))*597%2663)+7283.8-9.60+167.8644%((3))))+(8871)<p>POSTGRESQL = 8291561284461.33301440<p>SQUILU = 8291561284461.3<p>JAVA = 8.291561284461331E12<p>D = 8.29156e+12<p>SQLITE = 8290383058308.3<p>PHP = 8035491468054<p>JAVSCRIPT = 8036090802426.098<p>MYSQL = 8036090802312.071<p>PYTHON = 1.1107636287e+13<p>RUBY = 11107636286950.146<p>LUA = 11665910614443<p>=====

21 条评论

psyklic将近 4 年前
This is solely due to differences in the &#x2F; and % operators:<p><pre><code> # int&#x2F;int -&gt; int POSTGRESQL = 8291561284461.33301440 SQUILU = 8291561284461.3 JAVA = 8.291561284461331E12 D = 8.29156e+12 # int&#x2F;int -&gt; float LUA = 11665910614443 PYTHON3 = 11665910614443.387 # int&#x2F;int -&gt; float, UNIQUE: pos%neg -&gt; neg JAVSCRIPT = 8036090802426.098 MYSQL = 8036090802312.071 # single-precision FLOAT # int&#x2F;int -&gt; int, UNIQUE: negint&#x2F;posint -&gt; negint (the floor) PYTHON2 = 1.1107636287e+13 RUBY = 11107636286950.146 # int&#x2F;int -&gt; int, UNIQUE: negfloat%posint -&gt; negint (the ceil) SQLITE = 8290383058308.3 # int&#x2F;int -&gt; float, UNIQUE: negfloat%posint -&gt; negint (the ceil) PHP = 8035491468054</code></pre>
评论 #28099932 未加载
评论 #28099890 未加载
Pinus将近 4 年前
So, we have (at least) different evaluation orders, different meanings of the % operator (How are negative numbers handled?), different meanings of the &#x2F; operator (is 10&#x2F;3=3 or 3.3333 — notice that Python3 joins the 8.29e+12 fold!). This shows two things: 1) Even though it is syntactically correct in N different languages, it does not necessarily mean the same thing, and 2) floating-point math has many traps. Neither should be news to the HN readership.
mingodad将近 4 年前
After removing all modulo operations and transforming all numbers to floating point:<p>====<p>EXPR = (((((((788.0)<i>(8.46))))+8342.0</i>1.803-1.0))<i>4186.4</i>(15.0))<i>(((22.0&#x2F;((7530.0&#x2F;((2.0)</i>(((((25.0))-421))))))*597.0)+7283.8-9.60+167.0))+(8871.0)<p>SQUILU = 8259816920615.1<p>LUA = 8259816920615.1<p>JAVASCRIPT = 8259816920615.111<p>SQLITE = 8259816920615.11<p>POSTGRESQL = 8259816920615.11377654520912550160000<p>MYSQL = 8259816920615.113<p>AMPL = 8.25982e+12<p>PHP = 8259816920615.1<p>RUBY = 8259816920615.111<p>PYTHON = 8.25981692062e+12<p>====
评论 #28100681 未加载
评论 #28101337 未加载
评论 #28100014 未加载
MauranKilom将近 4 年前
Other than nerdsniping, what is the point of this expression?<p>If you wanted to show how different languages treat modulo, type conversions, division, etc., you could just show individual examples. That would be significantly clearer.<p>Was there some practical reason behind creating this expression, or is it just obfuscation?
评论 #28104829 未加载
评论 #28104160 未加载
amichal将近 4 年前
Fairly early on in my career I was on a team writing a GUI programming environment (think old school flow charts) that generated ASP (classic) or Java backends. We allowed simple expressions in assignments with the basic arithmetic operators and functions. We parsed them into a tiny AST and output Java or ASP code for them with lots of explicit parens and casts. After fiddling with minor differences in precedence, associativity and implicit casts we still had some cases that did not match. Pairing with a more senior dev we wrote a C program to generate and compare a large number (tens of millions) of random expressions trying to explore the important variables (small and large floats. Floats that cause funny rounding. Long sequences of almost the same precedence etc).<p>We got all three languages to agree to full precision for almost every case but in each run there were stubbornly alway a few dozen we couldn’t get to match for non obvious and seeming random reasons.<p>This was a prototype that never saw real production use and we spent a couple of weeks on it… it always bothered me that we never understood that last 0.00001%
评论 #28104667 未加载
评论 #28107752 未加载
SavantIdiot将近 4 年前
You can&#x27;t hold a language responsible for &quot;mistakes&quot; if you don&#x27;t understand how the language works. Quotes intentional.
banana_giraffe将近 4 年前
Different languages have different rules around some math operators, notably around order of operations and how % and &#x2F; handle floating point values. There are much simpler expressions that will yield differences.<p>Also, you appear to be using Python 2, the results are different with any modern version of Python.
评论 #28100855 未加载
ben-schaaf将近 4 年前
It&#x27;s hardly incorrect that different languages have different semantics, especially considering the mix of types and use of % in that expression. You could just as well complain that they don&#x27;t all print floating point numbers in the exact same way.
hinkley将近 4 年前
A big part of the oft-overlooked notion of Information Architecture are the twin concepts of Source of Truth and System of Record. Many systems mix or conflate the two or don&#x27;t consider them at all, other than subconsciously when wrestling with issues of code duplication (split SoT).<p>If the result of a calculation is that important to the functioning of your code, then having three implementations in multiple languages is just madness. You need to pick one to believe, or constantly deal with random catastrophes. Personally I enjoy very much not being woken up at 7 am EST to solve production issues. Almost as much as I hate other people getting kudos for problems that we should have not signed up for in the first place. Arsonist-firefighters exist in every dark corner of the programming world, and in much greater numbers than actual arsonist-firefighters.<p>From a technology selection standpoint, all of this info is extremely important, because it will inform both my choice of tools and my immediate task list to deal with any answers I don&#x27;t like. But if you&#x27;re wrestling with this every day, you&#x27;ve already fucked up.
mingodad将近 4 年前
And after removing all modulo operations:<p>====<p>EXPR = (((((((788)*(8.46))))+8342*1.803-1))*4186.4*(15))*(((22&#x2F;((7530&#x2F;((2)*(((((25))-421))))))*597)+7283.8-9.60+167))+(8871)<p>SQUILU = 8515287402650.3<p>SQLITE = 8515287402650.34<p>POSTGRESQL = 8515287402650.347200<p>D = 8.51529e+12<p>JAVA = 8.515287402650345E12<p>MYSQL = 8259816920501.086<p>JAVASCRIPT = 8259816920615.111<p>PHP = 8259816920615.1<p>LUA = 8259816920615.1<p>AMPL = 8.25982e+12<p>GAMS = 8.25982E+12<p>RUBY = 7701542593121.873<p>PYTHON = 7.70154259312e+12<p>====
评论 #28099777 未加载
评论 #28099970 未加载
评论 #28100852 未加载
hansvm将近 4 年前
This kind of thing can bite you even when operating purely on integers. E.g., for a long time (it&#x27;s been fixed for over a dozen years by now though), Python implemented x%n with an intermediate cast to float, with predictably bad results on inputs larger than typical test cases. I had the damnedest time tracking down the culprit since I assumed it had to be serialization, hashing, or one of the standard culprits --- not that integer math was broken.
评论 #28100526 未加载
miga将近 4 年前
Haskell: =====<p>(%) = Data.Fixed.mod&#x27;<p>expr :: Double = 8.036090802426098e12<p>expr :: Rational = 630330872315297185317 % 78437500<p>fromRational (expr :: Rational) :: Double = 8.0360908024261e12
mingodad将近 4 年前
And making all operands to modulo operator &quot;%&quot; integer:<p>=====<p>EXPR = (((((((788)*(8.46))))+8342*1.803-1))*4186.4*(15))*(((22%284&#x2F;((7530&#x2F;((2)*(((((25))-421))))))*597%2663)+7283.8-9.60+167%((3))))+(8871)<p>D = 8.29038e+12<p>JAVA = 8.290383058308305E12<p>MYSQL = 8034912576159.046<p>POSTGRESQL = 8290383058308.307200<p>SQLITE = 8290383058308.3<p>SQUILU = 8291561284461.3<p>JAVASCRIPT = 8034912576273.071<p>PHP = 8035491468054<p>AMPL = 8.03491e+12<p>GAMS = 8.03491E+12<p>PYTHON = 1.11064580608e+13<p>RUBY = 11106458060797.121<p>LUA = 11664732388290<p>====
gus_massa将近 4 年前
Try to edit your post and add a \ before each * so it is not interpreted as italics here.<p>Have you tried to simplify it? My guess is that the problem is not in the last &quot;+(8871)&quot;.
评论 #28100344 未加载
ac42将近 4 年前
<a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;False_friend" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;False_friend</a>
anthropodie将近 4 年前
For Go, we get compilation error<p>.&#x2F;prog.go:8:129: invalid operation: 167.864 % 3 (operator % not defined on untyped float)
fishmaster将近 4 年前
Julia 1.3.1: 8.036090802426098e12
MrStonedOne将近 4 年前
In BYOND (Dream Maker&#x2F;.dm):<p>8035491119104<p>(single floats on all numbers, unsure of negmod behavior)
svnpenn将近 4 年前
You couldnt come up with a simpler example? Youve got at least 12 operations there. Pretty much a single division operation wouldve worked about the same
brudgers将近 4 年前
And that is why people still use FORTRAN.
评论 #28087063 未加载
NotSwift将近 4 年前
These are some shocking differences. How did you come up with this expression?
评论 #28086094 未加载