Home >Backend Development >Python Tutorial >Introduction to carry and rounding of decimals in Python (with code)
This article brings you an introduction to the carrying and rounding of decimals in Python (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Basic knowledge preparation
Odd rounding and even rounding, also known as the rounding rule and Banker's Rounding, is a A counting retention method is a numerical rounding rule. From a statistical point of view, "odd and even rounding" is more accurate than "rounding": in a large number of operations, because some of the results after rounding become larger and some become smaller, the mean error of the rounded results will be tends to zero. Instead of carrying every fifth like rounding, the result will be biased toward a large number, causing errors to accumulate and thus produce systematic errors. "Odd rounding into even rounding" minimizes the impact of rounding errors on the measurement results.
Rounding off for values (rounding off for values) - Before performing specific numerical operations, by omitting the last few digits of the original value, adjust the remaining last digits so that the last The process in which the obtained value is closest to the original value.
Infinity Infinity
NaN (Not a Number, not a number) is a type of value of numeric data type in computer science, which means undefined or unrepresentable value. Often used in floating point operations. NaN was first introduced in the IEEE 754 floating-point standard in 1985. In floating-point number operations, the concepts of NaN and infinity are different, although both are special values when using floating-point numbers to represent real numbers. Invalid Operation is also different from arithmetic overflow (which may return infinity) and arithmetic underflow (which may return the smallest general value, special value, zero, etc.). In IEEE 754-1985, NaN is represented by the exponent part being all 1 and the decimal part being non-zero. Taking the NaN of a 32-bit IEEE single-precision floating point number as an example, the bitwise representation is: S111 1111 1AXX XXXX XXXX XXXX XXXX XXXX, S is the sign bit, and the value of the sign bit S is irrelevant
Do it in python When performing precise numerical operations, the decimal
module is generally used to perform decimal operations, which uses the decimal number decimal number, context arithmetic context parameter, and signals signal information
We found that using round()
When rounding decimals, the rounding is not the desired one. The reason is that the rounding rule adopts the odd and even rounding (rounding) method. Simply put, integer If the part is an odd number, round it off. If it is an even number, use the rounding method, and this rule belongs to the numeric rounding
rule
2. quantize
quantize`(*exp* [,*rounding* [,*context* [,*watchexp* ] ] ] )
Returns a value equal to the first operand after rounding, with the exponent of the second operand.
>>> Decimal('1.41421356').quantize(Decimal('1.000')) Decimal('1.414')
3. Implement rounding
After rounding, return a value equal to the first operand and having the exponent of the second operand. The exponent of this exp is the exponent of the number on the left, exponent
# 实现四舍五入的方法 >>> from decimal import Decimal, ROUND_HALF_UP >>> Decimal('0.375').quantize(Decimal('0.00'), rounding=ROUND_HALF_UP) Decimal('0.38') >>> Decimal('0.125').quantize(Decimal('0.00'), rounding=ROUND_HALF_UP) Decimal('0.13')
[Related recommendations: Python video tutorial]
The above is the detailed content of Introduction to carry and rounding of decimals in Python (with code). For more information, please follow other related articles on the PHP Chinese website!