preamble
Recently, when I encountered floating point arithmetic while using Python, I found that I often encountered the following.
The above situation is mainly due to the fact that floating point numbers are actually stored in binary in computers, and some numbers are imprecise.
For example: 0.1 is decimal, when converted to binary it is an infinite loop number:
0.00011001100110011001100110011001100110011001100110011001100
And python is to double precision (64) bits to save the floating point number, the extra bits will be truncated, so see is 0.1, but the actual save on the computer is not the exact 0.1, involved in arithmetic, there may be a point of error, especially inside the financial neighborhood, the precision is more demanding, how to get a specific bit precision value in Python?
[Method 1] Utilizing the ROUND method.
[Method 2] Utilizing the DECIMAL module.
Readers interested in more Python related content can check out this site's topic: theSummary of Python mathematical operations techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques》
summarize
The above is the entire content of this article, I hope that the content of this article on your learning or work can bring some help, if there are questions you can leave a message to exchange, thank you for my support.