TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Bits in a Float, and Infinity, NaN, and Denormal (2012)

38 pointsby Cieplakover 6 years ago

6 comments

wjakobover 6 years ago
This analysis is somewhat dated and leaves out one important fact: nowadays, floating point arithmetic is carried out using a set of special scalar SSE instructions (and not the ancient x87 co-processor, as was done in the author&#x27;s benchmark).<p>SSE instructions remove performance pitfalls related to infinities and NaNs. The only remaining case where slowdowns are to be expected denormals (which can be set to flush-to-zero if desired.)<p>In other words: it&#x27;s perfectly fine to work with infinities and NaNs in your code.
phkahlerover 6 years ago
You may also want to read about posits:<p><a href="https:&#x2F;&#x2F;www.johndcook.com&#x2F;blog&#x2F;2018&#x2F;04&#x2F;11&#x2F;anatomy-of-a-posit-number&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.johndcook.com&#x2F;blog&#x2F;2018&#x2F;04&#x2F;11&#x2F;anatomy-of-a-posit...</a><p>I&#x27;m a fan of these not because of the claims regarding precision, but because they drop all the complexity and baggage of IEEE floating point.
评论 #17987565 未加载
kimburgessover 6 years ago
Groking that way that floats work really is a lot of fun.<p>Years ago a put together a math library (<a href="https:&#x2F;&#x2F;github.com&#x2F;KimBurgess&#x2F;netlinx-common-libraries&#x2F;blob&#x2F;master&#x2F;math.axi" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;KimBurgess&#x2F;netlinx-common-libraries&#x2F;blob&#x2F;...</a>) for a domain specific language that had some &quot;limited&quot; capabilities. All functionality had to be achieved through a combination of some internal serialisation functions and bit twiddling.<p>It was simultaneously one of the most painful and interesting projects I&#x27;ve done.
slivymover 6 years ago
On a personal note, this representation annoys me:<p>value = (-1) sign * 2 (exponent-127) * 1.fraction<p>It should be:<p>value = (-1) sign * 2 (exponent-127) * (1 + fraction*2^-23)<p>It sounds trivial, but you can&#x27;t reason mathematically about the first equation.
评论 #17987074 未加载
评论 #17986857 未加载
protonfishover 6 years ago
In my opinion, the confusion that arises when programmers get results from floating-point computations that are not what they expect stems from this:<p>&gt; Floats represent continuous values.<p>But as you probably know, this isn&#x27;t possible. The concept of infinite precision is interesting in theory, but disappears when any actual calculation needs to be made, whether on a digital electric computer or not.<p>I wonder if this is not a flaw in the crude mechanical representation of numbers, but a flaw in the decision to base floating-point computation on the concept of continuous numbers. I believe that a better model for floating-point computational representation and manipulation would be to reflect the rules of scientific measurements - that each number includes an explicit amount of precision that is preserved during mathematical operations.<p>This would not only keep JavaScript newbies from freaking out when they add 0.1 and 0.2, but prevent problems of thinking calculation results are correct, when they are not.<p>If you aren&#x27;t getting what I am saying, let me give an example. Let&#x27;s say, for some reason, you want to measure the diameter of a ball. You have a measuring tape so you wrap it around the widest part and record that it is 23.5cm. To calculate the radius, you should divide by π. If you do this in double-precision floating point, you will get 7.480282325319081, but this is nonsense. You can&#x27;t create a result that is magically more precise than your initial measurement though division or multiplication. The correct answer is 7.48cm. This preserves the amount of precision in the least precise operand, and is arguably the most correct result.
评论 #17987194 未加载
评论 #17987342 未加载
评论 #17987127 未加载
评论 #17987142 未加载
nayukiover 6 years ago
The article content is decent, but I can&#x27;t stand the fact that the author used this bit of CSS to make all the text unreasonably small: &lt;style&gt; body, div, ... { font-size: x-small } &lt;&#x2F;style&gt;