จำนวนทศนิยม (Floating-point numbers)
| English | ไทย |
|---|---|
| floating-point/ˈfləʊtɪŋ pɔɪnt/ | จุดลอย |
| mantissa/mænˈtɪsə/ | mantissa |
| exponent/ekˈspəʊnənt/ | เลขชี้กำลัง |
| normalised/ˈnɔːməlaɪzd/ | ปรับมาตรฐานแล้ว |
| precision/prɪˈsɪʒn/ | ความละเอียด |
| rounding errors/ˈraʊndɪŋ ˈerəz/ | ข้อผิดพลาดจากการปัดเศษ |
| fixed-point/fɪkst pɔɪnt/ | จุดคงที่ |
| overflow/ˌəʊvəˈfləʊ/ | overflow |
| underflow/ˌʌndəˈfləʊ/ | Underflow |
A missile that missed by 600 metres
- On 25 February 1991 a Patriot battery at Dhahran failed to intercept an incoming missile. Twenty-eight soldiers died.
- The system counted time in tenths of a second, and 0.1 has no exact binary representation: it is a repeating fraction, truncated to the register's width. Each tick lost a fraction of a microsecond.
- After a hundred hours running, the accumulated error was a third of a second, and in a third of a second the target moved 600 metres. The arithmetic was correct at every single step.
- This lesson is the floating-point 浮点 format, normalisation 规格化, and the approximation errors that make cases like this possible.
The format
- To store real numbers of very different sizes, a computer uses a binary form of scientific notation with two fields: a mantissa 尾数, the significant digits, and an exponent 指数, the power of 2 to multiply by. Both are stored as two's complement.
- Read the mantissa as a binary fraction: the first bit after the point is worth $\tfrac12$, the next $\tfrac14$, then $\tfrac18$. So
0.1010000is $\tfrac12 + \tfrac18 = 0.625$. - With exponent
00000010, that is $+2$, the value is $0.625 \times 2^2 = 2.5$.

