算法与计算机科学evanw.github.io
Float Toy:交互式浮点数可视化讲解
Float Toy
2020 — 2022,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
Float Toy 是一个交互式可视化工具,用可操作的二进制位展示 IEEE 754 浮点数的符号位、指数位和尾数位如何组合成具体数值。它把浮点数的编码方式和特殊值变得直观,适合用来建立对浮点格式的直觉。
为什么经久不衰:因为它把抽象枯燥的位布局变成可动手探索的演示,是理解浮点数表示与精度问题的经典入口。
HN 评论精选
用二进有理数 a/2^b 理解尾数和指数,能直观看出哪些数无法精确表示。
让我真正理解浮点数的是二进有理数(dyadic rational)的概念。它们就是分母为 2 的幂的有理数(如 1/2、3/16、61/32)。二进制浮点数本质上表示的就是二进有理数(不算 NaN、无穷等特殊值)。要把这种数写成浮点形式,只需写成 a/2^b,那么尾数是 a,指数是 -b。这就直观说明了为什么 1/3 无法表示。我觉得 a/b 这种有理数形式比 a×2^b 这种“科学计数法”形式更好懂。
pcwalton查看英文原文
The thing that made floating point easy to understand for me was the concept of a dyadic rational [1]. Those are just the rational numbers whose denominator is a power of two (e.g. 1/2, 3/16, 61/32). Fundamentally, binary floating point represents dyadic rationals (not counting special stuff like NaN, infinity, etc.) To represent such a number in floating point form, just write it as a/2^b. Then the mantissa is a, and the exponent is -b. This intuitively makes it obvious why, for example, 1/3 can't be represented. I find the rational form like a/b easier to understand than the "scientific notation" form like a×2^b. [1]: https://en.wikipedia.org/wiki/Dyadic_rational
浮点位模式按符号-幅值整数解读时具有单调排序,便于调试。
大约 25 年前 Win32 刚出现时我写过类似工具,今天偶尔调试浮点代码还会用。IEEE754 一个有趣性质:若把位模式当作符号-幅值整数来解释,浮点值会自然排序:0、最小正数……最大有限值、Infinity、NaN。也就是说,按浮点位模式递增,数值从 0 开始以越来越大的步长上升,最大有限值之后是 Infinity,再接所有 NaN;负数侧是镜像,只是最高符号位置 1。
userbinator查看英文原文
I wrote something similar ~25 years ago, when Win32 first appeared, and still use it today on the (relatively rare) occasion that I need to debug something that uses floating-point. One interesting thing you'll notice about IEEE754 is that the values sort naturally if you interpret them as sign-magnitude integers: 00000000 0 00000001 1.4e-45 (smallest representable positive value) 00000002 2.8e-45 (next-smallest representable positive value) 007fffff 1.1754942e-38 00800000 1.17549435e-38 3f000000 0.5 3f800000 1. 3fffffff 1.9999999 40000000 2. 40400000 3. 60000000 3.6893488e19 7f000000 1.7014118e38 7f7fffff 3.4028235e38 (largest finite value) 7f800000 Infinity 7f800001 first NaN 7fffffff last NaN In other words, if you count up in floats, it starts at 0 and goes up by increasingly larger amounts, then the number right "after" the "largest" is Infinity, followed by all the NaNs to the end of the range. The negative side is a "mirror image", the only difference being the highest (sign) bit is set.
半精度位数少,尾数对应 x/1024,更适合初学者建立直觉。
我觉得看 16 位浮点数更容易理解浮点。数值范围小,脑子里更好摆弄。当你只盯着 10 位尾数时,分数部分字面上就是 x/1024 这件事突然就清楚了。
seanalltogether查看英文原文
I feel like I groked floating point numbers easier when looking at 16bit floating point numbers. The values are much smaller and easier to juggle in your head. The fact that the fraction part literally translated to x / 1024 suddenly became clear to me when you're staring at just 10 bits https://en.wikipedia.org/wiki/Half-precision_floating-point_format#Half_precision_examples
建议工具把尾数显式展开为 1 + fraction/2^n,计算过程更清楚。
有一个我很久才搞懂、希望这个玩具能展示的点:尾数是如何参与计算的。现在这些位 0 10001 1000000000 显示为 1 x 2^2 x 1.5 = 6,而更清楚的说法是 1 x 2^2 x (1 + (512 / 1024)) = 6。
seanalltogether查看英文原文
One thing that took me a long time to grok that I wish this toy showed is how the fraction is included in the calculation underneath. Right now the following bits 0 10001 1000000000 show the calculation as 1 x 2^2 x 1.5 = 6 when it's more clear to actually say 1 x 2^2 x (1 + (512 / 1024)) = 6
历次讨论
同类还值得读
位运算技巧集
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编辑精选