Python数学函数

数学模块用于访问Python的数学函数。此函数的所有方法都用于整数或实型对象,而不用于复数。

要使用此模块,我们应该将该模块导入到我们的代码中。

import math

一些常数

这些常数用于将它们放入我们的计算中。

序号常数与说明
1

pi

返回pi的值:3.141592

2

Ë

返回自然基数e的值。e为0.718282

3

返回tau的值。头= 6.283185

4

信息

返回无限

5

不是数字类型。

数字和数字表示

这些函数用于表示不同形式的数字。方法如下-

序号功能说明
1

天花板(x)

返回天花板值。它是最小的整数,大于或等于数字x。

2

copysign(x,y)

它返回数字x,并将y的符号复制到x。

3

晶圆厂(x)

返回x的绝对值。

4

阶乘(x)

返回x的阶乘。其中x≥0

5

地板(x)

返回下限值。它是最大的整数,小于或等于数字x。

6

fsum(可迭代)

查找可迭代对象中元素的总和

7

gcd(x,y)

返回x和y的最大公约数

8

等式(x)

检查x既不是无穷大也不是nan。

9

isinf(x)

检查x是否为无穷大

10

isnan(x)

检查x是否不是数字。

11

余数(x,y)

将x除以y后找到余数。

范例程式码

import math
print('The Floor and Ceiling value of 23.56 are: ' + str(math.ceil(23.56)) + ', ' + str(math.floor(23.56)))
x = 10
y = -15
print('The value of x after copying the sign from y is: ' + str(math.copysign(x, y)))
print('Absolute value of -96 and 56 are: ' + str(math.fabs(-96)) + ', ' + str(math.fabs(56)))
my_list = [12, 4.25, 89, 3.02, -65.23, -7.2, 6.3]
print('Sum of the elements of the list: ' + str(math.fsum(my_list)))
print('The GCD of 24 and 56 : ' + str(math.gcd(24, 56)))
x = float('nan')
if math.isnan(x):
    print('It is not a number')
x = float('inf')
y = 45
if math.isinf(x):
    print('It is Infinity')
print(math.isfinite(x)) #x is not a finite number
print(math.isfinite(y)) #y is a finite number

输出结果

The Floor and Ceiling value of 23.56 are: 24, 23
The value of x after copying the sign from y is: -10.0
Absolute value of -96 and 56 are: 96.0, 56.0
Sum of the elements of the list: 42.13999999999999
The GCD of 24 and 56 : 8
It is not a number
It is Infinity
False
True

幂和对数函数

这些函数用于计算不同的幂相关和对数相关任务。

序号功能说明
1

战俘(x,y)

将x返回到幂y值。

2

平方根(x)

查找x的平方根

3

exp(x)

查找xe,其中e = 2.718281

4

log(x [,base])

返回x的对数,其中给出底数。默认基数为e

5

log2(x)

返回x的对数,以2为底

6

log10(x)

返回x的对数,以10为底



范例程式码

import math
print('The value of 5^8: ' + str(math.pow(5, 8)))
print('Square root of 400: ' + str(math.sqrt(400)))
print('The value of 5^e: ' + str(math.exp(5)))
print('The value of Log(625), base 5: ' + str(math.log(625, 5)))
print('The value of Log(1024), base 2: ' + str(math.log2(1024)))
print('The value of Log(1024), base 10: ' + str(math.log10(1024)))

输出结果

The value of 5^8: 390625.0
Square root of 400: 20.0
The value of 5^e: 148.4131591025766
The value of Log(625), base 5: 4.0
The value of Log(1024), base 2: 10.0
The value of Log(1024), base 10: 3.010299956639812

三角和角转换函数

这些函数用于计算不同的三角运算。

序号功能说明
1

罪恶(x)

以弧度返回x的正弦值

2

cos(x)

以弧度返回x的余弦值

3

tan(x)

返回弧度x的切线

4

asin(x)

这是正弦的逆运算,有acos,也有atan。

5

度(x)

将角度x从弧度转换为度

6

弧度(x)

将角度x从度转换为弧度

范例程式码

import math
print('The value of Sin(60 degree): ' + str(math.sin(math.radians(60))))
print('The value of cos(pi): ' + str(math.cos(math.pi)))
print('The value of tan(90 degree): ' + str(math.tan(math.pi/2)))
print('The angle of sin(0.8660254037844386): ' + str(math.degrees(math.asin(0.8660254037844386))))

输出结果

The value of Sin(60 degree): 0.8660254037844386
The value of cos(pi): -1.0
The value of tan(90 degree): 1.633123935319537e+16
The angle of sin(0.8660254037844386): 59.99999999999999