跳到主要内容

资源 · 指南或文章

Binary representation of floating-point numbers

从普通的 16 位整数讲起,解释 IEEE 754 如何用符号位、指数位和尾数位存储浮点数。适合被问到浮点数值(包括 JavaScript 数字)在内存中如何布局的候选人。

审核日期

打开原站: Binary representation of floating-point numbers (在新标签页中打开原网站)

添加到路线图

把这个资源放进你正在跟进的路线图。它会作为你自己的补充资源显示;路线图的步骤不会改变,也不计入你已记录的步骤。

来源内容语言:英语

Have you ever wondered how computers store the floating-point numbers like 3.1416 (𝝿) or 9.109 × 10⁻³¹ (the mass of the electron in kg) in the memory which is limited by a finite number of ones and zeroes (aka bits)? It seems pretty straightforward for integers (i.e. 17). Let's say we have 16 bits (2 bytes) to store the number. In 16 bits we may store the integers in a range of [0, 65535]: If we need a signed integer we may use two's complement and shift the range of [0, 65535] towards the negative numbers. In this case, our 16 bits would represent the numbers in a range of [-32768, +32767].

PrepKit 概览

本概览由 PrepKit 编辑根据源代码仓库撰写并审核,并非资源本身的文字。

适合人群
来源未说明适用级别
形式
文章

涵盖内容

  • 以无符号和二进制补码 16 位整数的取值范围作为起点
  • 半精度、单精度和双精度的位划分表:1 位符号位,加上 5、8 或 11 位指数和 10、23 或 52 位尾数
  • 偏置指数及其公式 2^(k-1) - 1,并用一张图解码一个半精度数
  • 半精度和单精度的取值范围和最小正数表
  • 链接到该目录下的 bitsToFloat.js 和 floatAsBinaryString.js 示例

使用建议

这是 PrepKit 的学习建议,并非来源的说法。

选几个十进制数(如 -27.15625),手动写出它们的半精度位表示,再用 floatAsBinaryString 或链接的可视化工具核对。目标是能用一个例子大声解释指数偏置。

范围与局限

作者称之为简化概述:没有涉及 -0、无穷大和 NaN,也没有讲解算术运算中的舍入或精度损失。解码过程是一张图片,代码为 JavaScript。

获取方式与形式

GitHub 上采用 MIT 许可的 trekhleb/javascript-algorithms 仓库中的 README;英文,免费阅读。附有作者博客上交互版本的链接。

PrepKit 于 2026年10月2日 审核。 资源来源提交: 85293e3e2b88

本概览的依据
  • src/algorithms/math/binary-floating-point/README.md

收录此条目的页面

订阅 PrepKit 通讯

可选。使用 PrepKit 并不需要订阅。

我们如何处理你的邮箱