浮点精度、舍入与异常
理解 IEEE 754 表示法和舍入模式,以及 FPU 和 SSE 单元如何报告溢出、下溢和无效操作等异常。
浮点精度、舍入与异常 是 CoddyKit 上的免费 Assembly Language & x86 Low-Level Systems Programming 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Assembly Language & x86 Low-Level Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Assembly Language & x86 Low-Level Systems Programming 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
IEEE 754 Format
x86 floating-point follows the IEEE 754 standard. A number is stored as sign, exponent, and mantissa:
- Single (32-bit): 1 + 8 + 23
- Double (64-bit): 1 + 11 + 52
- Extended (80-bit): used internally by the x87 FPU
Why Precision Matters
Most decimal fractions cannot be represented exactly in binary. For example 0.1 + 0.2 does not equal exactly 0.3. Accumulated rounding error is a core hazard in numerical code.
The Four Rounding Modes
IEEE 754 defines four rounding modes:
- Round to nearest, ties to even (default)
- Round toward negative infinity (floor)
- Round toward positive infinity (ceil)
- Round toward zero (truncate)
Controlling Rounding on x87
The x87 control word holds the rounding-control (RC) bits. You read it with fstcw, modify, then load with fldcw.
sub esp, 4
fstcw [esp] ; store control word
or word [esp], 0x0C00 ; RC = 11 -> round toward zero
fldcw [esp] ; load it back
add esp, 4SSE MXCSR Register
SSE has its own control/status register, MXCSR. It holds rounding-control bits plus exception masks and flags. Manage it with ldmxcsr and stmxcsr.
sub rsp, 4
stmxcsr [rsp] ; save MXCSR
ldmxcsr [rsp] ; restore MXCSR
add rsp, 4Floating-Point Exceptions
The standard defines six exceptions:
- Invalid (e.g. 0/0, sqrt of negative)
- Denormal operand
- Divide by zero
- Overflow
- Underflow
- Inexact (rounding occurred)
Masked vs Unmasked
Each exception can be masked. A masked exception produces a default result (like infinity or NaN) and sets a flag. An unmasked exception raises a CPU trap so your handler can intervene.
Special Values: NaN and Infinity
IEEE 754 reserves bit patterns for special values:
- +Inf / -Inf from overflow or divide-by-zero
- NaN (Not a Number) from invalid operations
Any comparison with NaN is false — even NaN == NaN.
Detecting NaN
Because NaN is unordered, you detect it with an unordered compare. In SSE, ucomiss sets the parity flag when an operand is NaN.
ucomiss xmm0, xmm0 ; compare value with itself
jp is_nan ; PF set => NaN detectedA Runnable C Demonstration
This self-contained C program shows that floating-point addition is not exact and detects a NaN.
#include <stdio.h>
#include <math.h>
int main(void) {
double a = 0.1 + 0.2;
printf("0.1+0.2 = %.17f\n", a);
printf("equals 0.3? %d\n", a == 0.3);
double nan_val = 0.0 / 0.0;
printf("isnan? %d\n", isnan(nan_val));
return 0;
}Practical Advice
To write robust floating-point code:
- Never compare floats with
==; use an epsilon tolerance - Be aware of accumulation order in sums
- Keep the default round-to-nearest unless you have a reason
- Check status flags after risky operations
Quick Check
Test your floating-point knowledge.
Recap
You explored floating-point behavior:
- IEEE 754 defines single, double, and 80-bit extended formats
- Four rounding modes are configured via the x87 control word or MXCSR
- Six exceptions can be masked (default result + flag) or unmasked (trap)
- NaN and infinity are special values; NaN compares false to everything
常见问题解答
「浮点精度、舍入与异常」课时是免费的吗?
是的 — 「浮点精度、舍入与异常」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Assembly Language & x86 Low-Level Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 Assembly Language & x86 Low-Level Systems Programming 课程共包含 4 节课。
「浮点精度、舍入与异常」这节课中我会学到什么?
理解 IEEE 754 表示法和舍入模式,以及 FPU 和 SSE 单元如何报告溢出、下溢和无效操作等异常。 你通过在浏览器中直接运行的动手代码来练习 Assembly Language & x86 Low-Level Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Assembly Language & x86 Low-Level Systems Programming 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Assembly Language & x86 Low-Level Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「浮点精度、舍入与异常」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Assembly Language & x86 Low-Level Systems Programming 课中编写并运行代码吗?
能。每节 Assembly Language & x86 Low-Level Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- x87 FPU 编程基础
- SSE/AVX 指令集简介
- 使用 SIMD 将代码向量化
- 浮点精度、舍入与异常