เวลา/ตัวเลข
ตัวแปลง Float IEEE 754
เลขฐานสิบ ↔ การแสดงผลแบบไบนารีตาม IEEE 754 (ความละเอียดเดี่ยวหรือคู่) — เครื่องหมาย เลขชี้กำลัง แมนทิสซา ค่าพิเศษ
Computers store fractional numbers in binary under the IEEE 754 standard — a sign, an exponent, and a mantissa, packed into a fixed number of bits. This tool shows that representation for any decimal number and parses it back into a number.
How to use it
- Number → bits: enter a decimal number to get its breakdown into sign, exponent, and mantissa for single (32-bit) or double (64-bit) precision.
- Bits → number: paste a bit representation to get the decimal value back.
- Special values (infinity, NaN, signed zero) are recognized and explained separately.
Common uses
- Understanding why 0.1 + 0.2 doesn't exactly equal 0.3 in most programming languages — a concrete demonstration of rounding error.
- Studying float's internal representation to understand precision limits in financial or scientific calculations.
- Parsing a binary dump or network packet where numbers are transmitted in IEEE 754 format.
Things to keep in mind
Most decimal fractions (0.1, for example) have no exact binary representation — that's the root cause of the typical rounding errors seen in calculation results.
Comparing floating-point numbers for exact equality (===) is unreliable — check instead whether the difference is smaller than a small threshold (epsilon).
บทความเกี่ยวกับเครื่องมือนี้: IEEE 754: เหตุใด 0.1 + 0.2 จึงไม่เท่ากับ 0.3 ในโค้ด
คำถามที่พบบ่อย
ความแม่นยำเดี่ยวกับความแม่นยำคู่ต่างกันอย่างไร?
ความแม่นยำเดี่ยว (float, 32 บิต) ให้ความแม่นยำประมาณ 7 หลักทศนิยม ความแม่นยำคู่ (double, 64 บิต) ให้ประมาณ 15-17 หลัก ชนิด Number ของ JavaScript ใช้ความแม่นยำคู่
ทำไมเลขทศนิยมบางตัวถึงสูญเสียความแม่นยำ?
IEEE 754 เก็บตัวเลขในรูปแบบไบนารี และเศษส่วนทศนิยมจำนวนมาก (เช่น 0.1) ไม่มีการแทนค่าไบนารีจำกัดที่แม่นยำ — จึงเก็บค่าที่ใกล้เคียงที่สุดเท่าที่เป็นไปได้พร้อมข้อผิดพลาดจากการปัดเศษเล็กน้อย
NaN, Infinity และ -0 หมายถึงอะไร?
NaN คือผลลัพธ์ของการดำเนินการที่ไม่ถูกต้อง เช่น 0/0 Infinity/-Infinity ปรากฏเมื่อค่าเกินช่วงที่แทนค่าได้ (เช่น หารด้วย 0) +0 และ -0 คือการแทนบิตที่ต่างกันสองแบบของศูนย์แบบมีเครื่องหมาย ซึ่งยังคงเท่ากันเมื่อเป็นตัวเลข
เลขซับนอร์มัล (denormalized) คืออะไร?
เป็นโหมดการแสดงผลพิเศษสำหรับตัวเลขที่มีขนาดเล็กเกินไปสำหรับรูปแบบปกติ — เลข 1 นำหน้าโดยนัยของแมนทิสซาจะถูกละทิ้ง ทำให้สามารถเข้าใกล้ศูนย์ได้ทีละน้อยโดยแลกกับความละเอียดบางส่วน
จะเปรียบเทียบตัวเลขทศนิยมอย่างถูกต้องได้อย่างไรแทนการตรวจสอบความเท่ากันแบบแน่นอน?
แทนที่จะใช้ a === b ให้ตรวจสอบว่าผลต่างระหว่างตัวเลขน้อยกว่าค่าขีดจำกัดเล็ก ๆ (epsilon) หรือไม่: Math.abs(a - b) < 1e-9 ความเท่ากันแบบแน่นอนแทบไม่มีการรับประกันเลยเนื่องจากข้อผิดพลาดจากการปัดเศษ