Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 68e1024

Browse files
authored
Update 15.-floating-point-arithmetic-issues-and-limitations.md
1 parent f94df6b commit 68e1024

1 file changed

Lines changed: 23 additions & 22 deletions

File tree

15.-floating-point-arithmetic-issues-and-limitations.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,75 @@
1-
# 15. Floating Point Arithmetic: Issues and Limitations
1+
# 15. Số hữu tỉ: Vấn đề và hạn chế
22

33

44

5-
Floating-point numbers are represented in computer hardware as base 2 \(binary\) fractions. For example, the decimal fraction
5+
Số hữu tỉ được máy tính hiểu dưới dạng phân số hệ nhị phân. Ví dụ như với phân số thập phân:
66

77
```text
88
0.125
99
```
1010

11-
has value 1/10 + 2/100 + 5/1000, and in the same way the binary fraction
11+
sẽ có giá trị là 1/10 + 2/100 + 5/1000, cũng theo cách đó là cách biểu diễn phân số nhị phân:
1212

1313
```text
1414
0.001
1515
```
1616

17-
has value 0/2 + 0/4 + 1/8. These two fractions have identical values, the only real difference being that the first is written in base 10 fractional notation, and the second in base 2.
17+
sẽ có giá trị là 0/2 + 0/4 + 1/8. Hai dạng phân số này đều có giá trị xác định, sự khác biệt duy nhất là một loại được viết dựa trên hệ thập phân, và loại kia là ở hệ nhị phân.
1818

19-
Unfortunately, most decimal fractions cannot be represented exactly as binary fractions. A consequence is that, in general, the decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine.
19+
Vấn đề ở đây là, hầu hết phân số thập phân không thể biểu diễn chính xác ở dạng nhị phân. Để giải quyết vấn đề này, theo cách cơ bản nhất, các số hữu tỉ được nhập vào chỉ được máy tính hiểu ở dạng tổng các phân số nhị phân ở dạng gần đúng.
2020

21-
The problem is easier to understand at first in base 10. Consider the fraction 1/3. You can approximate that as a base 10 fraction:
21+
Giống như vấn đề với phân số thập phân, mọi thứ sẽ khó khăn hơn với những số dạng 1/3, ở đó ta chỉ có thể biểu diễn gần đúng ở dạng thập phân như:
2222

2323
```text
2424
0.3
2525
```
2626

27-
or, better,
27+
hay chính xác hơn:
2828

2929
```text
3030
0.33
3131
```
3232

33-
or, better,
33+
hay chính xác hơn:
3434

3535
```text
3636
0.333
3737
```
3838

39-
and so on. No matter how many digits you’re willing to write down, the result will never be exactly 1/3, but will be an increasingly better approximation of 1/3.
39+
Bất kể ta cần mẫn viết thêm bao nhiêu số, kết quả không bao giờ chính xác là 1/3, mà nó chỉ tăng tính gần đúng tới 1/3.
40+
41+
Tương tự như thế, không cần biết bạn muốn thêm bao nhiêu số với phân số nhị phân, số hữu tỉ thập phân 0.1 mãi mãi không thể có giá trị chính xác là 0.1 ở dạng nhị phân, 1/10 mãi mãi là sự lặp lại vô tận trong trường hợp này.
4042

41-
In the same way, no matter how many base 2 digits you’re willing to use, the decimal value 0.1 cannot be represented exactly as a base 2 fraction. In base 2, 1/10 is the infinitely repeating fraction
4243

4344
```text
4445
0.0001100110011001100110011001100110011001100110011...
4546
```
4647

47-
Stop at any finite number of bits, and you get an approximation. On most machines today, floats are approximated using a binary fraction with the numerator using the first 53 bits starting with the most significant bit and with the denominator as a power of two. In the case of 1/10, the binary fraction is `3602879701896397 / 2** 55` which is close to but not exactly equal to the true value of 1/10.
48+
Việc biểu diễn gần đúng này sẽ được dừng lại ở một số hữu hạn bit. Trong hầu hết các máy tính hiện tại, số hữu tỉ được biểu diễn gần đúng sử dụng 53 bit đầu tiên biểu diễn phần tỉ số và phần mẫu số là lũy thừa của 2. Trong trường hợp của 1/10, phân số nhị phân sẽ là (3602879701896397 / (2 ** 55)), nghĩa là gần đúng chứ không phải là 1/10.
4849

49-
Many users are not aware of the approximation because of the way values are displayed. Python only prints a decimal approximation to the true decimal value of the binary approximation stored by the machine. On most machines, if Python were to print the true decimal value of the binary approximation stored for 0.1, it would have to display>>>
50+
Hầu hết người dùng đều không để ý đến giá trị gần đúng. Python chỉ in ra giá trị số hữu tỉ thập phân gần đúng của một số hữu tỉ nhị phân thật sự đang được lưu lại trong bộ nhớ máy tính (RAM). Với hầu hết các máy tính, nếu như Python thật sự in ra con số phân số nhị phân đang được lưu trữ trong bộ nhớ, giá trị 0.1 sẽ phải là:
5051

5152
```text
5253
>>> 0.1
5354
0.1000000000000000055511151231257827021181583404541015625
5455
```
5556

