The
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []4 module provides support for fast correctly rounded decimal floating point arithmetic. It offers several advantages over the
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6 datatype
Decimal “is based on a floating-point model which was designed with people in mind, and necessarily has a paramount guiding principle – computers must provide an arithmetic that works in the same way as the arithmetic that people learn at school. ” – excerpt from the decimal arithmetic specification
Decimal numbers can be represented exactly. In contrast, numbers like
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
7 and>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
8 do not have exact representations in binary floating point. End users typically would not expect>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
9 to display as>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']
0 as it does with binary floating pointThe exactness carries over into arithmetic. In decimal floating point,
>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']
1 is exactly equal to zero. In binary floating point, the result is>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']
2. While near to zero, the differences prevent reliable equality testing and differences can accumulate. For this reason, decimal is preferred in accounting applications which have strict equality invariantsThe decimal module incorporates a notion of significant places so that
>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']
3 is>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']
4. The trailing zero is kept to indicate significance. This is the customary presentation for monetary applications. For multiplication, the “schoolbook” approach uses all the figures in the multiplicands. For instance,>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']
5 gives>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']
6 while>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']
7 gives>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']
8Unlike hardware based binary floating point, the decimal module has a user alterable precision [defaulting to 28 places] which can be as large as needed for a given problem
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
4Both binary and decimal floating point are implemented in terms of published standards. While the built-in float type exposes only a modest portion of its capabilities, the decimal module exposes all required parts of the standard. When needed, the programmer has full control over rounding and signal handling. This includes an option to enforce exact arithmetic by using exceptions to block any inexact operations
The decimal module was designed to support “without prejudice, both exact unrounded decimal arithmetic [sometimes called fixed-point arithmetic] and rounded floating-point arithmetic. ” – excerpt from the decimal arithmetic specification
The module design is centered around three concepts. the decimal number, the context for arithmetic, and signals
A decimal number is immutable. It has a sign, coefficient digits, and an exponent. To preserve significance, the coefficient digits do not truncate trailing zeros. Decimals also include special values such as
>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']9,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []60, and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61. The standard also differentiates
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []62 from
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []63
The context for arithmetic is an environment specifying precision, rounding rules, limits on exponents, flags indicating the results of operations, and trap enablers which determine whether signals are treated as exceptions. Rounding options include
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []64,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []65,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []66,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []67,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []68,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []69,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []60, and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61
Signals are groups of exceptional conditions arising during the course of computation. Depending on the needs of the application, signals may be ignored, considered as informational, or treated as exceptions. Các tín hiệu trong mô-đun thập phân là.
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []62,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []63,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []64,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []65,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []66,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []67,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []68,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []69 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []60
Đối với mỗi tín hiệu, có một cờ và trình kích hoạt bẫy. Khi bắt gặp một tín hiệu, cờ của tín hiệu đó được đặt thành một, sau đó, nếu trình kích hoạt bẫy được đặt thành một, thì một ngoại lệ sẽ được đưa ra. Cờ dính, vì vậy người dùng cần đặt lại chúng trước khi theo dõi phép tính
Xem thêm
Đặc tả số học thập phân chung của IBM, Đặc tả số học thập phân chung
Hướng dẫn bắt đầu nhanh¶
Cách bắt đầu thông thường để sử dụng số thập phân là nhập mô-đun, xem ngữ cảnh hiện tại với
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 và, nếu cần, đặt các giá trị mới cho độ chính xác, làm tròn hoặc bật bẫy
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []8
Các trường hợp thập phân có thể được xây dựng từ các số nguyên, chuỗi, số float hoặc bộ dữ liệu. Việc xây dựng từ một số nguyên hoặc số float thực hiện chuyển đổi chính xác giá trị của số nguyên hoặc số float đó. Số thập phân bao gồm các giá trị đặc biệt như
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 viết tắt của “Không phải là số”, số dương và số âm
>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']9 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []62
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []2
Nếu tín hiệu
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []60 bị mắc kẹt, việc vô tình trộn số thập phân và số float trong hàm tạo hoặc so sánh thứ tự sẽ tạo ra một ngoại lệ
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []4
Mới trong phiên bản 3. 3
Tầm quan trọng của Số thập phân mới chỉ được xác định bởi số lượng chữ số đầu vào. Độ chính xác của ngữ cảnh và làm tròn chỉ phát huy tác dụng trong các phép toán số học
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []5
Nếu vượt quá giới hạn nội bộ của phiên bản C, việc xây dựng số thập phân sẽ tăng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []63
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
Thay đổi trong phiên bản 3. 3
Số thập phân tương tác tốt với phần lớn phần còn lại của Python. Đây là một rạp xiếc bay dấu phẩy động thập phân nhỏ
>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']
Và một số hàm toán học cũng có sẵn cho Số thập phân
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6
Phương thức
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []67 làm tròn một số thành số mũ cố định. Phương pháp này hữu ích cho các ứng dụng tiền tệ thường làm tròn kết quả đến một số vị trí cố định
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6
Như được hiển thị ở trên, hàm
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 truy cập ngữ cảnh hiện tại và cho phép thay đổi cài đặt. Cách tiếp cận này đáp ứng nhu cầu của hầu hết các ứng dụng
Đối với công việc nâng cao hơn, có thể hữu ích khi tạo các ngữ cảnh thay thế bằng cách sử dụng hàm tạo Context[]. Để thực hiện thay thế hoạt động, hãy sử dụng chức năng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []69
Theo tiêu chuẩn, mô-đun
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []4 cung cấp hai bối cảnh tiêu chuẩn sẵn sàng sử dụng,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []801 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []802. Cái trước đặc biệt hữu ích để gỡ lỗi vì nhiều bẫy được bật
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6
Bối cảnh cũng có cờ tín hiệu để theo dõi các điều kiện đặc biệt gặp phải trong quá trình tính toán. Các cờ vẫn được đặt cho đến khi bị xóa rõ ràng, vì vậy tốt nhất là xóa các cờ trước mỗi bộ tính toán được giám sát bằng cách sử dụng phương pháp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []803
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []80
The flags entry shows that the rational approximation to
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []804 was rounded [digits beyond the context precision were thrown away] and that the result is inexact [some of the discarded digits were non-zero]
Các bẫy riêng lẻ được đặt bằng cách sử dụng từ điển trong trường
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []805 của ngữ cảnh
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []81
Most programs adjust the current context only once, at the beginning of the program. Và, trong nhiều ứng dụng, dữ liệu được chuyển đổi thành
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 với một lần truyền bên trong một vòng lặp. With context set and decimals created, the bulk of the program manipulates the data no differently than with other Python numeric types
Đối tượng thập phân¶
class decimal. Số thập phân[giá trị=', context=None]¶Construct a new
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 object based from value
giá trị có thể là một số nguyên, chuỗi, tuple,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6 hoặc một đối tượng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 khác. If no value is given, returns
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []810. If value is a string, it should conform to the decimal numeric string syntax after leading and trailing whitespace characters, as well as underscores throughout, are removed
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []82
Other Unicode decimal digits are also permitted where
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []811 appears above. These include decimal digits from various other alphabets [for example, Arabic-Indic and Devanāgarī digits] along with the fullwidth digits
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []812 through
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []813
If value is a
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []814, it should have three components, a sign [
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []815 for positive or
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []816 for negative], a
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []814 of digits, and an integer exponent. For example,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []818 returns
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []819
If value is a
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6, the binary floating point value is losslessly converted to its exact decimal equivalent. This conversion can often require 53 or more digits of precision. For example,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []821 converts to
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []822
The context precision does not affect how many digits are stored. That is determined exclusively by the number of digits in value. For example,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []823 records all five zeros even if the context precision is only three
The purpose of the context argument is determining what to do if value is a malformed string. If the context traps
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []63, an exception is raised; otherwise, the constructor returns a new Decimal with the value of
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61
Sau khi được xây dựng, các đối tượng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 là bất biến
Changed in version 3. 2. The argument to the constructor is now permitted to be a
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6 instance.
Changed in version 3. 3.
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6 arguments raise an exception if the
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []60 trap is set. By default the trap is off.
Changed in version 3. 6. Underscores are allowed for grouping, as with integral and floating-point literals in code.
Decimal floating point objects share many properties with the other built-in numeric types such as
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6 and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []831. All of the usual math operations and special methods apply. Likewise, decimal objects can be copied, pickled, printed, used as dictionary keys, used as set elements, compared, sorted, and coerced to another type [such as
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6 or
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []831]
There are some small differences between arithmetic on Decimal objects and arithmetic on integers and floats. When the remainder operator
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []834 is applied to Decimal objects, the sign of the result is the sign of the dividend rather than the sign of the divisor
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []83
The integer division operator
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []835 behaves analogously, returning the integer part of the true quotient [truncating towards zero] rather than its floor, so as to preserve the usual identity
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []836
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []84
The
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []834 and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []835 operators implement the
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []839 and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []840 operations [respectively] as described in the specification
Decimal objects cannot generally be combined with floats or instances of
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []841 in arithmetic operations. an attempt to add a
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 to a
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6, for example, will raise a
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []844. However, it is possible to use Python’s comparison operators to compare a
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 instance
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []846 with another number
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []847. This avoids confusing results when doing equality comparisons between numbers of different types
Changed in version 3. 2. Mixed-type comparisons between
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 instances and other numeric types are now fully supported.
In addition to the standard numeric properties, decimal floating point objects also have a number of specialized methods
adjusted[] ¶Return the adjusted exponent after shifting out the coefficient’s rightmost digits until only the lead digit remains.
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []849 trả về bảy. Used for determining the position of the most significant digit with respect to the decimal pointas_integer_ratio[] ¶
Return a pair
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []850 of integers that represent the given
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 instance as a fraction, in lowest terms and with a positive denominator
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []85
The conversion is exact. Raise OverflowError on infinities and ValueError on NaNs
New in version 3. 6
as_tuple[] ¶Return a named tuple representation of the number.
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []852. canonical[] ¶
Return the canonical encoding of the argument. Currently, the encoding of a
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 instance is always canonical, so this operation returns its argument unchangedcompare[other , context=None] ¶
Compare the values of two Decimal instances.
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []854 returns a Decimal instance, and if either operand is a NaN then the result is a NaN
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []86compare_signal[other , context=None] ¶
This operation is identical to the
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []854 method, except that all NaNs signal. That is, if neither operand is a signaling NaN then any quiet NaN operand is treated as though it were a signaling NaNcompare_total[other , context=None] ¶
Compare two operands using their abstract representation rather than their numerical value. Similar to the
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []854 method, but the result gives a total ordering on
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 instances. Two
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 instances with the same numeric value but different representations compare unequal in this ordering
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []87
Quiet and signaling NaNs are also included in the total ordering. The result of this function is
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []810 if both operands have the same representation,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []860 if the first operand is lower in the total order than the second, and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []861 if the first operand is higher in the total order than the second operand. See the specification for details of the total order
This operation is unaffected by context and is quiet. không có cờ nào được thay đổi và không có phép làm tròn nào được thực hiện. As an exception, the C version may raise InvalidOperation if the second operand cannot be converted exactly
compare_total_mag[other , context=None] ¶Compare two operands using their abstract representation rather than their value as in
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []862, but ignoring the sign of each operand.
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []863 is equivalent to
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []864
This operation is unaffected by context and is quiet. không có cờ nào được thay đổi và không có phép làm tròn nào được thực hiện. As an exception, the C version may raise InvalidOperation if the second operand cannot be converted exactly
conjugate[] ¶Just returns self, this method is only to comply with the Decimal Specification
copy_abs[] ¶Return the absolute value of the argument. This operation is unaffected by the context and is quiet. no flags are changed and no rounding is performed
copy_negate[] ¶Trả về phủ định của đối số. This operation is unaffected by the context and is quiet. no flags are changed and no rounding is performed
copy_sign[other , context=None] ¶Return a copy of the first operand with the sign set to be the same as the sign of the second operand. Ví dụ
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []88
This operation is unaffected by context and is quiet. không có cờ nào được thay đổi và không có phép làm tròn nào được thực hiện. As an exception, the C version may raise InvalidOperation if the second operand cannot be converted exactly
Trả về giá trị của hàm số mũ [tự nhiên]
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []865 tại số đã cho. The result is correctly rounded using the
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []68 rounding mode
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []89classmethod from_float[f]¶
Hàm tạo thay thế chỉ chấp nhận các phiên bản của
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6 hoặc
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []831
Lưu ý
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []869 không giống với
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []870. kể từ 0. 1 không thể biểu diễn chính xác trong dấu phẩy động nhị phân, giá trị được lưu dưới dạng giá trị biểu diễn gần nhất là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []871. Giá trị tương đương ở dạng thập phân là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []872
Ghi chú
Từ Python 3. 2 trở đi, một phiên bản
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 cũng có thể được xây dựng trực tiếp từ một phiên bản
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []6
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []20
Mới trong phiên bản 3. 1
fma[khác , thứ ba, context=None]¶Hợp nhất nhân-thêm. Return self*other+third with no rounding of the intermediate product self*other
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []21is_canonical[] ¶
Return
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 if the argument is canonical and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876 otherwise. Currently, a
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 instance is always canonical, so this operation always returns
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875is_finite[] ¶
Return
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 if the argument is a finite number, and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876 if the argument is an infinity or a NaNis_infinite[] ¶
Return
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 if the argument is either positive or negative infinity and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876 otherwiseis_nan[] ¶
Return
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 if the argument is a [quiet or signaling] NaN and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876 otherwiseis_normal[context=None] ¶
Return
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 if the argument is a normal finite number. Return
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876 if the argument is zero, subnormal, infinite or a NaNis_qnan[] ¶
Return
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 if the argument is a quiet NaN, and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876 otherwiseis_signed[] ¶
Return
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 if the argument has a negative sign and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876 otherwise. Note that zeros and NaNs can both carry signsis_snan[] ¶
Return
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 if the argument is a signaling NaN and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876 otherwiseis_subnormal[context=None] ¶
Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 nếu đối số không bình thường và ngược lại là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876is_zero[] ¶
Return
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 if the argument is a [positive or negative] zero and
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876 otherwiseln[context=None] ¶
Return the natural [base e] logarithm of the operand. The result is correctly rounded using the
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []68 rounding modelog10[context=None] ¶
Return the base ten logarithm of the operand. The result is correctly rounded using the
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []68 rounding modelogb[context=None] ¶
For a nonzero number, return the adjusted exponent of its operand as a
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 instance. If the operand is a zero then
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []200 is returned and the
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []64 flag is raised. If the operand is an infinity then
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []202 is returnedlogical_and[other , context=None] ¶
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []203 is a logical operation which takes two logical operands [see Logical operands ]. The result is the digit-wise
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []204 of the two operands. logical_invert[context=None] ¶
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []205 is a logical operation. The result is the digit-wise inversion of the operandlogical_or[other , context=None] ¶
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []206 is a logical operation which takes two logical operands [see Logical operands ]. The result is the digit-wise
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []207 of the two operands. logical_xor[other , context=None] ¶
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []208 is a logical operation which takes two logical operands [see Logical operands ]. The result is the digit-wise exclusive or of the two operands. max[other , context=None] ¶
Like
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []209 except that the context rounding rule is applied before returning and that
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 values are either signaled or ignored [depending on the context and whether they are signaling or quiet]max_mag[other , context=None] ¶
Tương tự như phương pháp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []211, nhưng việc so sánh được thực hiện bằng cách sử dụng các giá trị tuyệt đối của toán hạngphút[khác , bối cảnh=None]¶
Giống như
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []212 ngoại trừ quy tắc làm tròn ngữ cảnh được áp dụng trước khi trả về và các giá trị
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 được báo hiệu hoặc bỏ qua [tùy thuộc vào ngữ cảnh và liệu chúng có báo hiệu hay không]min_mag[khác , bối cảnh=None]¶
Similar to the
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []214 method, but the comparison is done using the absolute values of the operandsnext_minus[context=None] ¶
Return the largest number representable in the given context [or in the current thread’s context if no context is given] that is smaller than the given operand
next_plus[bối cảnh=Không có]¶Return the smallest number representable in the given context [or in the current thread’s context if no context is given] that is larger than the given operand
next_toward[khác , bối cảnh=None]¶If the two operands are unequal, return the number closest to the first operand in the direction of the second operand. If both operands are numerically equal, return a copy of the first operand with the sign set to be the same as the sign of the second operand
normalize[context=None] ¶Chuẩn hóa số bằng cách loại bỏ các số 0 ở cuối bên phải và chuyển đổi bất kỳ kết quả nào bằng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []810 thành
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []216. Được sử dụng để tạo ra các giá trị chuẩn cho các thuộc tính của một lớp tương đương. Ví dụ:
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []217 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []218 đều chuẩn hóa thành giá trị tương đương
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []219số_lớp[bối cảnh=Không có]¶
Trả về một chuỗi mô tả lớp của toán hạng. Giá trị trả về là một trong mười chuỗi sau
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
220, chỉ ra rằng toán hạng là vô cực âm>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
221, chỉ ra rằng toán hạng là một số bình thường âm>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
222, chỉ ra rằng toán hạng là âm và không bình thường>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
223, cho biết toán hạng là số 0 âm>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
224, cho biết toán hạng là số 0 dương>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
225, chỉ ra rằng toán hạng là dương và không bình thường>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
226, chỉ ra rằng toán hạng là một số bình thường dương>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
227, chỉ ra rằng toán hạng là dương vô cùng>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
228, chỉ ra rằng toán hạng là một NaN yên tĩnh [Không phải là một số]>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []
229, chỉ ra rằng toán hạng là một NaN báo hiệu
Trả về một giá trị bằng toán hạng đầu tiên sau khi làm tròn và có số mũ của toán hạng thứ hai
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []22
Không giống như các thao tác khác, nếu độ dài của hệ số sau thao tác lượng tử hóa lớn hơn độ chính xác, thì một
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []63 được báo hiệu. Điều này đảm bảo rằng, trừ khi có điều kiện lỗi, số mũ được lượng tử hóa luôn bằng số mũ của toán hạng bên phải
Ngoài ra, không giống như các hoạt động khác, lượng tử hóa không bao giờ báo hiệu Underflow, ngay cả khi kết quả là không bình thường và không chính xác
Nếu số mũ của toán hạng thứ hai lớn hơn số mũ của toán hạng thứ nhất thì có thể cần phải làm tròn. Trong trường hợp này, chế độ làm tròn được xác định bởi đối số
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []231 nếu được đưa ra, nếu không thì bởi đối số
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []232 đã cho;
Một lỗi được trả về bất cứ khi nào số mũ kết quả lớn hơn
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []233 hoặc nhỏ hơn
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []234cơ số[] ¶
Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []235, cơ số [cơ số] trong đó lớp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 thực hiện tất cả các phép tính số học của nó. Bao gồm để tương thích với đặc điểm kỹ thuậtremainder_near[khác , bối cảnh=None]¶
Trả lại phần còn lại từ việc chia bản thân cho người khác. Điều này khác với
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []237 ở chỗ dấu của phần dư được chọn để giảm thiểu giá trị tuyệt đối của nó. Chính xác hơn, giá trị trả về là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []238 trong đó
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []239 là số nguyên gần nhất với giá trị chính xác của
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []240 và nếu hai số nguyên gần bằng nhau thì số chẵn được chọn
Nếu kết quả bằng không thì dấu của nó sẽ là dấu của tự
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []23xoay[khác , bối cảnh=None]¶
Trả về kết quả xoay các chữ số của toán hạng thứ nhất theo một lượng được chỉ định bởi toán hạng thứ hai. Toán hạng thứ hai phải là một số nguyên trong phạm vi -precision đến precision. Giá trị tuyệt đối của toán hạng thứ hai cho biết số vị trí cần xoay. Nếu toán hạng thứ hai là dương thì phép quay sang trái; . Hệ số của toán hạng đầu tiên được đệm ở bên trái với độ chính xác từ 0 đến độ dài nếu cần. Dấu và số mũ của toán hạng đầu tiên không thay đổi
same_quantum[other , bối cảnh=None]¶Kiểm tra xem bản thân và người khác có cùng số mũ hay cả hai đều
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61
This operation is unaffected by context and is quiet. không có cờ nào được thay đổi và không có phép làm tròn nào được thực hiện. As an exception, the C version may raise InvalidOperation if the second operand cannot be converted exactly
scaleb[khác , bối cảnh=None]¶Trả về toán hạng đầu tiên với số mũ được điều chỉnh bởi toán hạng thứ hai. Tương tự, trả về toán hạng đầu tiên nhân với
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []242. Toán hạng thứ hai phải là một số nguyênchuyển[khác , bối cảnh=None]¶
Trả về kết quả của việc dịch chuyển các chữ số của toán hạng thứ nhất theo một lượng được chỉ định bởi toán hạng thứ hai. Toán hạng thứ hai phải là một số nguyên trong phạm vi -precision đến precision. Giá trị tuyệt đối của toán hạng thứ hai cho biết số lượng vị trí cần dịch chuyển. Nếu toán hạng thứ hai là số dương thì phép dịch chuyển sang trái; . Các chữ số được chuyển vào hệ số là số không. Dấu và số mũ của toán hạng đầu tiên không thay đổi
sqrt[bối cảnh=Không có]¶Trả lại căn bậc hai của đối số về độ chính xác đầy đủ
to_eng_string[bối cảnh=Không có]¶Chuyển đổi thành chuỗi, sử dụng ký hiệu kỹ thuật nếu cần số mũ
Ký hiệu kỹ thuật có số mũ là bội số của 3. Điều này có thể để lại tối đa 3 chữ số ở bên trái của vị trí thập phân và có thể yêu cầu thêm một hoặc hai số 0 ở cuối
Ví dụ: điều này chuyển đổi
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []243 thành
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []244to_integral[làm tròn=Không có, context=None]¶
Giống với phương pháp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []245. Tên
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []246 đã được giữ để tương thích với các phiên bản cũ hơnto_integral_exact[làm tròn=Không có, context=None]¶
Làm tròn đến số nguyên gần nhất, báo hiệu
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []65 hoặc
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []66 nếu thích hợp nếu làm tròn xảy ra. Chế độ làm tròn được xác định bởi tham số
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []231 nếu được cung cấp, nếu không thì bởi
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []232 đã cho. Nếu không có tham số nào được cung cấp thì chế độ làm tròn của ngữ cảnh hiện tại được sử dụngto_integral_value[làm tròn=Không có, context=None]¶
Làm tròn đến số nguyên gần nhất mà không báo hiệu
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []65 hoặc
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []66. Nếu được đưa ra, áp dụng làm tròn;
Toán hạng logic¶
Các phương thức
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []203,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []205,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []206 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []208 mong muốn các đối số của chúng là toán hạng logic. Toán hạng logic là một thực thể
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 có số mũ và dấu đều bằng 0 và có tất cả các chữ số là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []815 hoặc
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []816
Đối tượng ngữ cảnh¶
Bối cảnh là môi trường cho các hoạt động số học. Chúng chi phối độ chính xác, đặt quy tắc làm tròn, xác định tín hiệu nào được coi là ngoại lệ và giới hạn phạm vi cho số mũ
Mỗi luồng có bối cảnh hiện tại riêng được truy cập hoặc thay đổi bằng cách sử dụng các hàm
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []69thập phân. getcontext[] ¶
Trả về bối cảnh hiện tại cho chuỗi hoạt động
thập phân. setcontext[c] ¶Đặt bối cảnh hiện tại cho chuỗi hoạt động thành c
Bạn cũng có thể sử dụng câu lệnh
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []262 và hàm
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []263 để tạm thời thay đổi ngữ cảnh hoạt độngthập phân. localcontext[ctx=None , \*\*kwargs]¶
Trả về trình quản lý ngữ cảnh sẽ đặt ngữ cảnh hiện tại cho luồng đang hoạt động thành một bản sao của ctx khi vào câu lệnh with và khôi phục ngữ cảnh trước đó khi thoát khỏi câu lệnh with. Nếu không có ngữ cảnh nào được chỉ định, một bản sao của ngữ cảnh hiện tại sẽ được sử dụng. Đối số kwargs được sử dụng để đặt các thuộc tính của ngữ cảnh mới
Ví dụ: đoạn mã sau đặt độ chính xác thập phân hiện tại thành 42 vị trí, thực hiện phép tính và sau đó tự động khôi phục ngữ cảnh trước đó
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []24
Sử dụng các đối số từ khóa, mã sẽ như sau
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []25
Tăng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []844 nếu kwargs cung cấp một thuộc tính mà
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []265 không hỗ trợ. Tăng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []844 hoặc
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []267 nếu kwargs cung cấp giá trị không hợp lệ cho một thuộc tính
Đã thay đổi trong phiên bản 3. 11. ______4263 hiện hỗ trợ đặt thuộc tính ngữ cảnh thông qua việc sử dụng đối số từ khóa.
Các bối cảnh mới cũng có thể được tạo bằng hàm tạo
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []265 được mô tả bên dưới. Ngoài ra, mô-đun cung cấp ba bối cảnh được tạo sẵnlớp thập phân. Bối cảnh cơ bản ¶
Đây là ngữ cảnh tiêu chuẩn được xác định bởi Đặc tả số học thập phân chung. Độ chính xác được đặt thành chín. Làm tròn được đặt thành
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []69. Tất cả cờ bị xóa. Tất cả các bẫy được bật [được coi là ngoại lệ] ngoại trừ
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []65,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []66 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []67
Do nhiều bẫy được bật nên bối cảnh này rất hữu ích để gỡ lỗi
lớp thập phân. Ngữ cảnh mở rộng ¶Đây là ngữ cảnh tiêu chuẩn được xác định bởi Đặc tả số học thập phân chung. Độ chính xác được đặt thành chín. Làm tròn được đặt thành
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []68. Tất cả cờ bị xóa. Không có bẫy nào được kích hoạt [để không phát sinh ngoại lệ trong quá trình tính toán]
Bởi vì các bẫy bị vô hiệu hóa, ngữ cảnh này hữu ích cho các ứng dụng muốn có giá trị kết quả là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 hoặc
>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']9 thay vì đưa ra các ngoại lệ. Điều này cho phép một ứng dụng hoàn thành một lần chạy khi có các điều kiện có thể làm dừng chương trìnhlớp thập phân. Ngữ cảnh mặc định ¶
Bối cảnh này được sử dụng bởi hàm tạo
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []265 làm nguyên mẫu cho các bối cảnh mới. Thay đổi một trường [độ chính xác như vậy] có tác dụng thay đổi mặc định cho các ngữ cảnh mới được tạo bởi hàm tạo
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []265
Bối cảnh này hữu ích nhất trong môi trường đa luồng. Thay đổi một trong các trường trước khi chuỗi bắt đầu có tác dụng đặt giá trị mặc định trên toàn hệ thống. Không nên thay đổi các trường sau khi các luồng đã bắt đầu vì nó sẽ yêu cầu đồng bộ hóa luồng để ngăn các điều kiện tương tranh
Trong các môi trường luồng đơn, tốt nhất là không sử dụng ngữ cảnh này. Thay vào đó, chỉ cần tạo bối cảnh một cách rõ ràng như được mô tả bên dưới
Các giá trị mặc định là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []279=
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []280,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []231=
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []68 và bật bẫy cho
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []68,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []63 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []64
Ngoài ba ngữ cảnh được cung cấp, các ngữ cảnh mới có thể được tạo bằng hàm tạo
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []265lớp thập phân. Bối cảnh[prec=Không có, rounding=None, Emin=None, Emax=None, capitals=None, clamp=None, flags=None, traps=None]¶
Tạo bối cảnh mới. Nếu một trường không được chỉ định hoặc là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []287, các giá trị mặc định sẽ được sao chép từ
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []288. Nếu trường cờ không được chỉ định hoặc là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []287, tất cả các cờ sẽ bị xóa
prec là một số nguyên trong phạm vi [
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []816,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []291] đặt độ chính xác cho các phép tính số học trong ngữ cảnh
Tùy chọn làm tròn là một trong những hằng số được liệt kê trong phần Chế độ làm tròn
Các trường bẫy và cờ liệt kê mọi tín hiệu sẽ được đặt. Nói chung, bối cảnh mới chỉ nên đặt bẫy và để trống cờ
Các trường Emin và Emax là các số nguyên xác định các giới hạn bên ngoài cho phép đối với số mũ. Emin phải nằm trong khoảng [
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []292,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []815], Emax trong khoảng [
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []815,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []295]
Trường chữ hoa là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []815 hoặc
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []816 [mặc định]. Nếu được đặt thành
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []816, số mũ được in bằng chữ hoa
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []299; .
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []401
Trường kẹp là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []815 [mặc định] hoặc
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []816. Nếu được đặt thành
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []816, số mũ
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []400 của phiên bản
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 có thể biểu thị trong ngữ cảnh này bị giới hạn nghiêm ngặt trong phạm vi
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []407. Nếu kẹp là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []815 thì tình trạng yếu hơn sẽ xảy ra. số mũ đã điều chỉnh của phiên bản
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 nhiều nhất là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []233. Khi clamp là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []816, một số bình thường lớn, nếu có thể, sẽ bị giảm số mũ và một số 0 tương ứng được thêm vào hệ số của nó, để phù hợp với các ràng buộc về số mũ; . Ví dụ
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []26
Giá trị kẹp của
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []816 cho phép tương thích với các định dạng trao đổi thập phân có chiều rộng cố định được chỉ định trong IEEE 754
Lớp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []265 định nghĩa một số phương thức có mục đích chung cũng như một số lượng lớn các phương thức để thực hiện số học trực tiếp trong một ngữ cảnh nhất định. Ngoài ra, đối với mỗi phương pháp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 được mô tả ở trên [ngoại trừ phương pháp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []415 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []416], có một phương pháp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []265 tương ứng. Ví dụ: đối với phiên bản
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []265
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []419 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 phiên bản
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []846,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []422 tương đương với
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []423. Mỗi phương thức
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []265 chấp nhận một số nguyên Python [một thể hiện của
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []831] ở bất kỳ đâu mà một thể hiện Số thập phân được chấp nhậnclear_flags[] ¶
Đặt lại tất cả các cờ thành
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []815clear_traps[] ¶
Đặt lại tất cả các bẫy thành
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []815
Mới trong phiên bản 3. 3
bản sao[] ¶Trả lại một bản sao của ngữ cảnh
copy_decimal[num] ¶Trả về một bản sao của ví dụ thập phân num
create_decimal[num] ¶Tạo một thể hiện Thập phân mới từ num nhưng sử dụng self làm ngữ cảnh. Không giống như hàm tạo
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806, độ chính xác của ngữ cảnh, phương pháp làm tròn, cờ và bẫy được áp dụng cho chuyển đổi
Điều này rất hữu ích vì các hằng số thường được cung cấp cho độ chính xác cao hơn mức cần thiết của ứng dụng. Một lợi ích khác là làm tròn ngay lập tức loại bỏ các hiệu ứng ngoài ý muốn từ các chữ số vượt quá độ chính xác hiện tại. Trong ví dụ sau, sử dụng đầu vào không được làm tròn có nghĩa là việc thêm 0 vào tổng có thể thay đổi kết quả
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []27
Phương pháp này thực hiện thao tác đánh số của đặc tả IBM. Nếu đối số là một chuỗi, không được phép có khoảng trắng ở đầu hoặc cuối hoặc dấu gạch dưới
create_decimal_from_float[f] ¶Tạo một thể hiện Thập phân mới từ một float f nhưng làm tròn bằng cách sử dụng self làm ngữ cảnh. Không giống như phương pháp lớp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []429, độ chính xác ngữ cảnh, phương pháp làm tròn, cờ và bẫy được áp dụng cho chuyển đổi
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []28
Mới trong phiên bản 3. 1
Etiny[] ¶Trả về giá trị bằng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []430 là giá trị số mũ tối thiểu cho kết quả không bình thường. Khi tràn xảy ra, số mũ được đặt thành
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []234Etop[] ¶
Trả về một giá trị bằng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []432
Cách tiếp cận thông thường để làm việc với số thập phân là tạo các phiên bản
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 và sau đó áp dụng các phép toán số học diễn ra trong ngữ cảnh hiện tại cho chuỗi hoạt động. Một cách tiếp cận khác là sử dụng các phương thức ngữ cảnh để tính toán trong một ngữ cảnh cụ thể. Các phương thức tương tự như các phương thức dành cho lớp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 và chỉ được kể lại ngắn gọn ở đâyabs[x] ¶
Trả về giá trị tuyệt đối của x
add[x , y]¶Trả về tổng của x và y
chính tắc[x] ¶Trả về cùng một đối tượng thập phân x
so sánh[x , y]¶So sánh x và y bằng số
so sánh_signal[x , y]¶So sánh các giá trị của hai toán hạng bằng số
so_total[x , y]¶So sánh hai toán hạng sử dụng biểu diễn trừu tượng của chúng
so_total_mag[x , y]¶So sánh hai toán hạng bằng biểu diễn trừu tượng của chúng, bỏ qua dấu
copy_abs[x] ¶Trả về một bản sao của x với dấu được đặt thành 0
copy_negate[x] ¶Trả về một bản sao của x với dấu đảo ngược
copy_sign[x , y]¶Sao chép dấu từ y đến x
chia[x , y]¶Trả về x chia cho y
divide_int[x , y]¶Trả về x chia cho y, rút gọn thành một số nguyên
divmod[x , y]¶Chia hai số và trả về phần nguyên của kết quả
exp[x] ¶Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []435fma[x , y, z]¶
Trả về x nhân với y, cộng với z
is_canonical[x] ¶Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 nếu x là hợp quy; is_finite[x] ¶
Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 nếu x hữu hạn; is_infinite[x] ¶
Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 nếu x là vô hạn; is_nan[x] ¶
Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 nếu x là qNaN hoặc sNaN; is_normal[x] ¶
Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 nếu x là số bình thường; is_qnan[x] ¶
Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 nếu x là một NaN yên tĩnh; is_signed[x] ¶
Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 nếu x âm; is_snan[x] ¶
Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 nếu x là NaN báo hiệu; is_subnormal[x] ¶
Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 nếu x không bình thường; is_zero[x] ¶
Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 nếu x là số không; ln[x] ¶
Trả về logarit tự nhiên [cơ số e] của x
log10[x] ¶Trả về logarit cơ số 10 của x
logb[x] ¶Trả về số mũ của độ lớn MSD của toán hạng
logic_and[x , y]¶Áp dụng phép toán logic và giữa các chữ số của mỗi toán hạng
logic_invert[x] ¶Đảo ngược tất cả các chữ số trong x
logic_or[x , y]¶Áp dụng phép toán logic hoặc giữa các chữ số của mỗi toán hạng
logic_xor[x , y]¶Áp dụng phép toán logic xor giữa các chữ số của mỗi toán hạng
max[x , y]¶So sánh hai giá trị bằng số và trả về giá trị lớn nhất
max_mag[x , y]¶So sánh các giá trị bằng số với dấu của chúng bị bỏ qua
phút[x , y]¶So sánh hai giá trị bằng số và trả về giá trị nhỏ nhất
min_mag[x , y]¶So sánh các giá trị bằng số với dấu của chúng bị bỏ qua
trừ[x] ¶Dấu trừ tương ứng với toán tử trừ tiền tố một ngôi trong Python
nhân lên[x , y]¶Trả về tích của x và y
next_minus[x] ¶Trả về số đại diện lớn nhất nhỏ hơn x
next_plus[x] ¶Trả về số biểu diễn nhỏ nhất lớn hơn x
next_toward[x , y]¶Trả về số gần x nhất, theo hướng về phía y
chuẩn hóa[x] ¶Rút gọn x về dạng đơn giản nhất
số_lớp[x] ¶Trả về một dấu hiệu của lớp x
cộng[x] ¶Plus tương ứng với toán tử cộng tiền tố một ngôi trong Python. Thao tác này áp dụng độ chính xác của ngữ cảnh và làm tròn, vì vậy đây không phải là thao tác nhận dạng
sức mạnh[x , y, modulo=None]¶Đưa
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []846 về lũy thừa của
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []847, giảm modulo
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []458 nếu đã cho
Với hai đối số, tính toán
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []459. Nếu
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []846 âm thì
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []847 phải tích phân. Kết quả sẽ không chính xác trừ khi
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []847 là tích phân và kết quả là hữu hạn và có thể được biểu thị chính xác bằng các chữ số 'chính xác'. Chế độ làm tròn của bối cảnh được sử dụng. Kết quả luôn được làm tròn chính xác trong phiên bản Python
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []463 dẫn đến
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []63 và nếu
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []63 không bị mắc kẹt, thì kết quả là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []466
Đã thay đổi trong phiên bản 3. 3. Mô-đun C tính toán
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []467 theo các hàm
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []468 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []469 được làm tròn chính xác. Kết quả được xác định rõ nhưng chỉ "hầu như luôn được làm tròn chính xác".
Với ba đối số, tính toán
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []470. Đối với dạng ba đối số, các hạn chế sau đối với các đối số được giữ
cả ba đối số phải là tích phân
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []847 phải không âmít nhất một trong số
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []846 hoặc>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []847 phải khác không >>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []458 phải khác 0 và có tối đa các chữ số 'chính xác'
Giá trị thu được từ
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []475 bằng với giá trị sẽ thu được bằng cách tính toán
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []470 với độ chính xác không giới hạn, nhưng được tính toán hiệu quả hơn. Số mũ của kết quả bằng 0, bất kể số mũ của
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []846,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []847 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []458. Kết quả luôn chính xácsố lượng hóa[x , y]¶
Trả về một giá trị bằng x [làm tròn], có số mũ là y
cơ số[] ¶Chỉ trả về 10, vì đây là Số thập phân,. ]
phần còn lại[x , y]¶Trả về phần còn lại từ phép chia số nguyên
Dấu của kết quả, nếu khác không, giống như dấu của số bị chia ban đầu
remainder_near[x , y]¶Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []480, trong đó n là số nguyên gần nhất với giá trị chính xác của
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []481 [nếu kết quả là 0 thì dấu của nó sẽ là dấu của x]xoay[x , y]¶
Trả về một bản sao được xoay x, y lần
same_quantum[x , y]¶Trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875 nếu hai toán hạng có cùng số mũscaleb[x , y]¶
Trả về toán hạng đầu tiên sau khi thêm giá trị thứ hai exp của nó
ca[x , y]¶Trả về một bản sao đã dịch chuyển của x, y lần
sqrt[x] ¶Căn bậc hai của một số không âm với độ chính xác theo ngữ cảnh
trừ[x , y]¶Trả về sự khác biệt giữa x và y
to_eng_string[x] ¶Chuyển đổi thành chuỗi, sử dụng ký hiệu kỹ thuật nếu cần số mũ
Ký hiệu kỹ thuật có số mũ là bội số của 3. Điều này có thể để lại tối đa 3 chữ số ở bên trái của vị trí thập phân và có thể yêu cầu thêm một hoặc hai số 0 ở cuối
to_integral_exact[x] ¶Làm tròn thành một số nguyên
to_sci_string[x] ¶Chuyển đổi một số thành một chuỗi bằng cách sử dụng ký hiệu khoa học
Hằng số¶
Các hằng số trong phần này chỉ liên quan đến mô-đun C. Chúng cũng được bao gồm trong phiên bản Python thuần túy để tương thích
32-bit
64-bit
thập phân. MAX_PREC ¶>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []483
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []484thập phân. MAX_EMAX ¶
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []483
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []484thập phân. MIN_EMIN ¶
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []487
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []488thập phân. MIN_ETINY ¶
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []489
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []490thập phân. HAVE_THREADS ¶
Giá trị là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875. Không dùng nữa, vì Python giờ đây luôn có chủ đề
Không dùng nữa kể từ phiên bản 3. 9
thập phân. HAVE_CONTEXTVAR ¶Giá trị mặc định là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875. Nếu Python là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []493, phiên bản C sử dụng bối cảnh cục bộ luồng thay vì ngữ cảnh cục bộ coroutine và giá trị là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876. Điều này nhanh hơn một chút trong một số tình huống ngữ cảnh lồng nhau
Mới trong phiên bản 3. 9. đã nhập ngược về 3. 7 và 3. 8.
Chế độ làm tròn¶
thập phân. VÒNG_TRẦN ¶Vòng về phía
>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']9thập phân. ROUND_DOWN ¶
Làm tròn về số không
thập phân. ROUND_FLOOR ¶Vòng về phía
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []60thập phân. ROUND_HALF_DOWN ¶
Làm tròn đến gần nhất với các mối quan hệ tiến về 0
thập phân. ROUND_HALF_EVEN ¶Làm tròn đến gần nhất với các mối quan hệ sẽ đến số nguyên chẵn gần nhất
thập phân. ROUND_HALF_UP ¶Làm tròn đến gần nhất với các mối quan hệ đi từ số không
thập phân. ROUND_UP ¶Làm tròn từ số 0
thập phân. ROUND_05UP ¶Làm tròn từ 0 nếu chữ số cuối cùng sau khi làm tròn về 0 sẽ là 0 hoặc 5;
Tín hiệu¶
Tín hiệu đại diện cho các điều kiện phát sinh trong quá trình tính toán. Mỗi cái tương ứng với một cờ ngữ cảnh và một trình kích hoạt bẫy ngữ cảnh
Cờ ngữ cảnh được đặt bất cứ khi nào gặp phải điều kiện. Sau khi tính toán, các cờ có thể được kiểm tra cho các mục đích thông tin [ví dụ: để xác định xem một phép tính có chính xác hay không]. Sau khi kiểm tra các cờ, đảm bảo xóa tất cả các cờ trước khi bắt đầu tính toán tiếp theo
Nếu trình kích hoạt bẫy của ngữ cảnh được đặt cho tín hiệu, thì điều kiện sẽ gây ra một ngoại lệ Python được đưa ra. Ví dụ: nếu bẫy
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []64 được đặt, thì ngoại lệ
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []64 sẽ xuất hiện khi gặp điều kiệnlớp thập phân. Đã kẹp ¶
Đã thay đổi số mũ để phù hợp với các ràng buộc biểu diễn
Thông thường, hiện tượng kẹp xảy ra khi một số mũ nằm ngoài giới hạn
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []499 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []233 của ngữ cảnh. Nếu có thể, số mũ được giảm xuống cho phù hợp bằng cách thêm các số 0 vào hệ sốlớp thập phân. DecimalException ¶
Lớp cơ sở cho các tín hiệu khác và lớp con của
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []501lớp thập phân. DivisionByZero ¶
Báo hiệu phép chia của một số không vô hạn cho 0
Có thể xảy ra với phép chia, phép chia modulo hoặc khi nâng một số lên lũy thừa âm. Nếu tín hiệu này không bị giữ lại, trả về
>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']9 hoặc
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []60 với dấu hiệu được xác định bởi đầu vào của phép tínhlớp thập phân. Không chính xác ¶
Cho biết đã xảy ra làm tròn và kết quả không chính xác
Tín hiệu khi các chữ số khác 0 bị loại bỏ trong quá trình làm tròn. Kết quả làm tròn được trả về. Cờ hoặc bẫy tín hiệu được sử dụng để phát hiện khi kết quả không chính xác
lớp thập phân. Hoạt động không hợp lệ ¶Một hoạt động không hợp lệ đã được thực hiện
Cho biết rằng một hoạt động được yêu cầu không có ý nghĩa. Nếu không bị mắc kẹt, trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61. nguyên nhân có thể bao gồm
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []29lớp thập phân. Tràn ¶
tràn số
Cho biết số mũ lớn hơn
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []233 sau khi làm tròn số. Nếu không bị mắc kẹt, kết quả phụ thuộc vào chế độ làm tròn, kéo vào trong đến số hữu hạn có thể biểu diễn lớn nhất hoặc làm tròn ra ngoài thành
>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']9. Trong cả hai trường hợp,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []65 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []66 cũng được báo hiệulớp thập phân. Làm tròn ¶
Làm tròn xảy ra mặc dù có thể không có thông tin bị mất
Báo hiệu bất cứ khi nào làm tròn loại bỏ chữ số; . Nếu không bị mắc kẹt, trả về kết quả không thay đổi. Tín hiệu này được sử dụng để phát hiện mất chữ số có nghĩa
lớp thập phân. Không bình thường ¶Số mũ thấp hơn
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []499 trước khi làm tròn
Xảy ra khi kết quả hoạt động không bình thường [số mũ quá nhỏ]. Nếu không bị mắc kẹt, trả về kết quả không thay đổi
lớp thập phân. Tràn đầy ¶Dòng dưới số với kết quả được làm tròn thành 0
Xảy ra khi một kết quả không bình thường được đẩy về 0 bằng cách làm tròn.
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []65 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []67 cũng được báo hiệulớp thập phân. FloatOperation ¶
Kích hoạt ngữ nghĩa chặt chẽ hơn để trộn số float và số thập phân
Nếu tín hiệu không bị giữ lại [mặc định], cho phép trộn số float và Số thập phân trong hàm tạo
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []515 và tất cả các toán tử so sánh. Cả chuyển đổi và so sánh đều chính xác. Bất kỳ sự xuất hiện nào của một hoạt động hỗn hợp đều được ghi lại âm thầm bằng cách đặt
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []60 trong các cờ ngữ cảnh. Chuyển đổi rõ ràng với
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []517 hoặc
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []518 không đặt cờ
Mặt khác [tín hiệu bị bẫy], chỉ có các phép so sánh đẳng thức và chuyển đổi rõ ràng là im lặng. Tất cả các hoạt động hỗn hợp khác tăng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []60
Bảng dưới đây tóm tắt hệ thống phân cấp tín hiệu
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []40
Ghi chú dấu phẩy động¶
Giảm thiểu lỗi làm tròn với độ chính xác tăng lên¶
Việc sử dụng dấu phẩy động thập phân giúp loại bỏ lỗi biểu diễn thập phân [giúp có thể biểu thị chính xác
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []520];
Ảnh hưởng của sai số làm tròn có thể được khuếch đại bằng cách cộng hoặc trừ các đại lượng gần như bù trừ dẫn đến mất ý nghĩa. Knuth cung cấp hai ví dụ hướng dẫn trong đó số học dấu phẩy động được làm tròn với độ chính xác không đủ gây ra sự phá vỡ các thuộc tính kết hợp và phân phối của phép cộng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []41
Mô-đun
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []4 cho phép khôi phục danh tính bằng cách mở rộng độ chính xác đủ để tránh mất ý nghĩa
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []42
Giá trị đặc biệt¶
Hệ thống số cho mô-đun
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []4 cung cấp các giá trị đặc biệt bao gồm
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []524,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []60,
>>> data = list[map[Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split[]]] >>> max[data] Decimal['9.25'] >>> min[data] Decimal['0.03'] >>> sorted[data] [Decimal['0.03'], Decimal['1.00'], Decimal['1.34'], Decimal['1.87'], Decimal['2.35'], Decimal['3.45'], Decimal['9.25']] >>> sum[data] Decimal['19.29'] >>> a,b,c = data[:3] >>> str[a] '1.34' >>> float[a] 1.34 >>> round[a, 1] Decimal['1.3'] >>> int[a] 1 >>> a * 5 Decimal['6.70'] >>> a * b Decimal['2.5058'] >>> c % a Decimal['0.77']9 và hai số không,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []63 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []62
Vô cực có thể được xây dựng trực tiếp với.
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []202. Ngoài ra, chúng có thể phát sinh từ việc chia cho 0 khi tín hiệu
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []64 không bị mắc kẹt. Tương tự như vậy, khi tín hiệu
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []68 không bị mắc kẹt, vô hạn có thể là kết quả của việc làm tròn vượt quá giới hạn của số lớn nhất có thể biểu thị
Các vô hạn được ký [affine] và có thể được sử dụng trong các phép toán số học khi chúng được coi là các số rất lớn, không xác định. Chẳng hạn, thêm một hằng số vào vô cùng sẽ cho một kết quả vô hạn khác
Một số hoạt động không xác định và trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 hoặc nếu tín hiệu
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []63 bị kẹt, hãy đưa ra một ngoại lệ. Ví dụ:
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []534 trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 có nghĩa là “không phải số”. Loại
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 này không hoạt động và sau khi được tạo, sẽ chạy qua các tính toán khác luôn dẫn đến một
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 khác. Hành vi này có thể hữu ích đối với một loạt phép tính đôi khi thiếu đầu vào — nó cho phép phép tính tiếp tục trong khi gắn cờ các kết quả cụ thể là không hợp lệ
Một biến thể là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []524 báo hiệu thay vì giữ im lặng sau mỗi hoạt động. Đây là một giá trị trả về hữu ích khi một kết quả không hợp lệ cần làm gián đoạn phép tính để xử lý đặc biệt
Hành vi của các toán tử so sánh của Python có thể hơi ngạc nhiên khi có liên quan đến
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61. Một bài kiểm tra về sự bằng nhau khi một trong các toán hạng là yên tĩnh hoặc có tín hiệu
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 luôn trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876 [ngay cả khi thực hiện
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []542], trong khi một bài kiểm tra về sự bất bình đẳng luôn trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []875. Nỗ lực so sánh hai Số thập phân bằng cách sử dụng bất kỳ toán tử nào trong số các toán tử
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []544,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []545,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []546 hoặc
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []547 sẽ tăng tín hiệu
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []63 nếu một trong hai toán hạng là
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 và trả về
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []876 nếu tín hiệu này không bị bẫy. Lưu ý rằng đặc tả số học thập phân chung không chỉ định hành vi so sánh trực tiếp; . 7]. Để đảm bảo tuân thủ các tiêu chuẩn nghiêm ngặt, hãy sử dụng các phương pháp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []854 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []553 thay thế
Các số 0 có dấu có thể là kết quả của các phép tính nằm dưới. Họ giữ dấu hiệu sẽ có kết quả nếu phép tính được thực hiện với độ chính xác cao hơn. Vì độ lớn của chúng bằng 0 nên cả số 0 dương và âm đều được coi là bằng nhau và dấu của chúng là thông tin
Ngoài hai số 0 có dấu phân biệt nhưng bằng nhau, còn có nhiều cách biểu diễn khác nhau của số 0 với độ chính xác khác nhau nhưng có giá trị tương đương. Điều này cần một chút để làm quen. Đối với một người đã quen với các biểu diễn dấu phẩy động được chuẩn hóa, không rõ ràng ngay lập tức rằng phép tính sau đây trả về một giá trị bằng 0
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []43
Làm việc với chủ đề¶
Hàm
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 truy cập một đối tượng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []265 khác cho mỗi luồng. Có ngữ cảnh luồng riêng biệt có nghĩa là các luồng có thể thực hiện các thay đổi [chẳng hạn như
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []556] mà không can thiệp vào các luồng khác
Tương tự như vậy, hàm
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []69 sẽ tự động gán mục tiêu của nó cho chuỗi hiện tại
Nếu
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []69 chưa được gọi trước
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61, thì
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61 sẽ tự động tạo ngữ cảnh mới để sử dụng trong chuỗi hiện tại
Bối cảnh mới được sao chép từ bối cảnh nguyên mẫu có tên là DefaultContext. Để kiểm soát các giá trị mặc định sao cho mỗi luồng sẽ sử dụng các giá trị giống nhau trong toàn bộ ứng dụng, hãy sửa đổi trực tiếp đối tượng DefaultContext. Điều này nên được thực hiện trước khi bất kỳ luồng nào được bắt đầu để không xảy ra tình trạng chạy đua giữa các luồng gọi
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []61. Ví dụ
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []44
Công thức nấu ăn¶
Dưới đây là một vài công thức đóng vai trò là các hàm tiện ích và minh họa các cách để làm việc với lớp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []45
Câu hỏi thường gặp về số thập phân¶
Q. Thật khó để gõ
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []563. Có cách nào để giảm thiểu việc nhập khi sử dụng trình thông dịch tương tác không?
A. Một số người dùng viết tắt hàm tạo chỉ bằng một chữ cái
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []46
Q. Trong ứng dụng điểm cố định có hai chữ số thập phân, một số đầu vào có nhiều vị trí và cần được làm tròn. Những người khác không được phép có thừa chữ số và cần được xác thực. Những phương pháp nên được sử dụng?
A. Phương thức
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []67 làm tròn đến một số chữ số thập phân cố định. Nếu bẫy
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []65 được đặt, nó cũng hữu ích cho việc xác thực
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []47
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []48
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []49
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []50
Q. Sau khi tôi có hai giá trị đầu vào hợp lệ, làm cách nào để duy trì giá trị bất biến đó trong toàn bộ ứng dụng?
A. Một số phép toán như cộng, trừ và nhân với một số nguyên sẽ tự động bảo toàn điểm cố định. Các hoạt động khác, như phép chia và phép nhân không nguyên, sẽ thay đổi số chữ số thập phân và cần được theo dõi bằng bước
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []67
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []51
Khi phát triển các ứng dụng điểm cố định, việc xác định các chức năng để xử lý bước
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []67 sẽ thuận tiện
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []52
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []53
Q. Có nhiều cách để thể hiện cùng một giá trị. Các số ________ 4568, ________ 4569, ________ 4570 và ________ 4571 đều có cùng giá trị ở các độ chính xác khác nhau. Có cách nào để chuyển đổi chúng thành một giá trị chuẩn có thể nhận biết được không?
A. Phương pháp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []572 ánh xạ tất cả các giá trị tương đương với một đại diện duy nhất
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []54
Q. Một số giá trị thập phân luôn in với ký hiệu số mũ. Có cách nào để có được một đại diện không theo cấp số nhân?
A. Đối với một số giá trị, ký hiệu hàm mũ là cách duy nhất để biểu thị số vị trí quan trọng trong hệ số. Ví dụ: biểu thị
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []573 dưới dạng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []574 giữ giá trị không đổi nhưng không thể hiển thị ý nghĩa hai vị trí của bản gốc
Nếu một ứng dụng không quan tâm đến việc theo dõi ý nghĩa, có thể dễ dàng loại bỏ số mũ và số 0 ở cuối, làm mất ý nghĩa nhưng vẫn giữ nguyên giá trị
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []55
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []56
Q. Có cách nào để chuyển đổi số float thông thường thành
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []806 không?
A. Có, bất kỳ số dấu phẩy động nhị phân nào cũng có thể được biểu thị chính xác dưới dạng Số thập phân mặc dù một chuyển đổi chính xác có thể chính xác hơn so với trực giác sẽ đề xuất
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []57
Q. Trong một phép tính phức tạp, làm cách nào để đảm bảo rằng tôi không nhận được kết quả giả do không đủ độ chính xác hoặc làm tròn bất thường
A. Mô-đun thập phân giúp dễ dàng kiểm tra kết quả. Cách thực hành tốt nhất là chạy lại các phép tính bằng cách sử dụng độ chính xác cao hơn và với các chế độ làm tròn khác nhau. Các kết quả khác nhau nhiều cho thấy độ chính xác không đủ, các vấn đề về chế độ làm tròn, đầu vào không ổn định hoặc thuật toán không ổn định về mặt số
Q. Tôi nhận thấy rằng độ chính xác của ngữ cảnh được áp dụng cho kết quả của hoạt động nhưng không áp dụng cho đầu vào. Có điều gì cần chú ý khi trộn các giá trị của các độ chính xác khác nhau không?
A. Đúng. Nguyên tắc là tất cả các giá trị được coi là chính xác và số học trên các giá trị đó cũng vậy. Chỉ có kết quả được làm tròn. Ưu điểm của đầu vào là “những gì bạn nhập là những gì bạn nhận được”. Một nhược điểm là kết quả có thể trông kỳ quặc nếu bạn quên rằng đầu vào chưa được làm tròn
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []58
Giải pháp là tăng độ chính xác hoặc buộc làm tròn đầu vào bằng cách sử dụng phép toán cộng một ngôi
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []59
Ngoài ra, đầu vào có thể được làm tròn khi tạo bằng phương pháp
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []576
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []0
Q. Việc triển khai CPython có nhanh với số lượng lớn không?
A. Đúng. Trong triển khai CPython và PyPy3, các phiên bản C/CFFI của mô-đun thập phân tích hợp thư viện libmpdec tốc độ cao để có độ chính xác tùy ý làm tròn số học dấu phẩy động thập phân chính xác 1.
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []577 sử dụng phép nhân Karatsuba cho các số cỡ trung bình và Biến đổi Lý thuyết Số cho các số rất lớn
Bối cảnh phải được điều chỉnh cho số học chính xác tùy ý chính xác.
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []499 và
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []233 phải luôn được đặt thành giá trị tối đa,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []580 phải luôn là 0 [mặc định]. Cài đặt
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []279 yêu cầu một số cẩn thận
Cách tiếp cận đơn giản nhất để thử số học bignum là sử dụng giá trị lớn nhất cho cả
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []279 2
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []1
Đối với kết quả không chính xác,
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []291 quá lớn trên nền tảng 64 bit và bộ nhớ khả dụng sẽ không đủ
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []2
Trên các hệ thống có phân bổ tổng thể [e. g. Linux], một cách tiếp cận phức tạp hơn là điều chỉnh
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []279 theo dung lượng RAM khả dụng. Giả sử bạn có 8GB RAM và mong đợi 10 toán hạng đồng thời sử dụng tối đa 500MB mỗi toán hạng
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []3
Nói chung [và đặc biệt là trên các hệ thống không có phân bổ tổng thể], nên ước tính các giới hạn thậm chí chặt chẽ hơn và đặt bẫy
>>> Decimal["1e9999999999999999999"] Traceback [most recent call last]: File "", line 1, in decimal.InvalidOperation: []65 nếu tất cả các tính toán được mong đợi là chính xác