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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Floating Point in the Browser, Part 1: Impossible Expectations

64 点作者 nikbackm超过 4 年前

9 条评论

SAI_Peregrinus超过 4 年前
I&#x27;ve mentioned it before, but JSON Numbers are arbitrary-precision floating-point. They can&#x27;t be naively stored as JavaScript numbers in all cases, since JS numbers are IEEE754 double-precision floating point. Likewise, you can&#x27;t store arbitrary JS number values in JSON Numbers, since it doesn&#x27;t handle IEEE754 exception values (qNaN, sNaN, ±INF).<p>So if you&#x27;re trying to publish data from a GPS chip that reports fix failure by setting the latitude and longitude to a NaN (possibly with a failure code in the exception value) you can handle that in JS but only through some intermediate representation in JSON.
评论 #24629607 未加载
评论 #24630352 未加载
评论 #24629647 未加载
评论 #24629478 未加载
chrisseaton超过 4 年前
Anyone who&#x27;s ever worked on programming language implementation will recognise that you will for all time get a steady stream of occasional bug reports that your floating point numbers aren&#x27;t arbitrarily precise.<p>Some language implementation bug trackers have a warning not to submit bugs for it front and centre because they get it so often.
评论 #24629146 未加载
评论 #24629459 未加载
saagarjha超过 4 年前
What’s also interesting is how the “minimum representation needed to roundtrip a number” is actually done. There is recent (2010) research into doing this efficiently using an algorithm called Grisu: <a href="https:&#x2F;&#x2F;dl.acm.org&#x2F;doi&#x2F;10.1145&#x2F;1809028.1806623" rel="nofollow">https:&#x2F;&#x2F;dl.acm.org&#x2F;doi&#x2F;10.1145&#x2F;1809028.1806623</a>. Many programming language implementations use it now for this purpose.
评论 #24629799 未加载
jakub_g超过 4 年前
The author has many blog posts on floating point math, and they&#x27;re all very interesting. If you&#x27;re into the topic, you can check the tagged entries:<p><a href="https:&#x2F;&#x2F;randomascii.wordpress.com&#x2F;category&#x2F;floating-point&#x2F;" rel="nofollow">https:&#x2F;&#x2F;randomascii.wordpress.com&#x2F;category&#x2F;floating-point&#x2F;</a><p>In general the whole blog is super interesting and many times submitted to HN:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;from?site=randomascii.wordpress.com" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;from?site=randomascii.wordpress...</a>
umvi超过 4 年前
TIL JavaScipt doesn&#x27;t have an integer type and uses double under the hood to store integers. Yet another footgun to add to the massive stack.
评论 #24629632 未加载
评论 #24630822 未加载
评论 #24629193 未加载
评论 #24628926 未加载
评论 #24628946 未加载
Spivak超过 4 年前
I really really wish these things were errors. Data loss should require an explicit opt-in. It&#x27;s easy to point to the spec and say that people should obviously know it back-and-forward but it&#x27;s extremely surprising that JSON decoding is a lossy operation and you have reach not for an option flag but a whole 3rd party library to not get that behavior.<p>JSON&#x27;s arbitrary precision decimals is the silliest footgun. When every implementation implicitly defines its own valid format by way of what native types it maps it makes for a really awkward way to pass messages between two black-box systems.
lmilcin超过 4 年前
I feel like a lot of discussion is on floating point accuracy and artifacts and most of it misses the point completely.<p>It doesn&#x27;t help there are IEEE standards for fp calculations which are also very misunderstood. The point of these standards is to provide <i>reproducibility</i> and <i>repeatability</i> so that different types of software running on different machines or with different compilers provide same results.<p>The goal of floating point never was to provide precise representation or calculation and in fact it is not possible to provide in general with floating point. The goal was to make it easy and relatively fast to perform real world calculations.<p>You understand your calculator gives approximate results except for very special cases, so why would you bicker over some digits that are very far to the right of your result.<p>It is actually much worse with calculators because they tend to employ very different calculation engines and very different precision. You can actually identify a particular model or model family of calculator by running couple calculations and finding what kind of artifacts are present in the results, exactly.<p>Yet nobody will tell you calculators are useless as I frequently hear about floating point.<p>I work with Java and there seems to be this movement recently where developers use arbitrary precision arithmetic to do any sort of calculation on anything without actual idea of when this is needed. Supposedly flating point is broken and cannot be trusted. Sigh...
评论 #24630707 未加载
评论 #24630734 未加载
pmarreck超过 4 年前
I avoid FP as much as possible precisely because of these nondeterministic (at least in the practical sense) results. I use integers almost exclusively and if I need fixed point I have view code that renders things correctly (such as for currency). When Erlang didn&#x27;t have a power function that didn&#x27;t use floating-point (and thus resulted in rounding errors), I wrote my own: <a href="https:&#x2F;&#x2F;github.com&#x2F;pmarreck&#x2F;elixir-snippets&#x2F;blob&#x2F;master&#x2F;math_integer_power.exs" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pmarreck&#x2F;elixir-snippets&#x2F;blob&#x2F;master&#x2F;math...</a>
评论 #24630261 未加载
spullara超过 4 年前
And now you know why the Twitter API has all IDs as both numbers and strings.