Why do you say floating point numbers lack precision?
Before we start this article, let's talk about why floating-point numbers lack precision; it's not really a Python problem, but rather a contradiction between the infinite precision of real numbers and the limited memory of computers.
For example, if I can only use integers (i.e., only to the nearest digit, and floating-point numbers in computers only have finite precision, such as double-precision floating-point numbers in C, which have a precision of 52 binary digits), then I can only approximate any real number (with infinite precision) by rounding it up or down.
For example, 1.2 I would denote as 1, 2.4 as 2, 3.6 as 4.
So?
When counting 1.2 - 1.2, due to computer representation issues, I counted what was actually 1 - 1, which turned out to be 0, which happened to be correct;
When counting 1.2 + 1.2 - 2.4, due to a computer representation problem, I counted what was actually 1 + 1 - 2, which turned out to be 0. Once again, I got it right;
But when calculating 1.2 + 1.2 + 1.2 - 3.6, due to a problem with the computer's representation, I actually calculated 1 + 1 + 1 - 4, and the result was -1, so my luck was not that good!
Here 1.2, 2.4, 3.6 are the equivalent of 0.1, 0.2, and 0.3 in your question, and 1, 2, and 4 are the values that actually do the math inside the computer, am I clear?
For the rest, see the IEEE 754 floating point standard, like CSAPP chapter 2 or something (though I guess you're not interested in reading it).
Another:Not only is there an error in the representation of floating-point numbers within the computer, but there may also be an error in the operation itself. For example, the integer 2 can be accurately represented in the computer, but to calculate the root 2 there is an error; another example is the division of two floating-point numbers, originally the two numbers are accurately represented, but the result of the precision of the division of the computer beyond the scope of the representation of real numbers, and then there is an error.
Well, without further ado, here is the body of this article:
set in motion
A common problem with floating point numbers is that they cannot represent decimal numbers exactly.
>>> a = 4.2 >>> b = 2.1 >>> a + b 6.300000000000001 >>> (a + b) == 6.3 False >>>
This is due to the fact that the underlying CPU andIEEE 754 The standard is characterized by performing arithmetic in its own floating-point units. A seemingly infinite number of decimals is infinite in the computer's binary representation.
Normally, this little bit of small error is allowed to exist. If this kind of error cannot be tolerated (e.g. in finance), then it is time to consider some ways to solve this problem.
Decimal
There are no small errors with this module.
>>> from decimal import Decimal >>> a = Decimal('4.2') >>> b = Decimal('2.1') >>> a + b Decimal('6.3') >>> print(a + b) 6.3 >>> (a + b) == Decimal('6.3') True
Although the code may seem strange, using strings to represent numbers, Decimal supports all common mathematical operations. The decimal module allows you to control every aspect of the calculation, including the number of digits and rounding. Before doing so, you need to create a temporary context to change this setting:
>>> from decimal import Decimal, localcontext >>> a = Decimal('1.3') >>> b = Decimal('1.7') >>> print(a / b) 0.7647058823529411764705882353 >>> with localcontext() as ctx: ... = 3 ... print(a / b) ... 0.765 >>> with localcontext() as ctx: ... = 50 ... print(a / b) ... 0.76470588235294117647058823529411764705882352941176 >>>
Because of Decimal's high precision numbers it is natural to use strings for presentation and transit.
summarize
In general, when it comes to finance, even a small error is not allowed in the calculation process. Therefore the decimal module provides a way to solve such problems.
Well, the above is the full content of this article, I hope that the content of this article on your learning or work has a certain reference learning value, if there are questions you can leave a message to exchange, thank you for my support.