In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to use Python mathematics-related modules". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Math module
Math library is a built-in mathematical class function library provided by Python, because complex types are often used in scientific calculations, and general calculations are not commonly used, so math libraries do not support complex types, only integer and floating-point operations.
Import math1, mathematical constant constant explain example math.pi pi π > > math.pi output result: 3.141592653589793math.e natural constant e > math.e output result: 2.718281828459045math.inf positive infinity, negative infinity:-math.inf > math.inf output result: infmath.nan non-floating point mark, NaN > math.nan output result: nan2, common function math.ceil (floating point number)
Rounding up operation; return value: integer
> import math > math.ceil (13.14) 14 > math.ceil (9.9) 10 > math.ceil (19) # invalid integer 19math.floor (floating point)
Rounding down operation; return value: integer
> import math > math.floor (13.14) 13 > math.floor (9.9) 9 > math.floor (19) # invalid integer 19round (floating point)
Rounding operation; return value: integer
> import math > round (13.14) 13 > round (9.9) 10 > round (11.936, 2) # the way to retain two decimal places is 11.94 > round (9) # invalid integer 9math.fabs (numeric value)
Get numeric absolute value operation; return value: floating point number
> import math > math.fabs (- 9) 9.0 > math.fabs (9) 9.0 > math.fabs (- 9.9) 9.9 > math.fabs (9.9) 9.9abs (numerical value)
Get numeric absolute value operation; return value: integer, floating point number (depending on the type of original data)
> import math > abs (- 9) 9 > abs (- 9.9) 9.9math.fmod (x, y)
Returns the remainder of xUnix y; return value: floating point number
> import math > math.fmod (4,2) 0.0 > math.fmod (5,2) 1.0 > math.fmod (10,3) 1.0math.pow (base, power)
Calculates the Nth power of a number; return value: floating point type
> import math > math.pow (2Magne4) 16.0 > math.pow (3Magne2) 9.0 > math.pow (5,3) 125.0math.sqrt (numerical value)
Square; return value: floating point number
> import math > math.sqrt (9) 3.0 > math.sqrt (4) 2.0 > math.sqrt (16) 4.0fsum (sequence)
Returns the sum of all elements in the sequence; return value: floating point number
> import math > math.fsum ((1,2,3,4,5)) 15.0 > math.fsum (range (1Magazine 11)) 55.0 > math.fsum (range (1101)) 5050.0sum (sequence)
Sum the values of a sequence; return value: numeric type (varies according to the type of values in the sequence)
> import math > sum 15 > sum (range (1jue 11). ) 55 > sum ([1.0, 2.0, 3.0, 4.0, 5.0]) 15.0math.modf (number)
Split a floating point number into a tuple consisting of decimal and integer parts; return value: tuple
> import math > math.modf (10.1) (0.0999999999964,10.0) > math.modf (9.9) (0.900000000004,9.0) > math.modf (9) (0.0,9.0) math.trunc (floating point)
Returns the integer portion of a floating-point number; return value: integer
> import math > math.trunc (2. 1) 2 > math.trunc (9. 9) 9 > math.trunc (10. 0) 10math.copysign (value 1, value 2)
Copy the plus or minus sign of the second number to the first number; return value: floating point number (value 1 sign is the plus or minus sign of value 2)
> import math > math.copysign (- 2,1) 2.0 > math.copysign (2memmer1)-2.0math.actorial (x)
Returns the factorial of x, and if x is not an integer or a negative number, ValueError; returns the value: integer
> import math > math.factorial (4) 24 > math.factorial (3) 6 > math.factorial (1) 1math.gcd (x, y)
Returns the maximum common divisor of integers x and y; return value: integer
> import math > math.gcd (2Magne4) 2 > math.gcd (3LING9) 3 > math.gcd (9L6) 3BIS, decimal module
The decimal module provides a Decimal data type for floating-point calculations. Compared to the built-in binary floating point implementation float this type is useful for financial applications and other situations that require accurate decimal representation, control precision, control rounding to meet legal or regulatory requirements, ensure decimal accuracy, or where the user wants the results to be consistent with manual calculations. Decimal reproduces manual mathematical operations, which ensures that binary floating-point numbers cannot precisely guarantee the accuracy of some data. High precision enables Decimal to perform modular operations and equivalence tests that binary floating point numbers cannot do.
1. When to use decimal
The addition of small numbers in python may result in incorrect results, which is due to the accuracy of scientific calculation.
As above: the value we need is 5.03. if we need to deal with this problem, we need to use the decimal module.
2. Use decimal
Setting precision: decimal.getcontext () .prec = num (num is the number of significant digits)
> import decimal > decimal.getcontext (). Prec = 3 > print (decimal.Decimal (2.02) + decimal.Decimal (3.01)) 5.03 > decimal.getcontext (). Prec = 2 > print (decimal.Decimal (2.02) + decimal.Decimal (3.01)) 5.0
Set the number of decimal places: quantize ()
Import decimalprint (decimal.Decimal (1.1234567890) .quantize (decimal.Decimal ("0.000")) # sets 3 decimal places print (decimal.Decimal (1.1234567890) .quantize (decimal.Decimal ("0.000")) # sets 2 decimal places print (decimal.Decimal (1.1234567890) .quantize (decimal.Decimal ("0.000")) # sets 1 decimal place
Output result:
1.1231.121.1 "how to use Python Mathematics related Modules" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.