Floating-point numbers
แนะนำใหม่
15:07
ประเภทข้อมูลที่กำหนดโดยผู้ใช้
ฟิลด์ข้อความธรรมดาจะเก็บข้อมูลไร้สาระได้อย่างสบายใจ ขอชนิดยานพาหนะมา คนหนึ่งพิมพ์ "กล้วย" — โปรแกรมรับไว้โดยไม่บ่น แต่หากคุณ…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| binary representation | A way of showing a number using only the digits zero and one. |
| repeating fraction | A fraction whose digits continue forever in a repeating pattern. |
| truncated/ˈtrʌŋkeɪtɪd/ | Describes a number shortened by removing digits without rounding. |
| register's | Belonging to a small storage location inside the processor. |
| accumulated error | The total difference from the correct value after small errors build up. |
| floating-point/ˈfləʊtɪŋ pɔɪnt/ | จุดทศนิยมลอยตัว |
| normalisation/ˌnɔːməlaɪˈzeɪʃn/ | The process of adjusting a mantissa and exponent to use available bits efficiently. |
| approximation errors | Differences between exact values and the close values used to represent them. |
| real numbers | All numbers that can be placed on a continuous number line. |
| scientific notation | A way to write a number as a value multiplied by a power of ten. |
| fields/fiːldz/ | Parts of a database record that each hold one item of data. |
| mantissa/mænˈtɪsə/ | ส่วนเศษ |
| significant digits | Digits that give useful information about the precision of a number. |
| exponent/ekˈspəʊnənt/ | เลขชี้กำลัง |
| two's complement/tuːz ˈkɒmplɪmənt/ | A signed binary integer representation in which a negative value is formed by inverting the positive value's bits and adding one within a fixed bit width. |
| binary fraction | A number with binary digits after a binary point. |
| bit/bɪt/ | A binary digit, taking the value zero or one. |
| denary/ˈdiːnəri/ | The base-ten number system, using the digits zero through nine. |
| sign bit/saɪn bɪt/ | The bit in a signed-number representation that indicates or contributes the number's sign. |
| normalised/ˈnɔːməlaɪzd/ | มาตรฐานรูป |
| significant bit | A bit that gives useful information about the value of a number. |
| binary point | The point separating whole-number bits from fractional bits in a binary number. |
| leading zeros | Zeros placed before the first non-zero digit to give a number the required width. |
| maximises precision | Makes a stored value as exact as the available bits allow. |
| precision/prɪˈsɪʒn/ | ความแม่นยำ |
| range/reɪndʒ/ | The set of values that can be represented in a given format. |
| Rounding errors/ˈraʊndɪŋ ˈerəz/ | Differences caused by replacing exact values with nearby values that can be stored. |
| fixed-point/fɪkst pɔɪnt/ | จุดทศนิยมคงที่ |
| BCD/ˌbiː siː ˈdiː/ | A way of storing each denary digit separately using four binary digits. |
| Overflow/ˌəʊvəˈfləʊ/ | The condition when a result is too large in magnitude to be represented. |
| Underflow/ˌʌndəˈfləʊ/ | The condition when a nonzero result is too close to zero to be represented. |
| integer/ˈɪntɪdʒə/ | A whole number that may be positive, negative or zero. |
| tolerance/ˈtɒlərəns/ | The largest acceptable difference when comparing two values for near equality. |
ขีปนาวุธที่พลาดเป้าหมาย 600 เมตร
- On 25 February 1991 a Patriot battery at Dhahran failed to intercept an incoming missile. Twenty-eight soldiers died.
- ระบบนับเวลาเป็นเศษส่วน tenth of a second, และ 0.1 ไม่มีรูปแบบไบนารีที่แม่นยำ: มันคือเศษส่วนซ้ำๆ ที่ถูกตัดทอนให้พอดีกับความกว้างของรีจิสเตอร์ ทุกรอบสัญญาณเสียเศษส่วนไมโครวินาที
- หลังจากรันนานร้อยชั่วโมง ความคลาดเคลื่อนสะสมมีเท่ากับหนึ่งในสามวินาที และในหนึ่ง_third วินาที เป้าหมายเคลื่อนที่ 600 เมตร การคำนวณถูกต้องในทุกขั้นตอน
- บทเรียนนี้คือรูปแบบ floating-point, normalisation, และความคลาดเคลื่อนจากการประมาณที่ทำให้กรณีเช่นนี้เป็นไปได้

รูปแบบ
- 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.
- อ่าน mantissa เป็นเศษส่วน binary: บิตแรกหลังจุดมีค่า $\tfrac12$, บิตถัดไป $\tfrac14$, ตามด้วย $\tfrac18$ ดังนั้น
0.1010000คือ $\tfrac12 + \tfrac18 = 0.625$ - ด้วย exponent
00000010, นั่นคือ $+2$, ค่าที่ได้คือ $0.625 \times 2^2 = 2.5$

