天天看点

python之数学函数应用

一、abs(x)

1.作用:

函数返回 x(数字)的绝对值,如果参数是一个复数,则返回它的大小(模)

2.举例说明:

#1.abs()
a = abs(-15)
print(a)
b = abs(1+2j)
print(b)      

查看运行结果:

python之数学函数应用

二、ceil(x)

1.作用:

返回一个大于或等于 x 的的最小整数。如math.ceil(4.1) 返回 5

2.举例说明:

#2.ceil()
a = math.ceil(-0.8)
b = math.ceil(100.1)
c = math.ceil(math.pi)
print(a,b,c)      

查看运行结果:

python之数学函数应用

三、exp(x)

1.作用:

返回e的x次幂

2.举例说明:

#3.exp()
a = math.exp(1)
print(a)      

查看运行结果:

python之数学函数应用

四、fabs(x) 

 1.作用:

返回数字的绝对值,如math.fabs(-10) 返回10.0,fabs() 函数只对浮点型跟整型数值有效,返回值也是浮点数

2.举例说明:

#4.fabs()
a = math.fabs(1)
b = math.fabs(3.14)
print(a,b)
print(type(a),type(b))      

查看运行结果:

python之数学函数应用

五、floor(x)

1.作用:

返回数字的下舍整数,如math.floor(4.9)返回 4

2.举例说明:

#5.floor()
a = math.floor(-1.2)
b = math.floor(3.14)
print(a,b)      

查看运行结果:

python之数学函数应用

六、log(x),log(x,y)

1.作用:

log() 方法返回x的自然对数,(以y为基数,默认不输入时以math.e为基数) x > 0,,math.log(100,10)返回2.0

2.举例说明:

a = math.log(100)
b= math.log(100,10)
print(a,b)      

查看运行结果:

python之数学函数应用

七、log10(x)

1.作用:

返回以10为基数的x的对数,如math.log10(100)返回 2.0

2.举例说明:

#7.log10()
a = math.log10(100)
b = math.log10(1000)
print(a,b)      

查看运行结果:

python之数学函数应用

八、max(x,y,..)与min((x,y,..)

1.作用:

返回给定参数的最大值与最小值

#max() min()
a = max(-100,2,3)
b = min(-50,0,3)
print(a,b)      

查看运行结果:

python之数学函数应用

 九、modf(x)

1.作用:

返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示

2.举例说明:

#9.modf()
a = math.modf(100)
b = math.modf(-10.34)
c = math.modf(math.pi)
print(a,b,c)      

查看运行结果:

python之数学函数应用

十、pow(x, y)

1.作用

返回x**y 运算后的值

2.举例说明:

#10.pow(x,y)
a = pow(2,0)
b = pow(5,5)
print(a,b)      

查看运行结果:

python之数学函数应用

 十一、round(x [,n])

1.作用

返回浮点数 x 的四舍五入值,如给出 n 值,则代表舍入到小数点后的位数。

2.举例说明:

#11.round()
a = round(3.14)
b = round(3.14158926,3)
c = round(-3.14,2)
d1 = round(2.675, 2)  #此时精度出现问题,可使用 Decimal(x).quantize(Decimal(小数位数)来替代
d2 = Decimal("2.675").quantize(Decimal("0.00"))
print(a,b,c,d1,d2)      

查看运行结果:

python之数学函数应用

十二、sqrt(x)

1.作用:

返回数字x的平方根。

2.举例说明:

#12.sqrt()
a = math.sqrt(100)
b = math.sqrt      

查看运行结果:

python之数学函数应用