{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# math模块及其应用"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <font size=\"4\"><font color=Red>__模块(module)__</font>是 Python 中非常重要的东西,可以把它理解为 Python 的扩展工具,可提供面向特定领域或方向的程序功能。Python 安装好之后,默认安装了一些模块,称为<font color=Red>__“标准库”__</font>,“标准库”中的模块<font color=Red>__不需要安装__</font>,就可以直接使用。 \n",
" <font size=\"4\">没有纳入标准库的模块,需要使用 <font color=Red>__“pip install模块名”__</font>安装之后才能使用。</font>\n",
" </font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"在数学运算之中,除了加、减、乘、除运算之外,还有其它更多的运算,比如乘方、开方、对数运算等等,要实现这些运算,可以使用Python 中的<font color=Red>__math模块__</font>。该模块是内置模块,不需要安装,可直接使用。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python中导入模块的方法有两种:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 导入模块名"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"此时,程序可以调用模块名中的<font color=Red>__所有函数__</font>,语法表示如下:"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"import 库名\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" 调用模块中函数时,需明确指出函数所在的模块的名称,格式如下: "
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"模块名.函数名(函数参数)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.141592653589793\n",
"2.23606797749979\n"
]
}
],
"source": [
"import math # 导入math模块,引用其中的函数时需要用在函数名前面加“math.”\n",
"\n",
"print(math.pi) # 输出math模块中的pi值3.141592653589793\n",
"print(math.sqrt(5)) # 调用math模块中的sqrt函数求解5的平方根并输出"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">若调用模块中函数时,<font color=Red>__未指出函数所在的模块__</font>的名称,将抛异常。</font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math \n",
"\n",
"print(pi) # 未指明模块名,抛未定义异常"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 直接导入库中的<font color=Red>__函数__</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">可以同时引用多个函数,各函数间用逗号分隔,也可以用通配符“*”代替,表示引入该为中的所有函数,语法表示如下:</font> "
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"from <库名 > import <函数名,函数名,...,函数名 >\n",
"from <库名 > import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"此时,调用该库的函数时不需要指明函数所在库的名称,格式如下: "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<函数名 >(<函数参数 >)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from math import pi,sqrt # 导入math中的常数pi和sqrt()函数,直接引用函数名\n",
"\n",
"\n",
"print(pi) # 输出math模块中的pi值3.141592653589793\n",
"print(sqrt(5)) # 调用math模块中的sqrt函数求解5的平方根并输出"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from math import * # 导入math中的所有函数,直接引用函数名\n",
"\n",
"\n",
"print(pi) # 输出math模块中的pi值3.141592653589793\n",
"print(sqrt(5)) # 调用math模块中的sqrt函数求解5的平方根并输出"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"一般程序较简单时,只引入一个库或所引用的函数仅在一个库中存在时,两种方法都可以使用。 \n",
"当编写的程序较复杂、<font color=Red>__引用多个库时__</font>,可能在多个库中存在同名函数,而这些同名函数功能可能不同。这时建议使用<font color=Red>__第一种方法__</font>,明确指出所引用的函数来自于哪个库,以免出现错误。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"math库中包括: \n",
"24个数论与表示函数 \n",
"10个幂和对数函数 \n",
"9个三角函数 \n",
"6个双曲函数 \n",
"2个角度转换函数 \n",
"4个特殊函数 \n",
"5个数学常量"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 24个数论与表示函数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 序号 | 函数或常量 | 描述与示例 |\n",
"| :---: | :--- | :---- |\n",
"| 1 | ceil(x) | 返回 x 的向上取整,即大于或等于 x 的最小的整数 |\n",
"| 2 | comb(n, k) | 返回不重复且无顺序地从 n 项中选择 k 项的方式总数 |\n",
"| 3 | copysign(x, y) | 返回一个基于 x 的绝对值和 y 的符号的浮点数。在支持带符号零的平台上,copysign(1.0, -0.0) 返回 -1.0. |\n",
"| 4 | fabs(x) | 返回 x 的绝对值 |\n",
"| 5 | factorial(x) | 以一个整数返回 x 的阶乘。 |\n",
"| 6 | floor(x) | 返回 x 的向下取整,小于或等于 x 的最大整数。 |\n",
"| 7 | fmod(x, y) | 返回 fmod(x, y) |\n",
"| 8 | frexp(x) | 以 (m, e) 对的形式返回 x 的尾数和指数 |\n",
"| 9 | fsum(iterable) | 返回迭代中的精确浮点值 |\n",
"| 10 | gcd(\\*integers) | 返回给定的整数参数的最大公约数 |\n",
"| 10 | isclose(a, b, \\*, rel_tol=1e-09, abs_tol=0.0) | 若 a 和 b 的值比较接近则返回 True,否则返回 False |\n",
"| 12 | isfinite(x) | 如果 x 既不是无穷大也不是NaN,则返回 True ,否则返回 False |\n",
"| 13 | isinf(x) | 如果 x 是正或负无穷大,则返回 True ,否则返回 False |\n",
"| 14 | isnan(x) | 如果 x 是 NaN(不是数字),则返回 True ,否则返回 False |\n",
"| 15 | isqrt(n) | 返回非负整数 n 的整数平方根。 这就是对 n 的实际平方根向下取整,或者相当于使得 a² ≤ n 的最大整数 a。 |\n",
"| 16 | lcm(\\*integers) | 返回给定的整数参数的最小公倍数 |\n",
"| 17 | ldexp(x, i) | 返回 x \\* (2\\*\\*i) 。 这基本上是函数 frexp() 的反函数。 |\n",
"| 18 | modf(x) | 返回 x 的小数和整数部分。两个结果都带有 x 的符号并且是浮点数 |\n",
"| 19 | nextafter(x, y) | 返回 x 趋向于 y 的最接近的浮点数值 |\n",
"| 20 | perm(n, k=None) | 返回不重复且有顺序地从 n 项中选择 k 项的方式总数 |\n",
"| 21 | prod(iterable, \\*, start=1) | 计算输入的 iterable 中所有元素的积。 积的默认 start 值为 1。 |\n",
"| 22 | remainder(x, y) | 返回 IEEE 754 风格的 x 相对于 y 的余数。对于有限 x 和有限非零 y ,这是差异 x - n\\*y ,其中 n 是与商 x / y 的精确值最接近的整数。如果 x / y 恰好位于两个连续整数之间,则将最接近的 偶数 用作 n 。 |\n",
"| 23 | trunc(x) | 返回去除小数部分的 x ,只留下整数部分。 |\n",
"| 24 | ulp(x) | 返回浮点数 x 的最小有效比特位的值 |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 10个幂和对数函数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 序号 | 函数或常量 | 描述与示例 |\n",
"| :---: | :--- | :---- |\n",
"| 1 | cbrt(x) | 返回 x的立方根(3.11新增) |\n",
"| 2 | exp(x) | 返回 e 次 x 幂,其中 e = 2.718281... 是自然对数的基数。这通常比 math.e ** x 或 pow(math.e, x) 更精确。 |\n",
"| 3 | exp2(x) | 返回 2 的x次幂(3.11新增) |\n",
"| 4 | expm1(x) | 返回 e 的 x 次幂,减1。这里 e 是自然对数的基数。 |\n",
"| 5 | log(x[, base]) | 使用一个参数,返回 x 的自然对数(底为 e ) |\n",
"| 6 | log1p(x) | 返回 1+x 的自然对数(以 e 为底)。 以对于接近零的 x 精确的方式计算结果。 |\n",
"| 7 | log2(x) | 返回 x 以2为底的对数。这通常比 log(x, 2) 更准确。 |\n",
"| 8 | log10(x) | 返回 x 底为10的对数。这通常比 log(x, 10) 更准确 |\n",
"| 9 | pow(x, y) | 将返回 x 的 y 次幂, pow(1.0, x) 和 pow(x, 0.0) 总是返回 1.0 |\n",
"| 10 | sqrt(x) | 返回 x 的平方根 |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 9个三角函数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 序号 | 函数或常量 | 描述与示例 |\n",
"| :---: | :--- | :---- |\n",
"| 1 | acos(x) | 返回以弧度为单位的 x 的反余弦值。 结果范围在 0 到 pi 之间 |\n",
"| 2 | asin(x) | 返回以弧度为单位的 x 的反正弦值。 结果范围在 -pi/2 到 pi/2 之间 |\n",
"| 3 | atan(x) | 返回以弧度为单位的 x 的反正切值。 结果范围在 -pi/2 到 pi/2 之间 |\n",
"| 4 | atan2(y, x) | 以弧度为单位返回 atan(y / x) 。结果是在 -pi 和 pi 之间 |\n",
"| 5 | cos(x) | 返回 x 弧度的余弦值 |\n",
"| 6 | dist(p, q) | 返回 p 与 q 两点之间的欧几里得距离,以一个坐标序列(或可迭代对象)的形式给出。 两个点必须具有相同的维度。 |\n",
"| 7 | hypot(\\*coordinates) | 返回欧几里得范数,sqrt(sum(x\\*\\*2 for x in coordinates))。 这是从原点到坐标给定点的向量长度 |\n",
"| 8 | sin(x) | 返回 x 弧度的正弦值 |\n",
"| 9 | tan(x) | 返回 x 弧度的正切值 |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6个双曲函数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 序号 | 函数或常量 | 描述与示例 |\n",
"| :---: | :--- | :---- |\n",
"| 1 | acosh(x) | 返回 x 的反双曲余弦值 |\n",
"| 2 | asinh(x) | 返回 x 的反双曲正弦值 |\n",
"| 3 | atanh(x) | 返回 x 的反双曲正切值 |\n",
"| 4 | cosh(x) | 返回 x 的双曲余弦值 |\n",
"| 5 | sinh(x) | 返回 x 的双曲正弦值 |\n",
"| 6 | tanh(x) | 返回 x 的双曲正切值 |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2个角度转换函数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 序号 | 函数或常量 | 描述与示例 |\n",
"| :---: | :--- | :---- |\n",
"| 1 | degrees(x) | 将角度 x 从弧度转换为度数 |\n",
"| 2 | radians(x) | 将角度 x 从度数转换为弧度 |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4个特殊函数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 序号 | 函数或常量 | 描述与示例 |\n",
"| :---: | :--- | :---- |\n",
"| 1 | erf(x) | 返回 x 处的 error function |\n",
"| 2 | erfc(x) | 返回 x 处的互补误差函数 |\n",
"| 3 | gamma(x) | 返回 x 处的 伽马函数 值 |\n",
"| 4 | lgamma(x) | 返回Gamma函数在 x 绝对值的自然对数 |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5个数学常量"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 序号 | 函数或常量 | 描述与示例 |\n",
"| :---: | :--- | :---- |\n",
"| 1 | pi | 数学常数 π = 3.141592653589793,精确到可用精度 |\n",
"| 2 | e | 数学常数 e = 2.718281828459045,精确到可用精度 |\n",
"| 3 | tau | 数学常数 τ = 6.283185307179586,精确到可用精度。Tau 是一个圆周常数,等于 2π,圆的周长与半径之比。 |\n",
"| 4 | inf | 浮点正无穷大 |\n",
"| 5 | nan | 一个浮点的 \"非数字\"(NaN)值。使用 isnan() 函数来测试 NaN,而不是 is 或 == |"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"\n",
"print(math.pi) # 3.141592653589793\n",
"print(math.e) # 2.718281828459045\n",
"print(math.tau) # 6.283185307179586"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"这些函数一般都是对C语言库中同名函数进行简单封装,仅支持整数和浮点数,<font color=Red>__不支持复数运算__</font>。 \n",
"如果需要复数支持,可以使用cmath模块。 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"下面对其中较常用的函数做简单说明,其他情况可参考官方文档https://docs.python.org/zh-cn/3/library/math.html 。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### 数学常量\n",
"###### math.pi:返回圆周率常数π值\n",
"###### math.e:返回自然常数e值"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"print(math.pi)\n",
"print(math.e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### 数论与表示函数\n",
"\n",
"###### math.fabs(x):以浮点数形式返回x的绝对值。\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"print(math.fabs(-6)) # 返回浮点型的绝对值,输出6.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.factorial(x):返回x的阶乘,要求x为非负整数,x为负数或浮点数时返回错误。"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"print(math.factorial(6)) # 返回6!,输出720"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"print(math.factorial(-6)) # 参数为负数,抛异常"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"print(math.factorial(6.5)) # 参数为浮点,抛异常"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.fsum(iterable)):返回浮点数迭代求和的精确值,避免多次求和导致精度损失。"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"print(sum([.1, .1,.1, .1, .1, .1, .1, .1, .1, .1])) \n",
"print(math.fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.gcd(\\*integers):返回给定的整数参数的最大公约数。\n",
"如果参数之一非零,则返回值将是能同时整除所有参数的最大正整数。<br>如果所有参数为零或无参数,则返回值为 0。<br>在 3.9 版后添加了对任意数量的参数的支持,3.8版本或之前只支持两个参数。"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"print(math.gcd(88, 44)) # 返回2个值的最大公约数,输出44\n",
"print(math.gcd(88, 44, 22)) # 返回3个值的最大公约数(3.9新增),输出22\n",
"print(math.gcd(88, 44, 0)) # 返回值是能同时整除所有参数的最大正整数,输出44\n",
"print(math.gcd(0, 0)) # 如果所有参数为零或无参数,则返回值为0\n",
"print(math.gcd()) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.lcm(\\*integers):返回给定的整数参数的最小公倍数。(3.9版新增函数)\n",
"如果所有参数均非零,则返回值将是为所有参数的整数倍的最小正整数。<br>如果参数之一为零,则返回值为 0。<br>不带参数的 lcm() 返回 1。"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"print(math.lcm(5, 44, 22)) # 返回3个值的最小公倍数,输出220\n",
"print(math.lcm(22, 44, 0)) # 如果参数之一为0,返回值为0\n",
"print(math.lcm(0, 0))\n",
"print(math.lcm()) # 无参数时,返回值为1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.prod(iterable, \\*, start=1):计算输入的可迭代对象 iterable 中所有元素的积。\n",
"积的默认起始值start为 1。<br>当可迭代对象为空时,返回起始值。<br>此函数特别针对数字值使用,并会拒绝非数字类型。"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"print(math.prod([1,2,3,4,5])) # 1*2*3*4*5,输出120\n",
"print(math.prod([1,2,3,4,5], start=2)) # 2*1*2*3*4*5,输出240\n",
"print(math.prod([]))\n",
"print(math.prod([], start=2)) #当可迭代对象为空时,返回起始值start(默认为1)。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.floor(x):返回不大于x的最大整数。"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"print(math.floor(9.99)) # 输出9\n",
"print(math.floor(-9.01)) # 输出-10\n",
"print(math.floor(9)) # 输出9"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.ceil(x):返回不小于x的最小整数。"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"print(math.ceil(9.01)) # 输出10\n",
"print(math.ceil(-9.99)) # 输出-9\n",
"print(math.ceil(9)) # 输出9"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.isqrt(n):返回非负整数 n 的整数平方根。\n",
"即对 n 的实际平方根向下取整,或者相当于使得 a² ≤ n 的最大整数 a。"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"print(math.isqrt(99)) # 输出 99的整数平方根 9"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"某些情况下,对于正数 n,需求使得 n ≤ a² 的最小整数 a ,或者换句话说就是 n 的实际平方根向上取整,可使用下面的方法实现。"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"print(1 + math.isqrt(99 - 1)) # 输出不小于99的平方根的最小整数10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### 幂和对数函数\n",
"\n",
"###### math.log(x[, base]):返回x以base为底的对数。\n",
"\n",
"使用一个参数,返回 x 的自然对数(底base为 e )。\n",
"\n",
"使用两个参数,返回给定的 base 为底的对数 x ,计算为 log(x)/log(base) 。"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"print(math.log(10)) # 返回10的自然对数\n",
"print(math.log(10, 2)) # 返回10以2为底对数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.log2(x):返回以2为底的x的对数。\n",
"其值通常比log(x, 2)值更精确"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"print(math.log2(50)) # 返回50以2为底对数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.log10(x):返回以10为底的x的对数。\n",
"其值通常比log(x, 10)值更精确"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"print(math.log10(50)) # 返回50以10为底对数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.exp(x):返回 $e^x$"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"print(math.exp(2)) # 输出 e 的平方,7.38905609893065"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.pow(x, y):返回x的y次幂\n",
"结果为浮点数,pow(1.0, x) 和 pow(x, 0.0) 总返回1.0。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n",
"1.0\n",
"1.0\n"
]
}
],
"source": [
"import math\n",
"print(pow(2, 3)) # 输出 2 的 3 次方 8.0\n",
"print(math.pow(1.0, 5)) # 输出 1.0\n",
"print(math.pow(5, 0)) # 输出 1.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.sqrt(x):返回x的平方根\n",
"结果为浮点数"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"print(math.sqrt(100)) # 输出 100的正数平方根 10.0\n",
"print(math.sqrt(2)) # 输出 2的正数平方根 1.4142135623730951"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### 三角函数\n",
"\n",
"###### math.sin(x):返回弧度x的正弦值。"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"print(math.sin(math.pi / 6)) \n",
"print(math.sin(math.pi / 3)) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.cos(x):返回弧度x的余弦值。"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"print(math.cos(math.pi / 6)) \n",
"print(math.cos(math.pi / 3)) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.tan(x):返回弧度x的正切值。"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"print(math.tan(math.pi / 4)) \n",
"print(math.tan(0)) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.asin(x):返回以弧度为单位的 x 的反正弦值。 \n",
"结果范围在 $-\\frac{\\pi}{2}$ 到 $\\frac{\\pi}{2}$ 之间。"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"print(math.asin(0.5)) \n",
"print(math.asin(1)) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.acos(x):返回以弧度为单位的 x 的反余弦值。 \n",
"结果范围在0到$\\pi$之间。"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"print(math.acos(0.5)) \n",
"print(math.acos(0)) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.atan(x):返回以弧度为单位的 x 的反正切值。 \n",
"结果范围在 $-\\frac{\\pi}{2}$ 到 $\\frac{\\pi}{2}$ 之间。"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"print(math.atan(1)) \n",
"print(math.atan(0)) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.hypot(*coordinates): 返回欧几里得范数。\n",
"即从原点到坐标给定点的向量长度"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"print(math.hypot(3, 4)) # 计算点(3, 4)到原点(0.0)的距离,输出 5.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### 角度转换函数\n",
"\n",
"###### math.degrees(x):弧度值转角度值。"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"print(math.degrees(math.pi / 4)) # 输出 45.0\n",
"print(math.degrees(math.pi)) # 输出 180.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###### math.radians(x):角度值转弧度值。"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"print(math.radians(45)) # 输出约为pi/4\n",
"print(math.radians(180)) # 输出约为pi"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 实例:梅钦法计算圆周率\n",
"\n",
"梅钦公式是格里高利/莱布尼茨计算的公式的变体,它的收敛速度很快,至今仍然是计算圆周率的主要公式,请根据此公式计算圆周率值。\n",
"\n",
"$$\\frac{\\pi}{4}=4\\arctan \\frac{1}{5}-\\arctan \\frac{1}{239} $$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 提示:math库中反正切函数为<font color=Red>__atan(x)__</font>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"\n",
"quarter_of_pi = 4 * math.atan(1 / 5) - math.atan(1 / 239)\n",
"pi = 4 * quarter_of_pi\n",
"print(pi) # 3.1415926535897936"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}