小白教程
所有教程
关于
Search
172.69.58.241
172.69.58.241
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Ref math isclose”的源代码
本页内容
上一节:
Ref_math_hypot
下一节:
Ref_math_isfinite
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:math.isclose(a,b)}}[[Category:python math|33]] = Python math.isclose() 方法 = [[python math|Python math 模块]] Python '''math.isclose()''' 方法返回用于检查两个值是否彼此接近,如果值接近,则返回 True,否则返回 False。 '''math.isclose()''' 根据给定的绝对和相对容差确定两个值是否被认为是接近的。 Python 版本:3.5 计算公式为: <sample title="" desc="" lang="python" hererun="1"> abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) </sample> === 语法 === math.isclose() 方法语法如下: <sample title="" desc="" lang="python" hererun="1"> math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) </sample> '''参数说明:''' * '''a''' -- 必需,数字。如果 x 不是一个数字,返回 TypeError。如果值为 0 或负数,则返回 ValueError。 * '''b''' -- 必需,数字。如果 x 不是一个数字,返回 TypeError。如果值为 0 或负数,则返回 ValueError。 * '''rel_tol''' -- 是相对容差,它是 a 和 b 之间允许的最大差值,相对于 a 或 b 的较大绝对值。例如,要设置5%的容差,请传递 rel_tol=0.05 。默认容差为 1e-09,确保两个值在大约9位十进制数字内相同。 rel_tol 必须大于零。 * '''abs_tol''' -- 是最小绝对容差,对于接近零的比较很有用。 abs_tol 必须至少为零。 === 返回值 === 返回一个布尔值,检查两个值是否彼此接近,如果值接近,则返回 True,否则返回 False。 == 以下实例检查两个值是否彼此接近: <sample title="" desc="" lang="python" hererun="1"> # 导入 math 包 import math # 输出两个值是否接近 print(math.isclose(8.005, 8.450, abs_tol = 0.4)) print(math.isclose(8.005, 8.450, abs_tol = 0.5)) </sample> 输出结果: <sample title="" desc="" lang="python" hererun="1"> False True </sample> 以下实例检查两个浮点数是否接近: <sample title="" desc="" lang="python" hererun="1"> # 导入 math 包 import math # 可用于浮点数判断 # 这样会输出 false,0.1+0.2 不会等于 0.3 print(0.1+0.2 == 0.3) print(0.1+0.2 ) # 这样会输出 true print(math.isclose(0.1+0.2, 0.3)) </sample> 输出结果: <sample title="" desc="" lang="python" hererun="1"> False 0.30000000000000004 True </sample> [[python math|Python math 模块]]
返回至“
Ref math isclose
”。
上一节:
Ref_math_hypot
下一节:
Ref_math_isfinite