🌊 浮点数:小数在电脑里的“科学记数法”🌊 Floating Point: decimals in scientific notation
上一片里,补码给整数安了精确的家。可 0.1 住哪儿?小电脑一算 0.1 + 0.2,居然不等于 0.3!Last leaf, two's complement gave integers an exact home. But where does 0.1 live? The little computer adds 0.1 + 0.2 — and it is NOT 0.3!
第 1 步:三块积木拼出小数Step 1: Three bricks build a decimal
小数怎么塞进 0 和 1?答案是抄作业:照着科学记数法拆成三块。How do decimals fit into zeros and ones? Copy a classic: split them into three parts like scientific notation.
浮点数 = 符号 + 尾数 + 指数。红色管正负,绿色存数字本身 3.14,蓝色记小数点搬几位。3.14 就是 +3.14×10⁰ 的二进制版。Float = sign + mantissa + exponent. Red holds plus/minus, green stores the digits 3.14, blue counts how far the point moves. So 3.14 is +3.14×10⁰, binary edition.
三块格子数得过来吗?格子不够,会发生什么?But the boxes are limited — what happens when they run out?
第 2 步:格子不够,精度打折Step 2: Too few boxes, precision suffers
0.1 化成二进制是无限循环小数,格子永远装不下,只能约等于。于是 0.1 + 0.2 = 0.30000000000000004!double 格子多一倍,误差更小,但依然是近似值。0.1 in binary repeats forever, so the boxes can only hold an approximation. Hence 0.1 + 0.2 = 0.30000000000000004! Double has twice the boxes, so a smaller mistake — still an approximation.
差一点点而已,真的重要吗?看看管钱的场景。Off by a hair — does it really matter? Ask anyone handling money.
第 3 步:钱,按“分”存整数Step 3: Money lives in integer cents
9.99 元用浮点数存,加减几次可能差 1 分,对账对到哭。正确姿势:存 999 整数分,展示时再除以 100。银行和超市都这么干。Store $9.99 as a float and a few adds can lose 1 cent — accountants cry. The right way: store 999 integer cents, divide by 100 only for display. Banks and shops all do this.
🎮 精度实验室(30 秒)🎮 Your turn: precision lab (30 seconds)
道理讲完了。先按按钮,看小电脑亲手算翻车;再过 4 关,练出火眼金睛。Theory done. First press the buttons and watch the little computer slip; then clear 4 levels and train your sharp eyes.
一句话记住它:浮点数 = 符号 + 尾数 + 指数的二进制科学记数法;格子有限所以全是近似值,钱按“分”存整数。Remember it in one line: a float is binary scientific notation — sign + mantissa + exponent; boxes are limited so all are approximations; store money as integer cents.
结构:符号管正负、尾数存数字、指数搬小数点Parts: sign for +/-, mantissa for digits, exponent moves the point陷阱:0.1 + 0.2 = 0.30000000000000004,别用 == 比小数Trap: 0.1 + 0.2 = 0.30000000000000004 — never compare decimals with ==钱:9.99 存成 999 整数分;double 比 float 准,但也是近似Money: store 9.99 as 999 integer cents; double beats float but still approximates
内容参考 OpenStax 等公开教材,多来源核对 · AI 生成、人工审核 · 发现错误欢迎指正,帮这片叶子长得更好。Based on OpenStax and other open textbooks, cross-checked across sources · AI-generated, human-reviewed · Spotted a mistake? Tell us — help this leaf grow.