ความแม่นยำ การปัดเศษ และข้อยกเว้นของเลขทศนิยม
ทำความเข้าใจการแทนค่าแบบ IEEE 754 โหมดการปัดเศษ และวิธีที่หน่วย FPU กับ SSE แจ้งข้อยกเว้น เช่น ค่าล้น ค่าต่ำกว่าช่วง และการดำเนินการที่ไม่ถูกต้อง
ความแม่นยำ การปัดเศษ และข้อยกเว้นของเลขทศนิยม เป็นบทเรียน Assembly Language & x86 Low-Level Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Assembly Language & x86 Low-Level Systems Programming หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Assembly Language & x86 Low-Level Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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
- ความแม่นยำ การปัดเศษ และข้อยกเว้นของเลขทศนิยม