"Python" questions about the precision control of decimal points in Python
Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
Floating point numbers are expressed in native double precision (64 bit) of floating point numbers on the machine. Provides approximately 17 digits of accuracy and an index ranging from -308 to 308. Same as the double type in C language. Python does not support 32-bit single-precision floating-point numbers. If the program needs precise control of interval and number precision, you can consider using numpy extension library.
Python 3.X defaults to a precision of 17 digits for floating point numbers.
Popular explanations about single and double precision:
For single-precision and double-precision types, the type specifier is float single-precision specifier and double double-precision specifier. In Turbo C, the single-precision type occupies 4 bytes (32 bits) of memory space, and its value range is 3.4E-38 to 3.4E + 38, which can only provide seven significant digits. The double-precision type occupies 8 bytes (64 bits) of memory space, and its value range is 1.7E-308 ～ 1.7E + 308, which can provide 16 significant digits.
Requires less precision
Convert high-precision floating-point numbers to low-precision floating-point numbers.
1.round () built-in method
This is the most used, just read the explanation of the use of round (), it is not very easy to understand. round () is not a simple rounding method.
For the built-in types supporting round (), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round (0.5 ) and round (-0.5) are 0, and round (1.5) is 2).
>>> round (2.5)
>>> round (1.5)
>>> round (2.675)
>>> round (2.675, 2)
round () If there is only one number as the parameter, when no number of digits is specified, an integer is returned, and it is the closest integer (similar to rounding in this regard). But when .5 appears, the distance between the two sides is the same, round () takes the even number close, which is why round (2.5) = 2. When specifying the number of decimal places, the rule of rounding is generally used, but in the case of .5, if the small tree before the number of digits to be selected is odd, it is directly discarded, and if the even number is rounded up. See the example below:
>>> round (2.635, 2)
>>> round (2.645, 2)
>>> round (2.655, 2)
>>> round (2.665, 2)
>>> round (2.675, 2)
2. Use formatting
The effect is the same as round ().
>>> a = ("% .2f"% 2.635)
>>> a = ("% .2f"% 2.645)
>>> a = int (2.5)
Accuracy analysis requiring more than 17 digits
Python defaults to a precision of 17 decimal places, but there is a problem here, what should we do when our calculation needs to use a higher precision (more than 17 decimal places)?
1. Use formatting (not recommended)
>>> a = "% .30f"% (1/3)
It can be displayed, but it is not accurate, and the numbers behind it are often meaningless.
2. Use the decimal module with high precision and cooperate with getcontext
>>> from decimal import *
>>> print (getcontext ())
Context (prec = 28, rounding = ROUND_HALF_EVEN, Emin = -999999, Emax = 999999, capitals = 1, clamp = 0, flags = , traps = [InvalidOperation, DivisionByZero, Overflow])
>>> getcontext (). prec = 50
>>> b = Decimal (1) / Decimal (3)
>>> c = Decimal (1) / Decimal (17)
>>> float (c)
The precision of the default context is 28 bits, which can be set to 50 bits or even higher. In this way, when analyzing complex floating-point numbers, you can have higher precision that you can control. In fact, you can pay attention to the rounding = ROUND_HALF_EVEN parameter in the context. ROUND_HALF_EVEN, when half, close to even.
About decimals and rounding
Since decimals are mentioned, they must be integers. These functions are generally used for rounding:
1. round ()
I won't say this, I have already talked about it. It must be noted that it is not a simple rounding, but a ROUND_HALF_EVEN strategy.
2. The ceil (x) of the math module
Take the smallest integer greater than or equal to x.
3. The floor (x) of the math module
Go to the largest integer less than or equal to x.
[Python] About the decimal point precision control in Python
Tags: note support visio native mat return flags context opera
Original address: http://www.cnblogs.com/yanglang/p/7976118.html
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to firstname.lastname@example.org. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
and provide relevant evidence. A staff member will contact you within 5 working days.