📜  Python数学模块

📅  最后修改于: 2022-05-13 01:54:22.322000             🧑  作者: Mango

Python数学模块

有时,在处理某种金融或科学项目时,有必要在项目中实施数学计算。 Python提供了math 模块来处理此类计算。 Math 模块提供了处理基本运算(如加法(+)、减法(-)、乘法(*)、除法(/))和高级运算(如三角函数、对数函数、指数函数)的函数。

在本文中,我们将借助包含在示例的帮助下解释的函数的庞大数据集,从基础到高级学习数学模块。

数学模块提供的常量

Math 模块提供了各种常数的各种值,如 pi、tau。拥有这样的常量可以节省每次我们想要使用每个常量的值的时间,而且也非常精确。数学模块提供的常量是 –

  • 欧拉数
  • 圆周率
  • 无限
  • 不是数字 (NaN)

让我们详细查看每个常量。

欧拉数

math.e常量返回欧拉数:2.71828182846。



句法:

例子:

Python3
# Import math Library
import math
 
# Print the value of Euler e
print (math.e)


Python3
# Import math Library
import math
 
# Print the value of pi
print (math.pi)


Python3
# Import math Library
import math
 
# radius of the circle
r = 4
 
# value of pie
pie = math.pi
 
# area of the circle
print(pie * r * r)


Python3
# Import math Library
import math
 
# Print the value of tau
print (math.tau)


Python3
# Import math Library
import math
 
# Print the positive infinity
print (math.inf)
 
# Print the negative infinity
print (-math.inf)


Python3
# Import math Library
import math
 
print (math.inf > 10e108)
print (-math.inf < -10e108)


Python3
# Import math Library
import math
 
# Print the value of nan
print (math.nan)


Python3
# Python code to demonstrate the working of
# ceil() and floor()
 
# importing "math" for mathematical operations
import math
 
a = 2.3
 
# returning the ceil of 2.3
print ("The ceil of 2.3 is : ", end="")
print (math.ceil(a))
 
# returning the floor of 2.3
print ("The floor of 2.3 is : ", end="")
print (math.floor(a))


Python3
# Python code to demonstrate the working of
# factorial()
 
# importing "math" for mathematical operations
import math
 
a = 5
 
# returning the factorial of 5
print("The factorial of 5 is : ", end="")
print(math.factorial(a))


Python3
# Python code to demonstrate the working of
# gcd()
 
# importing "math" for mathematical operations
import math
 
a = 15
b = 5
 
# returning the gcd of 15 and 5
print ("The gcd of 5 and 15 is : ", end="")
print (math.gcd(b, a))


Python3
# Python code to demonstrate the working of
# fabs()
 
# importing "math" for mathematical operations
import math
 
a = -10
 
# returning the absolute value.
print ("The absolute value of -10 is : ", end="")
print (math.fabs(a))


Python3
# Python3 code to demonstrate
# the working of exp()
import math
 
# initializing the value
test_int = 4
test_neg_int = -3
test_float = 0.00
 
# checking exp() values
# with different numbers
print (math.exp(test_int))
print (math.exp(test_neg_int))
print (math.exp(test_float))


Python3
# Python code to demonstrate pow()
# version 1
 
print ("The value of 3**4 is : ",end="")
 
# Returns 81
print (pow(3,4))


Python3
# Python code to demonstrate the working of
# logarithm
 
# importing "math" for mathematical operations
import math
 
 
# returning the log of 2,3
print ("The value of log 2 with base 3 is : ", end="")
print (math.log(2,3))
 
# returning the log2 of 16
print ("The value of log2 of 16 is : ", end="")
print (math.log2(16))
    
# returning the log10 of 10000
print ("The value of log10 of 10000 is : ", end="")
print (math.log10(10000))


Python3
# Python3 program to demonstrate the
# sqrt() method
 
# import the math module
import math
 
# print the square root of 0
print(math.sqrt(0))
 
# print the square root of 4
print(math.sqrt(4))
 
# print the square root of 3.5
print(math.sqrt(3.5))


Python3
# Python code to demonstrate the working of
# sin(), cos(), and tan()
 
# importing "math" for mathematical operations
import math
 
a = math.pi/6
 
# returning the value of sine of pi/6
print ("The value of sine of pi/6 is : ", end="")
print (math.sin(a))
 
# returning the value of cosine of pi/6
print ("The value of cosine of pi/6 is : ", end="")
print (math.cos(a))
 
