You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 15.-floating-point-arithmetic-issues-and-limitations.md
+14-14
Original file line number
Diff line number
Diff line change
@@ -156,80 +156,80 @@ True
156
156
157
157
## 15.1. Lỗi sai số
158
158
159
-
This section explains the “0.1” example in detail, and shows how you can perform an exact analysis of cases like this yourself. Basic familiarity with binary floating-point representation is assumed.
159
+
Đây là phần diễn giải về “0.1” và chỉ ra cho bạn cách làm những trường hợp tương tự. Phần này đòi hỏi những kiến thức cơ bản về số hữu tỉ được biểu diễn dưới dạng phân số nhị phân.
160
160
161
-
_Representation error_ refers to the fact that some \(most, actually\)decimal fractions cannot be represented exactly as binary \(base 2\) fractions. This is the chief reason why Python \(or Perl, C, C++, Java, Fortran, and many others\)often won’t display the exact decimal number you expect.
161
+
_Lỗi sai số_ dùng để chỉ về vấn đề rằng có một số \(hoặc thực tế là gần hết\)các số hữu tỉ thập phân không thể thể hiện chính xác ở dạng số hữu tỉ nhị phân. Đây là nguyên nhân chính vì sao Python \(hay Perl, C, C++, Java, Fortran, cùng rất nhiều ngôn ngữ lập trình khác\)thường không thể hiện chính xác được con số hữu tỉ mà bạn đang mong muốn.
162
162
163
-
Why is that? 1/10 is not exactly representable as a binary fraction. Almost all machines today \(November 2000\)use IEEE-754 floating point arithmetic, and almost all platforms map Python floats to IEEE-754 “double precision”. 754 doubles contain 53 bits of precision, so on input the computer strives to convert 0.1 to the closest fraction it can of the form_J_/2\*\*_N_where _J_is an integer containing exactly 53 bits. Rewriting
163
+
Vì sao lại thế? 1/10 không được thể hiện chính xác dưới dạng số hữu tỉ nhị phân. Hầu hết tất cả các máy tính ngày nay \(tính đến tháng 11 năm 2000\)sử dụng thuật toán số hữu tỉ IEEE-754, mà hầu hết hệ thống có sử dụng Python đều sử dụng chuẩn IEEE-754 “double precision” cho số hữu tỉ. 754 double precision bao gồm 53 bit thể hiện độ chính xác, và hướng máy tính đạt được con số 0.1 bằng một phân số nhị phân gần nhất dạng_J_/2\*\*_N_trong đó _J_là một số tự nhiên được thể hiện chính xác bằng 53 bit. Ta có thể viết:
164
164
165
165
```text
166
166
1 / 10 ~= J / (2**N)
167
167
```
168
168
169
-
as
169
+
lại là:
170
170
171
171
```text
172
172
J ~= 2**N / 10
173
173
```
174
174
175
-
and recalling that _J_ has exactly 53 bits \(is `>= 2**52`but`< 2**53`\), the best value for _N_is 56:>>>
175
+
đừng quên là ta sử dụng chính xác 53 bit để biểu diễn _J_\(`>= 2**52`nhưng`< 2**53`\), nên giá trị tốt nhất cho _N_là 56:>>>
176
176
177
177
```text
178
178
>>> 2**52 <= 2**56 // 10 < 2**53
179
179
True
180
180
```
181
181
182
-
That is, 56 is the only value for _N_that leaves _J_with exactly 53 bits. The best possible value for_J_is then that quotient rounded:>>>
182
+
Thế nên, 56 là giá trị tốt nhất với _N_để có thể biểu diễn _J_với chính xác 53 bit. Giá trị tốt nhất cho_J_được làm tròn:>>>
183
183
184
184
```text
185
185
>>> q, r = divmod(2**56, 10)
186
186
>>> r
187
187
6
188
188
```
189
189
190
-
Since the remainder is more than half of 10, the best approximation is obtained by rounding up:>>>
190
+
Vì phần dư lớn hơn 5, cách làm tròn tốt nhất là làm tròn lên:>>>
191
191
192
192
```text
193
193
>>> q+1
194
194
7205759403792794
195
195
```
196
196
197
-
Therefore the best possible approximation to 1/10 in 754 double precision is:
197
+
Vì thế, giá trị gần đúng nhất với 1/10 theo chuẩn 754 double precision là:
198
198
199
199
```text
200
200
7205759403792794 / 2 ** 56
201
201
```
202
202
203
-
Dividing both the numerator and denominator by two reduces the fraction to:
203
+
Chia cả tử số và mẫu số cho 2 ta có được một phân số:
204
204
205
205
```text
206
206
3602879701896397 / 2 ** 55
207
207
```
208
208
209
-
Note that since we rounded up, this is actually a little bit larger than 1/10; if we had not rounded up, the quotient would have been a little bit smaller than 1/10. But in no case can it be _exactly_1/10!
209
+
Lưu ý là vì chúng ta làm tròn nó, con số này có thể hơi lớn hơn 1/10; nếu ta không làm tròn, con số đó có thể hơi nhỏ hơn 1/10. Nhưng không có cách nào để nó _chính xác_ là 1/10 cả!
210
210
211
-
So the computer never “sees” 1/10: what it sees is the exact fraction given above, the best 754 double approximation it can get:>>>
211
+
Thế nên máy tính không bao giờ hiểu 1/10: những gì máy tính biết là phân số nhị phân được viết bên trên, giá trị gần đúng nhất mà nó có thể đạt được:>>>
212
212
213
213
```text
214
214
>>> 0.1 * 2 ** 55
215
215
3602879701896397.0
216
216
```
217
217
218
-
If we multiply that fraction by 10\*\*55, we can see the value out to 55 decimal digits:>>>
218
+
Nếu ta nhân con số trên với 10\*\*55, ta có thể thấy kết quả bao gồm 55 chữ số:>>>
meaning that the exact number stored in the computer is equal to the decimal value 0.1000000000000000055511151231257827021181583404541015625. Instead of displaying the full decimal value, many languages \(including older versions of Python\), round the result to 17 significant digits:>>>
225
+
điều này có nghĩa là con số thực tế được máy tính lưu trữ là 0.1000000000000000055511151231257827021181583404541015625. Thay vì việc hiển thị toàn bộ giá trị này, rất nhiều ngôn ngữ lập trình \(bao gồm cả các phiên bản cũ của Python\), làm tròn con số này về một số có 17 chữ số:>>>
226
226
227
227
```text
228
228
>>> format(0.1, '.17f')
229
229
'0.10000000000000001'
230
230
```
231
231
232
-
The[`fractions`](https://docs.python.org/3/library/fractions.html#module-fractions)and[`decimal`](https://docs.python.org/3/library/decimal.html#module-decimal)modules make these calculations easy:>>>
232
+
Module[`fractions`](https://docs.python.org/3/library/fractions.html#module-fractions)và[`decimal`](https://docs.python.org/3/library/decimal.html#module-decimal)khiến việc tính toán này trở nên dễ dàng hơn:>>>
0 commit comments