算法与计算机科学fabiensanglard.net编辑精选
图解浮点数
Floating Point Visually Explained
2017 — 2021,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
Fabien Sanglard 的经典博文,用大量可视化图示讲解 IEEE 754 浮点数的表示方式:把浮点数理解为“指数窗口 + 尾数偏移”,直观解释了为什么浮点数在数轴上分布不均匀、精度如何随数量级变化。文章还穿插了 90 年代数学协处理器等历史背景。
为什么经久不衰:它把抽象的浮点数标准变成一眼能看懂的图像,多年来反复登上 HN 首页,至今仍是理解浮点数最直观的入门材料。
HN 评论精选
从零推导浮点数编码,说明其设计动机
用 42,643,192 这个数举例,一步步推导出传统浮点表示:先写成二进制科学计数法,再去掉浪费的首位 1、加上符号位、用偏移码支持负指数、把指数放在尾数前以便排序,最终得到 ± 指数 尾数的经典布局。真实浮点数只是在此基础上加了无穷大等细节。
Veedrac查看英文原文
Let's represent the number 42,643,192, or 10100010101010111011111000₂, in different "floating point" representations. Scientific notation with 5 significant figures: 4.2643 × 10⁷ Scientific notation in base 2 with 17 significant binary figures: 1.0100010101010111₂ × 2²⁵ Let's pack this in a fixed-length datatype. Note that 011001₂ is the binary encoding of 25. 1 0100010101010111 011001 1 mantissa exp. This doesn't suffice because a. We're wasting a bit on the leading 1. b. We want to support negative values. c. We want to support negative exponents. d. It would be nice if values of the same sign sorted by their representation. The leading 1 can be dropped and replaced with a sign bit (0 for "+", 1 for "-"). 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. Thus we get to a traditional floating point representation. 0 111001 0100010101010111 ± exp. mantissa Real floating point has a little more on top (infinities, standardised field sizes, etc.) but is fundamentally the same.
补充史实:数学协处理器的买家不止科学家,还有 CAD 用户
文章说 1991 年只有科学家才需要数学协处理器,这只对了一部分。我父亲的公司做制造业设备,他用的 AutoCAD 硬性要求“数学协处理器”,只能自己买来装上。那次拆机升级让我迷上了组装电脑,后来父子俩在 90 年代靠这个开了生意。当时 CAD 圈子人人都需要它。
mdip查看英文原文
> 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 "Math CoProcessor". Performance were average and price was outrageous (200 USD in 1993 equivalent to 350 USD in 2016.). As a result, sales were mediocre. Actually, that's only partly 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 "Math Co-processor", 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.
用分段线性近似和百分比误差的视角补充理解浮点数
另一种文字版的直观解释:浮点数(忽略次正规数)就是对 2^x 的分段线性近似,每个整数区间一段。例如在 2^4 到 2^5 之间画一条直线,该区间的浮点数就均匀分布在这条线上。用“窗口+偏移”的说法,偏移就是窗口内的百分比位置——浮点数没有绝对误差,只有百分比误差。次正规数是例外,它们牺牲百分比精度来扩展极小数的表示范围。
Veserv查看英文原文
For another visual explanation in words, floating point numbers (ignoring subnormal) are just a linear approximation of 2^x [1] where there is one piece for each integer (x = 4 to x = 5, etc). As an example, draw a straight line between 2^4 (16) and 2^5 (32). The floating point numbers in that range are evenly spaced on that line. Another explanation using the window + offset terminology used in the post is that the offset is a percentage of the way through the window. So, for a window of 2^x, the difference between an offset of y and y + 1 is 2^(x-23) or 1/2^(-23) of 2^x. Put another way, floating point numbers do not have absolute error like integers (each number is within 1 of a representable value), but % error (each number is within 1/2^(-23) of a representable value). Essentially, floating point numbers use % error bars instead of absolute error bars. Using this model you can even see how to create your own floating point numbers. Just pick a % precision you want, for single FP that is 1/2^(-23) and double FP 1/2^(-52), that defines the range of your mantissa (offset). Then pick a range of x values you want to represent, that is the range of your exponent (window). As an aside, subnormal numbers do not respect this principle. They extend the expressible range for very small numbers by sacrificing % error for those numbers. In the very worst case of the smallest subnormal number you can get 25% error (it might actually be 50%). As might be imagined, this plays havoc on error propagation since if you ever multiply by a number that just so happens to be the smallest subnormal, all your multiplies might suddenly be off by a factor of 25% instead of the normal 100 * 2^(-23)% which is 2,000,000 times the % error which is quite a bit harder to compensate for. This is why many people consider subnormals to be a blemish. [1] The approximation is actually offset in the x direction for the bias. If you want to be more accurate, you are actually graphing 2^(x - 127).
补充偏移指数码的设计原因:保证按位排序等价于数值排序
这是我看过的最简单的十进制转浮点数方法。他没提为什么用偏移码存指数(存 127+E):这是为了让正浮点数按整数方式排序时仍保持正确的大小顺序。
commandlinefan查看英文原文
Wow, that's a much easier way to convert from decimal to floating point than I had ever seen. He doesn't mention why biased notation is used (i.e. why the exponent is stored as 127+E): it's used so that if you sort positive numbers as if they were integers, they'll still end up in the right order.
历次讨论
同类还值得读
位运算技巧集
Bit Twiddling Hacks
斯坦福页面上的经典位运算技巧合集,收录了 popcount、前导/尾随零、位反转、交换、舍入等大量技巧,并附有解释和性能讨论。
算法与计算机科学上首页 8 次 · 2011–2024graphics.stanford.edu编辑精选如何不按平均评分排序
How Not To Sort By Average Rating
经典文章解释了为什么按平均评分排序不可靠:只有少量评价的条目(比如仅一条五星)会轻易排到最前,而评价数量的差异被忽略。
算法与计算机科学上首页 7 次 · 2009–2024evanmiller.org编辑精选数据结构可视化
Data Structure Visualizations
旧金山大学 David Galles 教授制作的数据结构与算法交互式可视化网站,涵盖 B 树、红黑树、排序、图算法等,可逐步操作观察其运行过程。
算法与计算机科学上首页 6 次 · 2011–2021cs.usfca.edu编辑精选1996 年以来的计算机科学最佳论文奖
Best Paper Awards in Computer Science since 1996
Jeff Huang 维护的一个汇总页面,收录了自 1996 年以来计算机科学各主要会议的“最佳论文奖”获奖名单,并按年份和会议整理,方便读者一站式浏览历届获奖论文。
算法与计算机科学上首页 6 次 · 2010–2018jeffhuang.com编辑精选算法(Jeff Erickson)
Algorithms, by Jeff Erickson
伊利诺伊大学 Jeff Erickson 教授的算法课教材,免费在线开放。
算法与计算机科学上首页 5 次 · 2018–2022jeffe.cs.illinois.edu编辑精选