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
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -105,50 +105,50 @@ Cho dù các con số này không thể tiến tới chính xác giá trị củ
105
105
True
106
106
```
107
107
108
-
Binary floating-point arithmetic holds many surprises like this. The problem with “0.1” is explained in precise detail below, in the “Representation Error” section. See[The Perils of Floating Point](http://www.lahey.com/float.htm)for a more complete account of other common surprises.
108
+
Thuật toán hữu tỉ nhị phân tạo ra rất nhiều bất ngờ tương tự thế này. Vấn đề với "0.1" sẽ được trình bày cụ thể phía bên dưới, ở phần "Lỗi sai số". Xem[The Perils of Floating Point](http://www.lahey.com/float.htm)để hiểu rõ hơn về vấn đề này.
109
109
110
-
As that says near the end, “there are no easy answers.” Still, don’t be unduly wary of floating-point! The errors in Python float operations are inherited from the floating-point hardware, and on most machines are on the order of no more than 1 part in 2\*\*53 per operation. That’s more than adequate for most tasks, but you do need to keep in mind that it’s not decimal arithmetic and that every float operation can suffer a new rounding error.
110
+
Như được nói ở gần cuối, "vấn đề này không hề có câu trả lời dễ dàng". Tuy nhiên, ta đừng quá lo lắng về số hữu tỉ! Sai số của Python trong tính toán số hữu tỉ là sản phẩm kế thừa từ sai số hệ thống của số hữu tỉ, và hầu hết hệ thống có sai số không quá (1/(2 ** 53)). Việc này là quá chính xác với hầu hết các chương trình, chỉ cần lưu ý là nó không phải là thuật toán thập phân, và tất cả các phép tính hữu tỉ đều có sai số làm tròn.
111
111
112
-
While pathological cases do exist, for most casual use of floating-point arithmetic you’ll see the result you expect in the end if you simply round the display of your final results to the number of decimal digits you expect. [`str()`](https://docs.python.org/3/library/stdtypes.html#str)usually suffices, and for finer control see the[`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format)method’s format specifiers in[Format String Syntax](https://docs.python.org/3/library/string.html#formatstrings).
112
+
Do các sai số hệ thống này, hầu hết các trường hợp thông thường có sử dụng thuật toán hữu tỉ ta có thể có các kết quả như mong muốn bằng việc làm tròn kết quả cuối cùng sau khi tính toán. [`str()`](https://docs.python.org/3/library/stdtypes.html#str) thường được sử dụng, tìm hiểu thêm[`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format)để sử dụng tốt hơn hàm này[Format String Syntax](https://docs.python.org/3/library/string.html#formatstrings).
113
113
114
-
For use cases which require exact decimal representation, try using the [`decimal`](https://docs.python.org/3/library/decimal.html#module-decimal)module which implements decimal arithmetic suitable for accounting applications and high-precision applications.
114
+
Với các trường hợp đòi hỏi giá trị thập phân chính xác, ta có thể sử dụng module [`decimal`](https://docs.python.org/3/library/decimal.html#module-decimal)để tăng độ chính xác cho các thuật toán thập phân đòi hỏi trong các phần mềm kế toán hoặc các phần mềm kỹ thuật khác.
115
115
116
-
Another form of exact arithmetic is supported by the [`fractions`](https://docs.python.org/3/library/fractions.html#module-fractions)module which implements arithmetic based on rational numbers \(so the numbers like 1/3 can be represented exactly\).
116
+
Một lựa chọn khác cho thuật toán chính xác là module [`fractions`](https://docs.python.org/3/library/fractions.html#module-fractions) fractions với các thuật toán tăng cường độ chính xác dựa trên các số tỉ lệ (1/3 thể được biểu diễn chính xác nhờ số tỉ lệ).
117
117
118
-
If you are a heavy user of floating point operations you should take a look at the Numerical Python package and many other packages for mathematical and statistical operations supplied by the SciPy project. See <[https://scipy.org](https://scipy.org/)>.
118
+
Nếu bạn cần phải sử dụng số hữu tỉ thường xuyên, bạn nên tìm hiểu về các gói dữ liệu số của Python mà điển hình là [https://scipy.org](https://scipy.org/).
119
119
120
-
Python provides tools that may help on those rare occasions when you really _do_ want to know the exact value of a float. The [`float.as_integer_ratio()`](https://docs.python.org/3/library/stdtypes.html#float.as_integer_ratio)method expresses the value of a float as a fraction:>>>
120
+
Python có cung cấp một số công cụ có thể giúp bạn tìm hiểu giá trị thật sự của một số hữu tỉ. [`float.as_integer_ratio()`](https://docs.python.org/3/library/stdtypes.html#float.as_integer_ratio)là một giải pháp để thể hiện giá trị một số hữu tỉ ở dạng phân số.
121
121
122
122
```text
123
123
>>> x = 3.14159
124
124
>>> x.as_integer_ratio()
125
125
(3537115888337719, 1125899906842624)
126
126
```
127
127
128
-
Since the ratio is exact, it can be used to losslessly recreate the original value:>>>
128
+
Vì số tỉ lệ là chính xác, số này có thể được dùng để biểu diễn giá trị chính xác của giá trị số hữu tỉ ban đầu:
129
129
130
130
```text
131
131
>>> x == 3537115888337719 / 1125899906842624
132
132
True
133
133
```
134
134
135
-
The [`float.hex()`](https://docs.python.org/3/library/stdtypes.html#float.hex)method expresses a float in hexadecimal \(base 16\), again giving the exact value stored by your computer:>>>
135
+
[`float.hex()`](https://docs.python.org/3/library/stdtypes.html#float.hex)là giải pháp thể hiện một số hữu tỉ ở dạng hexadecimal (phần 16), đây là giá trị thực thế được lưu trữ trong máy tính:
136
136
137
137
```text
138
138
>>> x.hex()
139
139
'0x1.921f9f01b866ep+1'
140
140
```
141
141
142
-
This precise hexadecimal representation can be used to reconstruct the float value exactly:>>>
142
+
Một giá trị dưới dạng phần 16 được dùng để xây dựng lại giá trị chính xác của số hữu tỉ:
143
143
144
144
```text
145
145
>>> x == float.fromhex('0x1.921f9f01b866ep+1')
146
146
True
147
147
```
148
148
149
-
Since the representation is exact, it is useful for reliably porting values across different versions of Python \(platform independence\) and exchanging data with other languages that support the same format \(such as Java and C99\).
149
+
Vì giá trị này là giá trị chính xác, nó có thể sử dụng để truyền các giá trị qua lại giữa các phiên bản khác nhau của Python (từ những hệ điều hành độc lập) cũng như trao đổi dữ liệu với các ngôn ngữ lập trình khác có sử dụng định dạng tương tự (Java hay C++).
150
150
151
-
Another helpful tool is the [`math.fsum()`](https://docs.python.org/3/library/math.html#math.fsum)function which helps mitigate loss-of-precision during summation. It tracks “lost digits” as values are added onto a running total. That can make a difference in overall accuracy so that the errors do not accumulate to the point where they affect the final total:>>>
151
+
Còn một công cụ khác nữa là hàm [`math.fsum()`](https://docs.python.org/3/library/math.html#math.fsum)giúp giảm thiểu sai số trong phép tính tổng. Hàm này lưu lại các phần bị mất trong giá trị thực và thêm nó vào tổng thực tế. Việc này tạo ra một kết quả chính xác hơn khi ta cần sử dụng để so sánh:
152
152
153
153
```text
154
154
>>> sum([0.1] * 10) == 1.0
@@ -157,7 +157,7 @@ False
157
157
True
158
158
```
159
159
160
-
### 15.1. Representation Error
160
+
### 15.1. Lỗi sai số
161
161
162
162
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.
0 commit comments