Detailed description of precise floating point operations in python, and detailed description of python point operations
Why do floating point numbers lack accuracy?
Before starting this article, let's talk about why floating point numbers lack accuracy. In fact, this is not a Python issue, but a conflict between the infinite precision of real numbers and the limited memory of computers.
For example, if I can only use an integer (that is, it is only accurate to a single bit, and the floating point number in the computer has only limited precision, take the double-precision floating point number double in C language as an example, the precision is 52 binary bits. to represent any real number (infinite precision), I can only represent it by rounding.
For example, 1.2 is represented as 1, 2.4 is represented as 2, and 3.6 is represented as 4.
So?
In the calculation of 1.2-1.2, due to the computer's representation, I actually calculated 1-1 and the result was 0, which happened to be correct;
In the calculation of 1.2 + 1.2-2.4, I calculated 1 + 1-2 and the result was 0, which is correct again;
However, when we calculate 1.2 + 1.2 + 1.2-3.6, I calculated 1 + 1 + 1-4 due to a computer problem. The result is-1, luck is not so good!
Here, 1.2, 2.4, and 3.6 are equivalent to 0.1, 0.2, 0.3, 1, 2, and 4 in your problem. They are actually calculated inside the computer, have I made it clear?
For more information, see the IEEE 754 floating point standard, such as the second chapter of CSAPP (although you are not interested in reading it ).
In addition:Not only does the representation of floating point numbers in the computer have errors, but operations may also have errors. For example, integer 2 can be accurately expressed in a computer, but an error occurs when the root number 2 is calculated. For example, if two floating point numbers are separated, both numbers are exactly represented, however, the accuracy of the division result is beyond the representation range of the real number in the computer, and then there is an error.
Well, I will not talk much about it. Let's start with the text of this article:
Start
A common problem with floating point numbers is that they cannot accurately represent decimal numbers.
>>> a = 4.2>>> b = 2.1>>> a + b6.300000000000001>>> (a + b) == 6.3False>>>
This is because the underlying CPU and IEEE 754 standards use their own floating point units to perform arithmetic features. It seems that there are infinite decimal places in the binary representation of the computer.
Generally, this small error is allowed to exist. If this error cannot be tolerated (for example, in the financial sector), we need to consider some ways to solve this problem.
Decimal
This module does not produce any small error.
>>> from decimal import Decimal>>> a = Decimal('4.2')>>> b = Decimal('2.1')>>> a + bDecimal('6.3')>>> print(a + b)6.3>>> (a + b) == Decimal('6.3')True
Although the Code looks odd and uses strings to represent numbers, Decimal supports all common mathematical operations. The decimal module allows you to control every aspect of computing, including the number of digits and rounding. Before doing so, you need to create a temporary context environment 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:... ctx.prec = 3... print(a / b)...0.765>>> with localcontext() as ctx:... ctx.prec = 50... print(a / b)...0.76470588235294117647058823529411764705882352941176>>>
Since the high-precision Decimal numbers are naturally displayed and transitioned using strings.
Summary
In general, when it comes to the financial sector, even a small error is not allowed in the calculation process. Therefore, the decimal module provides a solution to this problem.
Well, the above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.