Logo
Section 1 / 6

内置函数:数学运算

学习如何使用内置函数执行不同的数学运算。

导入

内置函数:数学运算

学习如何使用内置函数执行不同的数学运算。

编程语言中的内置函数非常有用,能够在日常任务中带来巨大便利,因为我们无需自行编写这些功能。Python 内置了大量始终可用的函数。同样,PowerShell 也能访问 .Net 类,从而提供一些有用的函数。在本节课中,我们将介绍 PowerShell 和 Python 提供的各种内置函数。

基本数学运算

在日常工作中,我们经常需要执行基本的数学运算。下面逐一进行讲解。

常量:π 和 e

在 PowerShell 中,可以通过 System.Math 类访问数学常数 π(π = 3.141592)和欧拉数 e(e = 2.718281)。该类包含静态属性,用于存储这些常量的值,如下例所示:

1[math]::PI 2[math]::E

Python 则通过 math 模块来获取这些常量的值。

Python:

1import math 2print(math.pi) 3print(math.e)

计数与求和:len() 和 sum()

PowerShell 默认为数组等容器添加 Length 属性,同时还有一个别名 Count,可用于获取数组中元素的数量。

1$array = 1,2,3,4,5 2$array.Length 3 4# 或者使用别名 5$array.Count

而 Python 使用内置函数 len() 来返回容器中元素的数量。

1print(len([1,2,3]))

在 PowerShell 中,通过将数值管道传递给 Measure-Object 命令并使用 -Sum 参数,可以轻松实现数值相加。

1$array = 1,2,3,4,5 2($array | Measure-Object -Sum).Sum

Python 使用 sum() 函数对列表中的元素求和并返回结果。

1array = [1,2,3,4,5] 2print(sum(array))

绝对值与四舍五入:abs() 和 round()

Python 提供内置函数 abs(),可返回数字(整数或浮点数)的绝对值。

1print(abs(-1.45)) 2print(abs(-25))

在 PowerShell 中,可以使用 System.Math 类的 [math]::Abs() 方法获取传入参数的绝对值。

1[math]::Abs(-1.45) 2[math]::Abs(-25)

System.Math 类还提供了 Round() 方法,用于将浮点数 x 四舍五入到指定的小数位数 n。若省略 n,则默认为 0。

语法:

1[math]::Round(x,n)

示例:

1[math]::Round(1.345) 2[math]::Round(1.345, 2) 3[math]::Round(1.348, 2) 4[math]::Round(1.3489, 3)

Python 提供内置函数 round() 来返回四舍五入后的浮点数值。

1print(round(1.345)) 2print(round(1.345, 2)) 3print(round(1.348, 2)) 4print(round(1.3489, 3))

最大值与最小值:max() 和 min()

在 PowerShell 中,通过使用 Measure-Object 命令并配合 -Max 和 -Min 参数,可以在数组中查找最大值和最小值。

1$array = 1,2,3,4,5 2$Array | Measure-Object -Maximum -Minimum 3 4# 查找最大值 5($Array | Measure-Object -Maximum).Maximum 6 7# 查找最小值 8($Array | Measure-Object -Minimum).Minimum

Python 提供内置函数 max() 和 min(),用于查找列表中的最大值或最小值。

1array = [1,2,3,4,5] 2 3# 查找最大值 4print(max(array)) 5 6# 查找最小值 7print(min(array))

幂运算与平方根:pow() 和 sqrt()

PowerShell 可通过 System.Math 类的 Pow(x, y) 方法计算 x 的 y 次幂,两个参数均为数值类型。

1[math]::pow(2,3) 2[math]::pow(2,4) 3[math]::pow(10,10)

Python 提供内置函数 pow(x, y) 来计算 x 的 y 次幂。

1print(pow(2,3)) 2print(pow(2,4)) 3print(pow(10,10))

在 PowerShell 中,可以使用 System.Math 类的 sqrt() 方法计算平方根。

1[math]::sqrt(0) 2[math]::sqrt(8) 3[math]::sqrt(4.5)

在 Python 中,需先导入 math 模块,再通过点号(.)调用 sqrt() 函数来计算平方根。

1import math 2 3print(math.sqrt(0)) 4print(math.sqrt(8)) 5print(math.sqrt(4.5))

三角函数:sin()、cos() 和 tan()

两种语言都内置了三角函数,用于计算角度的正弦、余弦和正切值。

PowerShell:

1[Math]::Sin(90) 2[Math]::Cos(90) 3[Math]::Tan(30)

Python:

1import math 2 3print(math.sin(90)) 4print(math.cos(90)) 5print(math.tan(30))

对数:log() 和 log10()

对数是指数运算的逆运算。例如,当 10 的四次方等于 10000 时,10000 以 10 为底的对数就是 4。在 PowerShell 中,可以使用 System.Math 类的 Log10() 方法返回任意数以 10 为底的对数。Python 的 math 模块也提供 log10() 函数。两种语言都提供 log() 函数,用于计算以指定底数为基准的对数;若未指定底数,则返回自然对数(以 e 为底)。

PowerShell:

1# 以 10 为底的对数 2[Math]::Log10(10000) 3 4# 以 2 为底的对数 5[Math]::Log(10000, 2) 6 7# 默认底数为 e 8[Math]::Log(10000) 9 10[Math]::Log(10000, [Math]::E)

Python:

1import math 2 3# 以 10 为底的对数 4print(math.log10(10000)) 5 6# 以 2 为底的对数 7print(math.log(10000, 2)) 8 9# 默认底数为 e 10print(math.log(10000)) 11 12print(math.log(10000, math.e))

向下取整与向上取整:floor() 和 ceil()

在 Python 和 PowerShell 中,floor(x) 函数返回 x 的向下取整值(即小于或等于 x 的最大整数)。Python 还提供 ceil(x) 函数,返回 x 的向上取整值(即大于或等于 x 的最小整数)。在 PowerShell 中,该函数名为 Ceiling(x),但功能相同。

PowerShell:

1[Math]::floor(3.14) 2[Math]::floor(3.99) 3[Math]::ceiling(3.14) 4[Math]::ceiling(3.99)

Python:

1import math 2 3print(math.floor(3.14)) 4print(math.floor(3.99)) 5print(math.ceil(3.14)) 6print(math.ceil(3.99))

错误处理与异常

内置函数:选择与排列