เศษส่วนและเลขชี้กำลังของสอง
Build a floating-point number
Flip the mantissa and exponent bits to make a value, and check whether it is normalised.
A floating-point number is stored as:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
value = mantissa × 2^exponent — binary scientific notation, with both parts stored in two's complement.
Match each part of floating-point representation to its role.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
value = mantissa × 2^exponent; normalising maximises precision; money uses fixed-point/BCD to avoid rounding.
ตัวอย่างทำแบบฝึกหัด: ไบนารีเป็นเดนิอารี่
- A number has mantissa
10110000and exponent00000011. Find its denary value. - เลขชี้กำลังคือ $+3$. ส่วนเศษเริ่มด้วย 1 ดังนั้นจึงเป็น ลบ และอ่านตามกฎ complement ของสอง: เมื่อเป็น
1.0110000บิตสัญลักษณ์มีค่าเท่ากับ $-1$ และบิตเศษบวกเพิ่ม $\tfrac14 + \tfrac18 = 0.375$ ดังนั้นส่วนเศษจึงมีค่า $-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.

A floating-point number has mantissa 10110000 and exponent 00000011. What is its denary value?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The mantissa starts with 1, so it is negative: $-1 + \dfrac{1}{4} + \dfrac{1}{8} = -0.625$. Times $2^{3}$ gives $-5.0$. Reading it as positive is the usual error.
ตัวอย่างวิธีทำ: จากระบบทศนิยมไปเป็นระบบทวิภาค
- จัดเก็บ $+2.5$ ในรูปแบบส่วนเศษ 8 บิต เลขชี้กำลัง 8 บิต
- ในระบบทวิภาค $2.5 = 10.1$ เขียนในรูปเศษคูณด้วยกำลังของสอง: $2.5 = 0.101 \times 2^2$
- ดังนั้นส่วนเศษคือ
01010000มีบิตสัญลักษณ์เป็น 0 ตามด้วย.101เติมด้วยเลขศูนย์ และเลขชี้กำลังคือ00000010 - ต้องเขียนให้อยู่ในรูปแบบปกติก่อน แล้วข้อมูลทั้งสองส่วนจะอ่านออกได้ทันที

Written as a normalised fraction times a power of two, $2.5 = 0.101 \times 2^{n}$ (the fraction in binary). The exponent $n$ is ____.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$2.5$ เป็น $10.1$ in binary; sliding the point two places left gives $0.101$, so the exponent is $2$ and the mantissa is 01010000.
การทำให้เป็นมาตรฐาน (Normalisation)
- จำนวนหนึ่งถูกเรียกว่า ปกติ เมื่อบิตที่มีนัยสำคัญตัวแรกอยู่ ติดกับจุดทวิภาค ทันที จึงไม่มีเลขศูนย์นำที่ไม่จำเป็น สำหรับจำนวนบวกส่วนเศษเริ่ม
0.1; สำหรับจำนวนลบ เริ่ม1.0 - เหตุผล: เพื่อ ความแม่นยำสูงสุด เพราะทุกบิตในส่วนเศษจะถือข้อมูลไว้แทนที่จะเป็นเลขศูนย์นำ
- การทำให้เป็นปกติคือการเลื่อนส่วนเศษไปทางซ้ายและ ลด เลขชี้กำลังลงเท่าจำนวนตำแหน่งที่เลื่อน หรือเลื่อนไปทางขวาและเพิ่มมันขึ้น ค่าไม่เปลี่ยน มีเพียงการแสดงผลเท่านั้น

