"Python" questions about the precision control of decimal points in Python
Source: Internet
Author: User
basis
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)
2
>>> round (1.5)
2
>>> round (2.675)
3
>>> round (2.675, 2)
2.67
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)
2.63
>>> round (2.645, 2)
2.65
>>> round (2.655, 2)
2.65
>>> round (2.665, 2)
2.67
>>> round (2.675, 2)
2.67
2. Use formatting
The effect is the same as round ().
>>> a = ("% .2f"% 2.635)
>>> a
‘2.63’
>>> a = ("% .2f"% 2.645)
>>> a
‘2.65’
>>> a = int (2.5)
>>> a
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)
>>> a
‘0.333333333333333314829616256247’
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)
>>> b
Decimal (‘0.33333333333333333333333333333333333333333333333’)
>>> c = Decimal (1) / Decimal (17)
>>> c
Decimal (‘0.058823529411764705882352941176470588235294117647059’)
>>> float (c)
0.058823529411764705
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.
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:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.