6.4 数学函数
math库包含了很多数学函数,以下是math库常用函数:
函数名 | 说明 | 示例 |
---|---|---|
math.ceil(x) | 返回大于或等于x的最小整数 | math.ceil(4.7) 返回 5 |
math.floor(x) | 返回小于或等于x的最大整数 | math.floor(4.7) 返回 4 |
math.trunc(x) | 返回x的整数部分(向0取整) | math.trunc(4.7) 返回 4 |
math.sqrt(x) | 返回x的平方根 | math.sqrt(16) 返回 4.0 |
math.exp(x) | 返回e的x次幂 | math.exp(2) 返回 7.3890560989306495 |
math.log(x[, base]) | 返回以base为底x的对数,base缺省时,默认为e(自然对数) | math.log(10) 返回 2.302585092994046 |
math.log10(x) | 返回以10为底x的对数 | math.log10(100) 返回 2.0 |
math.pow(x, y) | 返回x的y次幂 | math.pow(4, 3) 返回 64.0 |
math.radians(x) | 将角度x转换为弧度 | math.radians(180) 返回 3.141592653589793 |
math.degrees(x) | 将弧度x转换为角度 | math.degrees(3.141592653589793) 返回 180.0 |
math.sin(x) | 返回x(弧度)的正弦值 | math.sin(0) 返回 0.0 |
math.cos(x) | 返回x(弧度)的余弦值 | math.cos(0) 返回 1.0 |
math.tan(x) | 返回x(弧度)的正切值 | math.tan(0) 返回 0.0 |
math.asin(x) | 返回x的反正弦值,返回值是弧度 | math.asin(0) 返回 0.0 |
math.acos(x) | 返回x的反余弦值,返回值是弧度 | math.acos(1) 返回 0.0 |
math.atan(x) | 返回x的反正切值,返回值是弧度 | math.atan(0) 返回 0.0 |
math.atan2(y, x) | 返回给定的y和x坐标值的反正切值,返回值是弧度 | math.atan2(1, 1) 返回 0.785... |
math.hypot(x, y) | 返回欧几里德范数sqrt(x*x + y*y) | math.hypot(3, 4) 返回 5.0 |
math.acosh(x) | 返回x的反双曲余弦值 | math.acosh(1) 返回 0.0 |
math.asinh(x) | 返回x的反双曲正弦值 | math.asinh(0) 返回 0.0 |
math.atanh(x) | 返回x的反双曲正切值 | math.atanh(0) 返回 0.0 |
算术运算符也有部分能取代的,例如a的b次方,即可以用a**b,也可以用math.pow(a,b)
import matha=3b=4print(math.pow(a,b)) #计算并打印3的4次方,结果是81