# returning the value of tangent of pi/6
print ("The value of tangent of pi/6 is : ", end="")
print (math.tan(a))


Python3
# Python code to demonstrate the working of
# degrees() and radians()
 
# importing "math" for mathematical operations
import math
 
a = math.pi/6
b = 30
 
# returning the converted value from radians to degrees
print ("The converted value from radians to degrees is : ", end="")
print (math.degrees(a))
 
# returning the converted value from degrees to radians
print ("The converted value from degrees to radians is : ", end="")
print (math.radians(b))


Python3
# Python code to demonstrate
# working of gamma()
import math
 
# initializing argument
gamma_var = 6
 
# Printing the gamma value.
print ("The gamma value of the given argument is : "
                    + str(math.gamma(gamma_var)))


Python3
# Python3 code to demonstrate
# the working of isnan()
import math
 
# checking isnan() values
# with inbuilt numbers
print (math.isinf(math.pi))
print (math.isinf(math.e))
 
 
# checking for NaN value
print (math.isinf(float('inf')))


Python3
# Python3 code to demonstrate
# the working of isnan()
import math
 
# checking isnan() values
# with inbuilt numbers
print (math.isnan(math.pi))
print (math.isnan(math.e))
 
 
# checking for NaN value
print (math.isnan(float('nan')))


输出:

2.718281828459045

圆周率

你们都必须熟悉pi。圆周率表示为 22/7 或 3.14。 math.pi为 pi 提供了更精确的值。

句法:

示例 1:



蟒蛇3

# Import math Library
import math
 
# Print the value of pi
print (math.pi)

输出:

3.141592653589793

例二:求圆的面积

蟒蛇3

# Import math Library
import math
 
# radius of the circle
r = 4
 
# value of pie
pie = math.pi
 
# area of the circle
print(pie * r * r)

输出:

50.26548245743669

Tau定义为圆周与圆半径的比值。 math.tau常量返回值 tau:6.283185307179586。

句法:

例子:

蟒蛇3

# Import math Library
import math
 
# Print the value of tau
print (math.tau)

输出:

6.283185307179586

无限

无限基本上意味着从两个方向(即消极和积极)永无止境或无限的事物。它不能用数字来描述。正无穷大的math.inf常数回报。对于负无穷大,使用-math.inf

句法:



示例 1:

蟒蛇3

# Import math Library
import math
 
# Print the positive infinity
print (math.inf)
 
# Print the negative infinity
print (-math.inf)

输出:

inf
-inf

示例 2:将无穷大的值与最大浮点值进行比较

蟒蛇3

# Import math Library
import math
 
print (math.inf > 10e108)
print (-math.inf < -10e108)

输出:

True
True

NaN

math.nan常量返回一个浮点 nan(非数字)值。该值不是合法数字。 nan 常量等价于 float(“nan”)。

例子:

蟒蛇3

# Import math Library
import math
 
# Print the value of nan
print (math.nan)

输出:

nan

数值函数

在本节中,我们将处理与数论和表示论一起使用的函数,例如求一个数的阶乘。

找到天花板和地板值

Ceil 值表示大于数字的最小整数值,下限值表示小于数字的最大整数值。这可以分别使用ceil()floor()方法轻松计算。



例子:

蟒蛇3

# Python code to demonstrate the working of
# ceil() and floor()
 
# importing "math" for mathematical operations
import math
 
a = 2.3
 
# returning the ceil of 2.3
print ("The ceil of 2.3 is : ", end="")
print (math.ceil(a))
 
# returning the floor of 2.3
print ("The floor of 2.3 is : ", end="")
print (math.floor(a))

输出:

The ceil of 2.3 is : 3
The floor of 2.3 is : 2

找到数字的阶乘

使用阶乘() 函数我们可以在一行代码中找到一个数字的阶乘。如果数字不是整数,则会显示错误消息。

例子:

蟒蛇3

# Python code to demonstrate the working of
# factorial()
 
# importing "math" for mathematical operations
import math
 
a = 5
 
# returning the factorial of 5
print("The factorial of 5 is : ", end="")
print(math.factorial(a))

输出:

The factorial of 5 is : 120

寻找 GCD

gcd() 函数用于查找作为参数传递的两个数字的最大公约数。

例子:

蟒蛇3

# Python code to demonstrate the working of
# gcd()
 
# importing "math" for mathematical operations
import math
 
a = 15
b = 5
 
# returning the gcd of 15 and 5
print ("The gcd of 5 and 15 is : ", end="")
print (math.gcd(b, a))