A fraction and a power of two
สร้างจำนวนทศนิยม
กลับด้านบิตของmantissaและexponentเพื่อสร้างค่า และตรวจสอบว่ามันเป็นnormalisedหรือไม่
จำนวนทศนิยมถูกจัดเก็บเป็น:
value = mantissa × 2^exponent — ระบบเลขสองฐานแบบวิทยาศาสตร์ โดยเก็บทั้งสองส่วนในรูปสองส่วนเสริม (two's complement)
จับคู่แต่ละส่วนของรูปแบบทศนิยมเข้ากับบทบาทของมัน
value = mantissa × 2^exponent; normalising เพิ่มความละเอียด; เงินใช้ fixed-point/BCD เพื่อหลีกเลี่ยงการปัดเศษ
Worked example: binary to denary
- A number has mantissa
10110000and exponent00000011. Find its denary value. - The exponent is $+3$. The mantissa begins with 1, so it is negative and is read by two's complement rules: as
1.0110000the sign bit is worth $-1$ and the fraction bits add $\tfrac14 + \tfrac18 = 0.375$, so the mantissa is $-1 + 0.375 = -0.625$. - $\text{number} = -0.625 \times 2^3 = -5.0$.
- The commonest error is reading a negative mantissa as if it were positive. Check the first bit before anything else.
จำนวนทศนิยมมี mantissa 10110000 และ exponent 00000011 ค่าฐานสิบของมันคืออะไร?
mantissaเริ่มด้วย 1 ดังนั้นเป็นลบ: −1 + 1/4 + 1/8 = −0.625 คูณด้วย 2^3 ได้ −5.0 การอ่านมันเป็นบวกคือข้อผิดพลาดทั่วไป
Worked example: denary to binary
- Store $+2.5$ in the same 8-bit mantissa, 8-bit exponent format.
- In binary, $2.5 = 10.1$. Written as a fraction times a power of two: $2.5 = 0.101 \times 2^2$.
- So the mantissa is
01010000, a sign bit of 0 then.101padded with zeros, and the exponent is00000010. - Always write it in the normalised form first; then the two fields read straight off.
เขียนในรูปแบบเศษส่วนnormalised คูณด้วยกำลังของสอง 2.5 = 0.101 x 2^__. ให้ค่าexponent
2.5 คือ 10.1 ในระบบเลขสองฐาน; เลื่อนจุดไปทางซ้ายสองตำแหน่งจะได้ 0.101 ดังนั้นexponentคือ 2 และmantissaคือ 01010000
Normalisation
- A number is normalised when the first significant bit sits immediately after the binary point, so there are no wasted leading zeros. For a positive number the mantissa starts
0.1; for a negative one,1.0. - Why: it maximises precision 精度, because every mantissa bit then carries information rather than a leading zero.
- To normalise, shift the mantissa left and decrease the exponent by the same number of places, or shift right and increase it. The value is unchanged; only its representation is.

Shift the bits, adjust the exponent, keep the value
Trading mantissa bits against exponent bits
- The total number of bits is fixed, so the two fields compete.
- More mantissa bits means greater precision: each value is stored more exactly, with a smaller rounding error.
- More exponent bits means greater range: much larger and much smaller magnitudes can be represented, but each one less precisely.
- A question asking for the effect of moving a bit from one field to the other wants exactly this trade: range against precision.
การทำnormaliseจำนวนทศนิยม
ผ่านขั้นตอนnormalisation การเลื่อนmantissaเพื่อกำจัดศูนย์นำหน้าที่ไม่จำเป็น — และการปรับexponentให้สอดคล้อง — รักษาค่าเดิมแต่ใช้ทุกบิตเพื่อความละเอียดสูงสุด
การทำnormaliseจำนวนทศนิยม:
Normalisation เลื่อนmantissaเพื่อให้บิตที่มีนัยสำคัญตัวแรกอยู่หลังจุด — ทุกบิตจะมีข้อมูล และค่าไม่เปลี่ยนแปลง
ข้อใดต่อไปนี้เป็นความจริงเกี่ยวกับการnormaliseจำนวนทศนิยม? เลือก ทั้งหมด ที่เหมาะสม
Normalisation เปลี่ยนเฉพาะรูปแบบ ค่าไม่เปลี่ยนแปลง นั่นคือสิ่งที่การปรับexponentรับประกัน
Approximation and its consequences
- Many denary reals cannot be stored exactly in binary. $0.1$ is the repeating fraction $0.000110011\ldots$, so it must be truncated: the stored value is close to $0.1$ but never equal to it.
- Rounding errors 舍入误差 accumulate over many operations, which is why
0.1 + 0.2is not exactly0.3and why the Patriot's clock drifted. - So never test two reals for equality: write
ABS(x - 0.3) < 1e-9instead ofx = 0.3. And beware subtracting two nearly equal values, which throws away most of the significant digits. - For values that must be exact, currency above all, use fixed-point 定点 or BCD instead.
จับคู่การเปลี่ยนแปลงในการจัดสรรบิตแต่ละอันกับผลกระทบของมัน
ขนาดคำคงที่ ดังนั้นความละเอียดและช่วงจึงแลกเปลี่ยนกัน; Overflowและunderflowคือสนามexhaustionของexponent field ที่ปลายแต่ละด้าน
Overflow and underflow
- Overflow 溢出 happens when a result is too large for the exponent's range, so it cannot be represented at all.
- Underflow 下溢 happens when a result is too small, so close to zero that the exponent cannot go low enough, and it rounds to zero.
- Both are properties of the exponent field's size, which is the other half of the trade-off above.
0.1 ไม่สามารถเก็บExactใน binary (มันเป็นเศษส่วนซ้ำ), ดังนั้น 0.1 + 0.2 จึงไม่ได้ 0.3 Exactly บนคอมพิวเตอร์
ความคลาดเคลื่อนของการประมาณค่าเล็กๆ บวกกัน — นั่นคือเหตุผลที่เงินถูกจัดการด้วยfixed-pointหรือBCD ไม่ใช่floating-point
สำหรับการคำนวณเงินที่แม่นยำคุณควรใช้:
ข้อผิดพลาดจากการปัดเศษแบบทศนิยมลอยจุดไม่ยอมรับได้สำหรับเงิน; การเก็บข้อมูลแบบจุดคงที่หรือ BCD จะเก็บค่าทศนิยมได้อย่างแม่นยำ
Marks that slip away
- The mantissa is a fraction, not an integer: the first bit after the point is a half.
- A mantissa starting with 1 is negative and follows two's complement rules. Check that bit first.
- Normalisation maximises precision; it does not change the value, and it does not make the number bigger.
- More mantissa means precision, more exponent means range. Overflow is too big, underflow is too small.
You've got it
- floating point stores $\text{mantissa} \times 2^{\text{exponent}}$, both in two's complement; the mantissa is a binary fraction
- convert by reading the mantissa as a fraction (two's complement if it starts with 1) and multiplying by two to the exponent
- normalised means the first significant bit is immediately after the point, which maximises precision; shifting left lowers the exponent
- more mantissa bits give precision, more exponent bits give range; binary cannot store many reals exactly, so expect rounding errors, compare with a tolerance, and use fixed-point or BCD for currency