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.

Floating Point Visually Explained

451 pointsby alxmdevover 7 years ago

24 comments

Veedracover 7 years ago
Let&#x27;s represent the number 42,643,192, or 10100010101010111011111000₂, in different &quot;floating point&quot; representations.<p>Scientific notation with 5 significant figures:<p><pre><code> 4.2643 × 10⁷ </code></pre> Scientific notation in base 2 with 17 significant binary figures:<p><pre><code> 1.0100010101010111₂ × 2²⁵ </code></pre> Let&#x27;s pack this in a fixed-length datatype. Note that 011001₂ is the binary encoding of 25.<p><pre><code> 1 0100010101010111 011001 1 mantissa exp. </code></pre> This doesn&#x27;t suffice because<p>a. We&#x27;re wasting a bit on the leading 1.<p>b. We want to support negative values.<p>c. We want to support negative exponents.<p>d. It would be nice if values of the same sign sorted by their representation.<p>The leading 1 can be dropped and replaced with a sign bit (0 for &quot;+&quot;, 1 for &quot;-&quot;). The exponent can have 100000₂ subtracted from it, so 011001₂ represents 25-32, or -7, and 111001₂ represents 25. Sorting can be handled by putting the exponent before the mantissa.<p>Thus we get to a traditional floating point representation.<p><pre><code> 0 111001 0100010101010111 ± exp. mantissa </code></pre> Real floating point has a little more on top (infinities, standardised field sizes, etc.) but is fundamentally the same.
评论 #15361832 未加载
kehrlannover 7 years ago
Best simple explanation I&#x27;ve seen so far.<p>I highly recommend Fabien&#x27;s Game Engine Black Book. I&#x27;m halfway through it, and it&#x27;s really fun. I&#x27;ve only been a software dev for 6 years, so looking at how things could be hacked around in the 90s to squeeze every drop of performance out of very constrained devices is fascinating.
评论 #15359948 未加载
评论 #15361529 未加载
mdipover 7 years ago
&gt; People who really wanted an hardware floating point unit in 1991 could buy one. The only people who could possibly want one back then would have been scientists (as per Intel understanding of the market). They were marketed as &quot;Math CoProcessor&quot;. Performance were average and price was outrageous (200 USD in 1993 equivalent to 350 USD in 2016.). As a result, sales were mediocre.<p>Actually, that&#x27;s only <i>partly</i> true. My father owned a company that outfitted large manufacturing shops (MI company, you can imagine who his customers were). As a result, he used AutoCAD. The version of AutoCAD he used had a hard requirement on the so-called &quot;Math Co-processor&quot;, so he ended up having to purchase one and install it himself. That was my first taste of taking a computer apart and upgrading it and I credit that small move with my becoming interested in building PCs, which led to my dad and I starting a business in the 90s doing that for individuals and businesses. There were definitely more reasons for that kind of add-on than just scientific fields; anyone in the computer aided drafting world at that time needed one as well.
评论 #15365081 未加载
jordighover 7 years ago
Okay, fine, I agree that sometimes mathematical notation is bad and we are all computer people here, not math people, so we get really scared of mathematical notation.<p>But is (-1)^S 1.M 2^(E-127) so bad that it required a whole blog post to explain it? Except for the &quot;1.M&quot; pseudo-notation to explain the mantissa with the implicit on bit, all of those symbols are found in most programming languages we use.<p>I don&#x27;t think the value of the blog post was explaining the notation. We all knew what operations to perform when we saw it. The value seems to lie more in thinking of the exponent as the offset on the real line and the mantissa as a certain window inside that offset.<p>Personally, though, this still doesn&#x27;t seem like a huge, deep insight to me, but maybe I&#x27;m just way too used to floating point and have forgotten how hard it was to learn this. I did learn about mantissa, exponents, and even learned how to use a log table in high school, but maybe I&#x27;m just old and had an unusual high school experience.
评论 #15360183 未加载
评论 #15360298 未加载
评论 #15360891 未加载
评论 #15360478 未加载
评论 #15362628 未加载
dragontamerover 7 years ago
Here&#x27;s everything you need to know about Floating Point in as shortly as I can write it.<p>1. Floating points are simply &quot;Binary Scientific notation&quot;. The speed of light is 2.98E8... which in &quot;normal form&quot; is written 298,000,000. An IEEE 754 Single has 8-bits for the exponent (E8 in the speed of light), and 24-bits for the mantissa (the 2.98 part). There&#x27;s some complicated stuff like offset shifting here, but this is the &quot;core idea&quot; of floating point.<p>2. &quot;Rounding&quot; is forced to happen in Floating Point whenever &quot;information drops off&quot; the far side of the mantissa. The mantissa is only 24-bits long, and many numbers (such as .1) require an infinite number of bits to represent! As such, this &quot;rounding error&quot; builds up <i>exponentially</i> the more operations you perform.<p>3. Subtraction (cancellation error) is the biggest single source of error and the one that needs to be most studied. &quot;Subtraction&quot; can occur when a positive and negative number is added together.<p>4. Because of this error (and all errors!), Floating point operations are NOT associative. (A + B) + C gives a different value than A + (B + C). The commutative property remains for multiplication and addition (A+B == B+A). If you require &quot;bit-perfect&quot; and consistent floating-point simulations, you MUST take into account the order of all operations, even simple addition and multiplication.<p>For example: Try &quot;0.1 + 0.7 + 1&quot; vs &quot;1 + 0.1 + .7&quot; in Python, and you&#x27;ll see that these to orderings lead to different results.<p>---------------<p>Once you fully know and understand these 4 facts, then everything else is just icing on the cake. For example to prevent &quot;cancellation error&quot; (#3), you can sort the numbers by magnitude, and then add them up from smallest magnitude to largest magnitude.
评论 #15359924 未加载
评论 #15359955 未加载
评论 #15360773 未加载
habermanover 7 years ago
I like the &quot;window&#x2F;offset&quot; concept. I wrote an extended blog article with yet different visual aids: <a href="http:&#x2F;&#x2F;blog.reverberate.org&#x2F;2014&#x2F;09&#x2F;what-every-computer-programmer-should.html" rel="nofollow">http:&#x2F;&#x2F;blog.reverberate.org&#x2F;2014&#x2F;09&#x2F;what-every-computer-prog...</a>
评论 #15360431 未加载
conistonwaterover 7 years ago
I really wish he didn&#x27;t make [0,1] one of the windows, because in floating point arithmetic the range [0,1] contains approximately as many floating point numbers (a billion or so in Float32) as the range [1,∞). There are &quot;windows&quot; [2^k,2^(k+1)] for positive <i>as well as negative</i> k. Just creates unnecessary scope for further confusion.
评论 #15364668 未加载
cesarbover 7 years ago
IMO, the best way to explain floating point is to play with a tiny float. With an 8-bit float (1 bit sign, 4 bits exponent, 3 bits mantissa, exponent bias 7), there are only 256 possible values. One can write by hand a table with the corresponding value for each of the 256 possibilities, and get a feel to how it really works.<p>(I got the 1+4+3 from <a href="http:&#x2F;&#x2F;www.toves.org&#x2F;books&#x2F;float&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.toves.org&#x2F;books&#x2F;float&#x2F;</a>, I don&#x27;t know if it&#x27;s the best allocation for the bits; but for didactic purposes, it works.)
评论 #15363375 未加载
makmanalpover 7 years ago
&gt; Since floating point units were so slow, why did the C language end up with float and double types ? After all, the machine used to invent the language (PDP-11) did not have a floating point unit! The manufacturer (DEC) had promised to Dennis Ritchie and Ken Thompson the next model would have one. Being astronomy enthusiasts they decided to add those two types to their language.<p>Wait, what was the alternative? No floats? How the heck would people calculate things with only integers?<p>edit: AFAIK bignums are even slower, and fixed-point accumulates error like crazy
评论 #15361736 未加载
评论 #15361004 未加载
评论 #15360591 未加载
评论 #15360622 未加载
评论 #15366540 未加载
jokoonover 7 years ago
Imagine a ruler with all floating point values on it, each time the mantissa comes at its maximum, you increase the exponent, so the space between farther float values doubles.<p>The number of mantissa values being constant for each exponent value, the exponent describes some kind of &quot;zoom level&quot;.<p>Float values on a ruler would sort of looks like this:<p><pre><code> ... x x x x x x x x x x... ^ exponent increases, spacings are doubled</code></pre>
coldteaover 7 years ago
I see how some people just get the math, but I don&#x27;t see why programmers here say they find it difficult to understand the window &#x2F; offset explanation the article gives.<p>A &quot;window&quot; is a common programming term for a range between two values.<p>An &quot;offset&quot; is a common term for where a value falls after a starting point.<p>In simpler decimal and equidistant terms, the idea is to split a range of values in windows, divide each window in N values, and store an FP number by storing which window and which index inside the window (0 to N) it falls.<p>The FP scheme actually uses powers of 2 instead of equal distant windows (so the granularity becomes coarser as the numbers become bigger) but the principle is the same.
评论 #15361045 未加载
userbinatorover 7 years ago
I think the example values at <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Minifloat" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Minifloat</a> are most useful for intuitively understanding how floating point works --- especially the &quot;all values&quot; table, which shows how the numbers are spaced by 1s, then 2s, then 4s, etc. meaning the same number of values can represent a larger range of magnitudes, but sacrificing precision in the process.
mauritsover 7 years ago
Obligatory: What Every Computer Scientist Should Know About Floating-Point Arithmetic [1] (pdf)<p>[1]: <a href="http:&#x2F;&#x2F;www.itu.dk&#x2F;~sestoft&#x2F;bachelor&#x2F;IEEE754_article.pdf" rel="nofollow">http:&#x2F;&#x2F;www.itu.dk&#x2F;~sestoft&#x2F;bachelor&#x2F;IEEE754_article.pdf</a>
s17nover 7 years ago
A much easier and better way to understand floating point is to just do it in base 10.
评论 #15360030 未加载
BinaryBulletover 7 years ago
See also: An interactive floating point visualization: <a href="https:&#x2F;&#x2F;evanw.github.io&#x2F;float-toy&#x2F;" rel="nofollow">https:&#x2F;&#x2F;evanw.github.io&#x2F;float-toy&#x2F;</a>
oxideover 7 years ago
As a complete layman with only a cursory knowledge of programming, as well as a complete lack of math skills above Algebra 2, (I didn&#x27;t even complete that, tbh, once they threw graphing into the equation. I did get slope-intercept form down, but that&#x27;s it.)<p>I ended up finding this easier to understand than I expected, and a great read.<p>I love explanations like these, with a visual breakdown. It really helps it &quot;click.&quot;<p>as long as I glazed over the math formulas and didn&#x27;t let the numbers overwhelm me.<p>This is what I took away: the exponent &quot;reaches&quot; out to the max value of the [0,1] [2,4] etc, and the number represented tends to be like 51-53% of the way down the line of the mantissa.<p>It &quot;clicked&quot; a bit for me, see? Am I way off?<p>This is the way I always learned math the best in school, an alternate explanation that helps it &quot;click.&quot;<p>Very good explanation, from my point of view, of how floating point numbers work and what they even are.<p>That&#x27;s a nice feeling for someone like me who is pretty bad at math and finds formulas like the one shown in the article to be, frankly, indecipherable.<p>But now I (sort of) understand how floating point numbers work, (sort of) what they are, why they are important, and what role they play.<p>Could I program anything using one? No. But, I could learn someday, and explanations like these give me some hope that I just might be able to learn a programming language if I put the effort in. That I could learn the math required of me, even!
javajoshover 7 years ago
Why did they fix the bit-width for the mantissa and exponent? It would be nice to have more bits for the mantissa when you are near 1, and then ignore the mantissa entirely when you&#x27;re dealing with enormous exponents, and very far from one. Granted, there would be some overhead (e.g. a 3-bit field describing the exponent length, or something) but it would be a useful data-structure.
评论 #15360635 未加载
评论 #15360360 未加载
dsegoover 7 years ago
&gt; Instead of Exponent, think of a Window between two consecutive power of two integers.<p>I know what an exponent is, or if you want &quot;order of magnitude&quot;. Sorry, but &quot;A window between two consecutive power of two integers&quot; doesn&#x27;t make it easier to think about.
评论 #15360338 未加载
评论 #15360319 未加载
agumonkeyover 7 years ago
The last bits of trivia are very nice.<p>The x87 coprocessor makes me wonder about days were each chip changed you system. It was such a different mindset that videogame consoles had parallel routes between the board and the cartridge themselves to allow hardware extension per game.
trianglemanover 7 years ago
&gt;I wanted to vividly demonstrate how much of a handicap it was to work without floating points.<p>So, did he manage to demonstrate that in the book? Because the page linked here, while explaining how floating points are represented in memory, does not explain how computers perform operations on them, or what purpose does a FPU serve (how does it differ from an ALU).
endorphoneover 7 years ago
<a href="https:&#x2F;&#x2F;dennisforbes.ca&#x2F;index.php&#x2F;2017&#x2F;04&#x2F;11&#x2F;floating-point-numbers-an-infinite-number-of-mathematicians-enter-a-bar&#x2F;" rel="nofollow">https:&#x2F;&#x2F;dennisforbes.ca&#x2F;index.php&#x2F;2017&#x2F;04&#x2F;11&#x2F;floating-point-...</a>
piyush_soniover 7 years ago
Does anyone have an alternate link, for some strange reason this link appears to be blocked at my work.
pmelendezover 7 years ago
Off topic: The response of dragontamer is one example about why down votes alone are not enough. It was downvoted to the dead level and now nobody can reply to it. But also nobody gave the reason why what he was saying is incorrect.
评论 #15360019 未加载
评论 #15359949 未加载
评论 #15359932 未加载
评论 #15359916 未加载
vxNsrover 7 years ago
I was hoping for something akin to a xkcd or SMBC comic, this isn&#x27;t really much better than what my asm prof said when he explained it for MIPS programming. Maybe it&#x27;s because I don&#x27;t get what he means by offset and window, but this wasn&#x27;t really that helpful.
评论 #15359775 未加载
评论 #15359814 未加载
评论 #15360225 未加载
评论 #15359928 未加载