输出:

The gcd of 5 and 15 is : 5

寻找绝对值

fabs()函数返回数字的绝对值。

例子:



蟒蛇3

# Python code to demonstrate the working of
# fabs()
 
# importing "math" for mathematical operations
import math
 
a = -10
 
# returning the absolute value.
print ("The absolute value of -10 is : ", end="")
print (math.fabs(a))

输出:

The absolute value of -10 is : 10.0

请参阅以下文章以获取有关数字函数的详细信息。

  • Python的数学函数 |设置 1(数值函数)

对数和幂函数

幂函数可以表示为 x^n,其中 n 是 x 的幂,而对数函数被视为指数函数的倒数。

寻找 exp 的力量

exp() 方法用于计算 e 的幂,即e^y        或者我们可以说 y 的指数。

例子:

蟒蛇3

# Python3 code to demonstrate
# the working of exp()
import math
 
# initializing the value
test_int = 4
test_neg_int = -3
test_float = 0.00
 
# checking exp() values
# with different numbers
print (math.exp(test_int))
print (math.exp(test_neg_int))
print (math.exp(test_float))

输出:

54.598150033144236
0.049787068367863944
1.0

求一个数的幂

pow()函数计算 x**y。此函数首先将其参数转换为浮点数,然后计算幂。

例子:

蟒蛇3

# Python code to demonstrate pow()
# version 1
 
print ("The value of 3**4 is : ",end="")
 
# Returns 81
print (pow(3,4))

输出:

The value of 3**4 is : 81.0

求对数

  • log()函数返回以 b 为底的 a 的对数值。如果未提及基数,则计算值是自然对数。
  • log2(a)函数以 2 为底计算 log a 的值。该值比上面讨论的函数值更准确。
  • log10(a)函数以 10 为底计算 log a 的值。该值比上述函数的值更准确。

蟒蛇3

# Python code to demonstrate the working of
# logarithm
 
# importing "math" for mathematical operations
import math
 
 
# returning the log of 2,3
print ("The value of log 2 with base 3 is : ", end="")
print (math.log(2,3))
 
# returning the log2 of 16
print ("The value of log2 of 16 is : ", end="")
print (math.log2(16))
    
# returning the log10 of 10000
print ("The value of log10 of 10000 is : ", end="")
print (math.log10(10000))

输出:



The value of log 2 with base 3 is : 0.6309297535714574
The value of log2 of 16 is : 4.0
The value of log10 of 10000 is : 4.0

求平方根

sqrt()函数返回数字的平方根。

例子:

蟒蛇3

# Python3 program to demonstrate the
# sqrt() method
 
# import the math module
import math
 
# print the square root of 0
print(math.sqrt(0))
 
# print the square root of 4
print(math.sqrt(4))
 
# print the square root of 3.5
print(math.sqrt(3.5))

输出:

0.0
2.0
1.8708286933869707

请参阅以下文章以获取有关对数函数和幂函数的详细信息

  • Python的数学函数 |组 2(对数和幂函数)

三角函数和角函数

你们都必须了解三角函数以及如何很难找到任何角度的正弦值和余弦值。 Math 模块提供了内置函数来查找这些值,甚至可以在度和弧度之间更改值。

查找正弦、余弦和正切

sin()、cos() 和 tan()函数返回作为参数传递的值的正弦、余弦和正切。此函数传递的值应以弧度为单位

例子:

蟒蛇3

# Python code to demonstrate the working of
# sin(), cos(), and tan()
 
# importing "math" for mathematical operations
import math
 
a = math.pi/6
 
# returning the value of sine of pi/6
print ("The value of sine of pi/6 is : ", end="")
print (math.sin(a))
 
# returning the value of cosine of pi/6
print ("The value of cosine of pi/6 is : ", end="")
print (math.cos(a))
 
# returning the value of tangent of pi/6
print ("The value of tangent of pi/6 is : ", end="")
print (math.tan(a))

输出:

The value of sine of pi/6 is : 0.49999999999999994
The value of cosine of pi/6 is : 0.8660254037844387
The value of tangent of pi/6 is : 0.5773502691896257

将值从度数转换为弧度,反之亦然

  • degree()函数用于将参数值从弧度转换为度数。
  • radians()函数用于将参数值从度数转换为弧度。

例子:

蟒蛇3

# Python code to demonstrate the working of
# degrees() and radians()
 
# importing "math" for mathematical operations
import math
 
a = math.pi/6
b = 30
 
