SoFunction
Updated on 2025-03-02

About the use of Math library in Python

Math library overview

mathThe library isPythonThe built-in mathematical function library provided is that because complex types are often used for scientific calculations, they are not commonly used in calculations, somathThe library does not support complex types, only integer and floating point operations.

mathThe library provides a total of4mathematical constants44functions.44The functions are divided into4Class, including16numerical values ​​represent functions,8logarithmic function of power,16triangular logarithmic functions4A special function of higher quality.

mathThere are many functions in the library. In the process of learning, we only need to understand the function functions one by one and remember individual commonly used functions. In actual programming, if necessarymathLibrary, you can view it at any timemathLibrary quick reference.

mathThe functions in the library cannot be used directly, and you need to use reserved words first.importReference to this library, the reference method is as follows.

The first type:import mathrightmathFunctions in the library are adoptedmath.<b>()Formal use

For example:

import math
a=(10.2)		#Solid upwardprint(a)

11

The second type:from math import <function name>rightmathFunctions in the library can be directly used<function name>()Formal use

For example:

from math import floor
a=floor(10.2)		#Fill downprint(a)

10

Another form of the second is from math import * . If this method is introducedmathLibrary,mathAll functions in the library can be used<function name>()Use directly in the form.

mathReferences to libraries and other function libraries can be freely selected to implement these two methods.

math library analysis

mathMathematical constants of the library (4 in total)

constant describe
Pi, value is3.141 592 653 589 793
Natural logarithm, value is2.718 281 828 459 045
Positive infinity, negative infinity is-
Non-floating point marks,NaN (Not a Number)

mathNumerical representation functions of the library (16 in total)

function describe
(x) Returns the absolute value of x
(x,y) Return to the touch of x and y
([x,y,…]) Floating point numbers are precisely summed
(x) Round upward, returning the smallest integer not less than x
(x) Round downward, returning the maximum integer no greater than x
(x) Returns the factorial of x, if x is a decimal or a negative number, return ValueError
(a,b) Returns the greatest common divisor of a and b
(x) Indicates that x = m*2e, returns (m,e), when x = 0, returns (0.0,0)
(x,i) Return the value of x*2i, (x) function inverse operation
(x) Returns the fractional and integer parts of x
(x) Returns the integer part of x
(x,y) Replace the positive and negative sign of the value x with the positive and negative sign of the value y
(a,b) Compare the similarities of a and b, return True or False
(x) Return True when x is not infinity or NaN; otherwise, return False
(x) Return True when x is positive and negative infinity; otherwise, return False
(x) Return True when x is NaN; otherwise return False

([x,y,…])Functions are very useful in mathematical summing operations, such as:

a=0.1 + 0.2 + 0.3
print(a)

0.6000000000000001

import math
a=([0.1, 0.2, 0.3])
print(a)

0.6

Floating point numbers, such as 0.1, 0.2, and 0.3, inPythonThe internal representation of the interpreter contains a precision mantissa of several bits after a decimal point. When the floating point number is calculated, this precision mantissa may affect the output result. Therefore, when it comes to floating point calculation and result comparison, it is recommended to usemathFunctions provided by the library, not directly usedPythonProvided operator.

mathThe power logarithmic function of the library (total 8)

function Mathematical representation describe
(x,y) xy Returns the y power of x
(x) ex Returns the x power of e,eIt's a natural logarithm
(x) ex-1 returneofxPower subtraction1
(x) √x returnxSquare root
(x[,base]) logbasex returnxlogarithmic value, enter onlyxWhen, return the natural logarithm, i.e.ln x
math.log1p(x) ln(1+x) return1+xNatural logarithmic value
math.log2(x) log2x returnxof2Logarithmic value
math.log10(x) log10x returnxof10Logarithmic value

mathThe library's triangular operation functions (16 in total)

function Mathematical representation describe
(x) none anglexradian value to angle value
(x) none anglexAngle value radian value
(x,y) √x2+y2 Returns the distance from the coordinate to the origin
(x) sin x returnxthe sine function value,xIt's the radian value
(x) cos x returnxThe cosine function value,xIt's the radian value
(x) tan x returnxtangent function value,xIt's the radian value
(x) arcsin x returnxthe inverse sine function value,xIt's the radian value
(x) arccos x returnxthe inverse cosine function value,xIt's the radian value
(x) arctan x returnxthe arctangent function value,xIt's the radian value
math.atan2(x,y) arctan y/x returny/xthe arctangent function value,xIt's the radian value
(x) sinh x returnxHyperbolic sine function value
(x) cosh x returnxHyperbolic cosine function value
(x) tanh x returnxHyperbolic tangent function value
(x) atcsinh x returnxThe value of the inverse hyperbolic sine function
(x) arccosh x returnxThe value of the inverse hyperbolic cosine function
(x) arctanh x returnxThe value of the inverse hyperbolic tangent function

mathHigher special functions of the library (4 in total)

(x) Gaussian error function, applied to probability theory, statistics and other fields
(x) The rest-complement Gaussian error function,(x)= 1 - (x)
(x) Gamma function, also known as Euler's second integral function
(x) Natural logarithm of gamma function

This is the end of this article about the use of the Math library in Python. For more related content of Python Math library, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!