เลื่อนบิต ปรับเลขชี้กำลัง รักษาค่าเดิมไว้
การแลกเปลี่ยนบิตส่วนเศษกับบิตเลขชี้กำลัง
- จำนวนบิตทั้งหมดมีจำกัด ดังนั้นสองส่วนนี้จึงแข่งขันกัน
- ส่วนเศษมากขึ้น หมายถึง ความแม่นยำสูงขึ้น: แต่ละค่าถูกจัดเก็บแม่นยำขึ้น ด้วยข้อผิดพลาดในการปัดเศษที่น้อยลง
- เลขชี้กำลังมากขึ้น หมายถึง ช่วงค่ากว้างขึ้น: สามารถแสดงขนาดที่มากและน้อยมากได้ แต่แต่ละค่ามีความแม่นยำน้อยลง
- คำถามที่ถามถึงผลกระทบของการย้ายบิตจากส่วนหนึ่งไปยังอีกส่วนหนึ่ง ต้องการคำตอบเรื่องการแลกเปลี่ยนนี้: ช่วงค่าเทียบกับความแม่นยำ
Normalising a floating-point number
Step through normalisation. Shifting the mantissa to remove wasted leading zeros — and adjusting the exponent to match — keeps the value the same but spends every bit on precision.
Normalising a floating-point number:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Normalisation shifts the mantissa so the first significant bit follows the point — every bit then carries information, and the value is unchanged.
Which are true of normalising a floating-point number? Select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Normalising changes only the representation. The value is unchanged; that is what adjusting the exponent guarantees.
การประมาณค่าและผลกระทบ
- จำนวนจริงหลายตัว ไม่สามารถจัดเก็บ Exact ได้ ในระบบทวิภาค $0.1$ เป็นเศษซ้ำ $0.000110011\ldots$ จึงต้องตัดทอน: ค่าที่จัดเก็บจะใกล้เคียงกับ $0.1$ แต่ไม่เคยเท่ากับมัน
- ข้อผิดพลาดจากการปัดเศษ จะสะสมเมื่อผ่านกระบวนการคำนวณจำนวนมาก นี่คือเหตุผลที่
0.1 + 0.2ไม่เท่ากับ0.3อย่างแท้จริง และทำไมนาฬิกาของ Patriot ถึงเดินผิดเวลา - 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. - สำหรับค่าที่ต้อง Exact โดยเฉพาะเงิน ให้ใช้ fixed-point หรือ BCD แทน
Match each change in the bit allocation to its effect.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The word size is fixed, so precision and range trade against each other; overflow and underflow are the exponent field running out at each end.
Overflow และ underflow
- Overflow เกิดขึ้นเมื่อผลลัพธ์ ใหญ่เกินไป สำหรับช่วงของเลขชี้กำลัง จึงไม่สามารถแสดงได้เลย
- Underflow เกิดขึ้นเมื่อผลลัพธ์ เล็กเกินไป จนใกล้ศูนย์มากจนเลขชี้กำลังไม่สามารถลดลงได้อีก และถูกปัดเศษให้เป็นศูนย์
- ทั้งสองเป็นคุณสมบัติของ ขนาดของช่องเลขชี้กำลัง ซึ่งเป็นอีกครึ่งหนึ่งของสมการแลกเปลี่ยนข้างต้น

0.1 cannot be stored exactly in binary (it is a repeating fraction), so 0.1 + 0.2 does not give exactly 0.3 on a computer.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The tiny approximation errors add up — which is why money is handled with fixed-point or BCD, not floating-point.
For exact money calculations you should use:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Floating-point rounding errors are unacceptable for currency; decimal fixed-point or an appropriate BCD representation can store amounts exactly to the chosen decimal scale.
คะแนนที่หลุดหายไป
- ส่วนเศษคือ เศษส่วน ไม่ใช่จำนวนเต็ม: บิตแรกหลังจุดคือครึ่งหนึ่ง
- ส่วนเศษที่เริ่มด้วย 1 คือ ลบ และปฏิบัติตามกฎ complement ของสอง ตรวจสอบบิตนี้ก่อนเสมอ
- การทำให้เป็นปกติ เพิ่มความแม่นยำ; ไม่ได้เปลี่ยนค่า และไม่ทำให้จำนวนมีขนาดใหญ่ขึ้น
- ส่วนเศษมากขึ้น คือความแม่นยำ, เลขชี้กำลังมากขึ้น คือช่วงค่า, Overflow คือใหญ่เกินไป, Underflow คือเล็กเกินไป
คุณเข้าใจแล้ว
- floating point จัดเก็บ $\text{mantissa} \times 2^{\text{exponent}}$, ทั้งสองอยู่ในรูป complement ของสอง; ส่วนเศษคือเศษส่วนทวิภาค
- แปลงโดยการอ่านส่วนเศษเป็นเศษส่วน (complement ของสองถ้าเริ่มด้วย 1) คูณด้วยสองยกกำลังเลขชี้กำลัง
- normalised หมายถึงบิตที่มีนัยสำคัญตัวแรกอยู่ติดกับจุด ซึ่ง เพิ่มความแม่นยำ; การเลื่อนไปทางซ้ายลดเลขชี้กำลังลง
- บิตส่วนเศษมากขึ้นให้ความ ความแม่นยำ, บิตเลขชี้กำลังมากขึ้นให้ ช่วง; ระบบทวิภาคไม่สามารถจัดเก็บจำนวนจริงหลายตัวได้ Exactly ดังนั้นควรคาดหวัง ข้อผิดพลาดจากการปัดเศษ, เปรียบเทียบด้วยค่าความคลาดเคลื่อน, และใช้ fixed-point หรือ BCDสำหรับเงิน
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้