# returning the converted value from radians to degrees
print ("The converted value from radians to degrees is : ", end="")
print (math.degrees(a))
 
# returning the converted value from degrees to radians
print ("The converted value from degrees to radians is : ", end="")
print (math.radians(b))

输出:



The converted value from radians to degrees is : 29.999999999999996
The converted value from degrees to radians is : 0.5235987755982988

请参阅以下文章以获取有关三角函数和角函数的详细信息。

  • Python的数学函数 |第 3 组(三角函数和角函数)

特殊功能

除了我们已经讨论过的所有数字、对数函数之外,math 模块还提供了一些更有用的函数,这些函数不属于上面讨论的任何类别,但在编码时可能会在某些时候变得方便。

寻找伽马值

gamma()函数用于返回参数的 gamma 值。

例子:

蟒蛇3

# Python code to demonstrate
# working of gamma()
import math
 
# initializing argument
gamma_var = 6
 
# Printing the gamma value.
print ("The gamma value of the given argument is : "
                    + str(math.gamma(gamma_var)))

输出:

The gamma value of the given argument is : 120.0

检查值是无穷大还是 NaN

isinf()函数用于检查值是否为无穷大。

例子:

蟒蛇3

# Python3 code to demonstrate
# the working of isnan()
import math
 
# checking isnan() values
# with inbuilt numbers
print (math.isinf(math.pi))
print (math.isinf(math.e))
 
 
# checking for NaN value
print (math.isinf(float('inf')))

输出:

False
False
True

如果数字是“NaN”, isnan()函数返回真,否则返回假。

例子:

蟒蛇3

# Python3 code to demonstrate
# the working of isnan()
import math
 
# checking isnan() values
# with inbuilt numbers
print (math.isnan(math.pi))
print (math.isnan(math.e))
 
 
# checking for NaN value
print (math.isnan(float('nan')))

输出:

False
False
True

请参阅以下文章以获取有关特殊功能的详细信息。

  • Python的数学函数 |设置 4(特殊函数和常量)

Python的数学函数列表

Function NameDescription
ceil(x)Returns the smallest integral value greater than the number
copysign(x, y)Returns the number with the value of ‘x’ but with the sign of ‘y’
fabs(x)Returns the absolute value of the number
factorial(x)Returns the factorial of the number
floor(x)Returns the greatest integral value smaller than the number
gcd(x, y)Compute the greatest common divisor of 2 numbers
fmod(x, y)Returns the remainder when x is divided by y
frexp(x)Returns the mantissa and exponent of x as the pair (m, e)
fsum(iterable)Returns the precise floating-point value of sum of elements in an iterable 
isfinite(x)Check whether the value is neither infinity not Nan
isinf(x)Check whether the value is infinity or not
isnan(x)Returns true if the number is “nan” else returns false
ldexp(x, i)Returns x * (2**i)
modf(x)Returns the fractional and integer parts of x
trunc(x)Returns the truncated integer value of x
exp(x)Returns the value of e raised to the power x(e**x)
expm1(x)Returns the value of e raised to the power a (x-1)
log(x[, b])Returns the logarithmic value of a with base b
log1p(x)Returns the natural logarithmic value of 1+x
log2(x)Computes value of log a with base 2
log10(x)Computes value of log a with base 10
pow(x, y)Compute value of x raised to the power y (x**y)
sqrt(x)Returns the square root of the number
acos(x)Returns the arc cosine of value passed as argument
asin(x)Returns the arc sine of value passed as argument
atan(x)Returns the arc tangent of value passed as argument
atan2(y, x)Returns atan(y / x)
cos(x)Returns the cosine of value passed as argument
hypot(x, y)Returns the hypotenuse of the values passed in arguments
sin(x)Returns the sine of value passed as argument
tan(x)Returns the tangent of the value passed as argument
degrees(x)Convert argument value from radians to degrees
radians(x)Convert argument value from degrees to radians
acosh(x)Returns the inverse hyperbolic cosine of value passed as argument
asinh(x)Returns the inverse hyperbolic sine of value passed as argument
atanh(x)Returns the inverse hyperbolic tangent of value passed as argument
cosh(x)Returns the hyperbolic cosine of value passed as argument
sinh(x)Returns the hyperbolic sine of value passed as argument
tanh(x)Returns the hyperbolic tangent of value passed as argument
erf(x)Returns the error function at x
erfc(x)Returns the complementary error function at x
gamma(x)Return the gamma function of the argument
lgamma(x)Return the natural log of the absolute value of the gamma function