56-
That is more digits than most people find useful, so Python keeps the number of digits manageable by displaying a rounded value instead>>>
57+
Đây là một con số không phải ai cũng thấy hữu dụng nên Python chỉ thể hiện một con số được làm tròn thay vì giá trị thật
5758

5859
```text
5960
>>> 1 / 10
6061
0.1
6162
```
6263

63-
Just remember, even though the printed result looks like the exact value of 1/10, the actual stored value is the nearest representable binary fraction.
64+
Luôn nhớ rằng, dù cho kết quả được in ra có vẻ giống với giá trị thực của 1/10, giá trị được lưu trữ chỉ là giá trị phân số nhị phân gần đúng mà thôi.
6465

65-
Interestingly, there are many different decimal numbers that share the same nearest approximate binary fraction. For example, the numbers `0.1` and `0.10000000000000001` and`0.1000000000000000055511151231257827021181583404541015625` are all approximated by `3602879701896397/ 2 ** 55`. Since all of these decimal values share the same approximation, any one of them could be displayed while still preserving the invariant `eval(repr(x)) == x`.
66+
Thú vị hơn, ta sẽ thấy có rất nhiều số hữu tỉ thập phân có cùng một giá trị tương đương nhị phân. Ví dụ như 0.10.10000000000000001 hay 0.1000000000000000055511151231257827021181583404541015625 đều được biểu diễn chung bằng (3602879701896397 / (2 ** 55)). Chính vì nguyên nhân này, tất cả các giá trị hữu tỉ thập phân bên trên đều được logic eval(repr(x)) == 1/10 trả về giá trị True.
6667

67-
Historically, the Python prompt and built-in [`repr()`](https://docs.python.org/3/library/functions.html#repr) function would choose the one with 17 significant digits, `0.10000000000000001`. Starting with Python 3.1, Python \(on most systems\) is now able to choose the shortest of these and simply display `0.1`.
68+
Trong quá khứ, build-in function của Python là repr() sẽ hiển thị đến ký tự thứ 17 (0.10000000000000001). Bắt đầu từ Python 3.1, Python đã có thể lựa chọn giá trị tương đương gần nhất và hiển thị 0.1 (với hầu hết phần cứng).
6869

69-
Note that this is in the very nature of binary floating-point: this is not a bug in Python, and it is not a bug in your code either. You’ll see the same kind of thing in all languages that support your hardware’s floating-point arithmetic \(although some languages may not _display_ the difference by default, or in all output modes\).
70+
Lưu ý đây là tính tự nhiên của số hữu tỉ nhị phân, đây không phải là lỗi của Python, cũng càng không phải là lỗi của chương trình bạn viết. Vấn đề này cũng được xử lý tương tự ở rất nhiều ngôn ngữ lập trình khác.
7071

71-
For more pleasant output, you may wish to use string formatting to produce a limited number of significant digits:>>>
72+
Để output dễ nhìn hơn, ta có thể dùng string format để đưa ra độ chính xác cho kết quả ta cần.
7273

7374
```text
7475
>>> format(math.pi, '.12g') # give 12 significant digits
@@ -81,23 +82,23 @@ For more pleasant output, you may wish to use string formatting to produce a lim
8182
'3.141592653589793'
8283
```
8384

84-
It’s important to realize that this is, in a real sense, an illusion: you’re simply rounding the _display_ of the true machine value.
85+
Ta rút ra một vấn đề quan trọng từ việc này: ta chỉ nhận được con số làm tròn từ giá trị thật mà máy tính đang lưu trữ.
8586

86-
One illusion may beget another. For example, since 0.1 is not exactly 1/10, summing three values of 0.1 may not yield exactly 0.3, either:>>>
87+
Mỗi sai số tạo nên các sai số khác. Ví dụ như, vì 0.1 không phải chính xác là 1/10, tổng của ba giá trị 0.1 có thể không phải là 0.3
8788

8889
```text
8990
>>> .1 + .1 + .1 == .3
9091
False
9192
```
9293

93-
Also, since the 0.1 cannot get any closer to the exact value of 1/10 and 0.3 cannot get any closer to the exact value of 3/10, then pre-rounding with [`round()`](https://docs.python.org/3/library/functions.html#round) function cannot help:>>>
94+
Tất nhiên, do 0.1 không thể đúng là 1/10 0.3 không thể đúng là 3/10, việc làm tròn trước với hàm [`round()`](https://docs.python.org/3/library/functions.html#round) không thể giúp được gì:
9495

9596
```text
9697
>>> round(.1, 1) + round(.1, 1) + round(.1, 1) == round(.3, 1)
9798
False
9899
```
99100

100-
Though the numbers cannot be made closer to their intended exact values, the [`round()`](https://docs.python.org/3/library/functions.html#round) function can be useful for post-rounding so that results with inexact values become comparable to one another:>>>
101+
Cho dù các con số này không thể tiến tới chính xác giá trị của nó, hàm [`round()`](https://docs.python.org/3/library/functions.html#round) có thể hữu dụng để làm tròn sau khi tính toán, khiến những số gần đúng có thể so sánh như hai giá trị tương đương.
101102

102103
```text
103104
>>> round(.1 + .1 + .1, 10) == round(.3, 10)

0 commit comments

Comments
 (0)