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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Floating Point Math

56 点作者 hegzploit将近 3 年前

8 条评论

svat将近 3 年前
A couple of thoughts I&#x27;ve always had about floating-point arithmetic:<p>1. IMO it&#x27;s unfortunate that most languages default to floating-point. Most programmers, most of the time, would be better served by slightly slower but less confusing alternatives (it&#x27;s nice that Raku uses rational numbers by default: similarly for integers, it&#x27;s great that Python uses arbitrary-precision integers by default). At any rate, programmers would be a lot less confused about floating-point arithmetic if they had to opt in to it explicitly, e.g. instead of 0.1 + 0.2 if they had to say something super-explicit like (just exaggerating a bit for effect, this is probably impractical anyway):<p><pre><code> NearestRepresentableSum(NearestRepresentable(&quot;0.1&quot;), NearestRepresentable(&quot;0.2&quot;)) </code></pre> till they got the hang of it.<p>2. IMO when explaining floating-point arithmetic it helps to add a picture, such as this one (added to Wikipedia by a user named Joeleoj123 in Nov 2020): <a href="https:&#x2F;&#x2F;upload.wikimedia.org&#x2F;wikipedia&#x2F;commons&#x2F;b&#x2F;b6&#x2F;FloatingPointPrecisionAugmented.png" rel="nofollow">https:&#x2F;&#x2F;upload.wikimedia.org&#x2F;wikipedia&#x2F;commons&#x2F;b&#x2F;b6&#x2F;Floating...</a><p>With this picture (or a better version of it), one can communicate several main ideas:<p>- There are a finite number of representable values (the green points on the number line),<p>- Any literal like &quot;0.1&quot; or &quot;0.2&quot; or &quot;0.3&quot; is interpreted as the closest representable value (the closest green point),<p>- Arithmetic operations like addition and multiplication give the closest green point to the true sum,<p>- There are more of them near 0 and they get sparser away from 0 (the &quot;floating-point&quot; part),<p>etc.<p>Further, by staring at this picture, and the right words, one can infer (or explain) many of the important properties of floating-point arithmetic: why addition is commutative but not associative, why it is a good idea to add the small numbers first, maybe even the ideas behind Kahan summation and what not.
评论 #31806581 未加载
评论 #31808019 未加载
评论 #31807197 未加载
评论 #31809928 未加载
评论 #31806498 未加载
ketzu将近 3 年前
It&#x27;s a nice website explaining the problem lightly, but I think the cool part is the encyclopedic language list handling floating point numbers and the reference for bigdecimal support.<p>The only sentence I don&#x27;t really like:<p>&gt; When you have a base-10 system (like ours), it can only express fractions that use a prime factor of the base.<p>It&#x27;s a weird mix of over- and under-generalization. The second half sounds like a feature of all number systems independent of the base, and we can express more numbers (just with a notion of &#x27;repeating infinitely&#x27; or as fractions), that&#x27;s why they even switch to &quot;expressed cleanly&quot; in the next sentence.<p>If I get more sleep and can think of a good way to express it, maybe I&#x27;ll actually make a pull request ;)
评论 #31805812 未加载
评论 #31807337 未加载
WaffleIronMaker将近 3 年前
previous discussions: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;from?site=30000000000000004.com" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;from?site=30000000000000004.com</a>
评论 #31805798 未加载
dx034将近 3 年前
Such a shame that decimals don&#x27;t get more attention. I program in Go a lot and they don&#x27;t even have a fixed point number type built into the language. Floating point numbers always seems to be the default. And while there is support via third party packages, it makes everything harder to use, from communicating with databases to (de-)serializing data.<p>However, in reality most numbers I deal with can reasonably be assumed to have a maximum number of decimals and can be stored easily this way. I know way fewer examples where I&#x27;d need floating point precision than where I need a fixed number of decimals. But with poorer support I always fall back to using float.
评论 #31807179 未加载
nraynaud将近 3 年前
Do you have a good blog post on choosing tolerances and ranges? I just started at a company where it’s probably about now that we should get smart with that. We do geometry so the problem gets even worse.
评论 #31813193 未加载
toxicFork将近 3 年前
Why don&#x27;t we store fractions as fractions rather than floating point?
评论 #31805814 未加载
评论 #31807303 未加载
评论 #31806305 未加载
评论 #31805876 未加载
评论 #31806497 未加载
评论 #31806172 未加载
评论 #31806554 未加载
评论 #31806442 未加载
waynecochran将近 3 年前
We all know this by now right? We know computers store numbers in binary (unless using BCD) and numbers like 1&#x2F;10 and 1&#x2F;3 can only be approximated in a finite number of bits. This isn&#x27;t news is it?
评论 #31806147 未加载
评论 #31805825 未加载
评论 #31805766 未加载
评论 #31807159 未加载
franga2000将近 3 年前
&gt; Your language isn’t broken, it’s doing floating point math.<p>Hard disagree right from the start - if your language is doing floating point math by default (as in, if numeric literals and basic operators use floating point in the absence of explicit type annotations or tags), your language is broken. Maybe if it&#x27;s hyper-targeted towards graphics or ML or some other field where floating point errors don&#x27;t matter and performance is king, then I&#x27;d call it fine, but users of such languages probably aren&#x27;t reading this page.<p>Now, I get what the author is saying - there isn&#x27;t a bug in your interpreter, it&#x27;s how it&#x27;s designed. But that means it&#x27;s broken by design.