math标准库函数功能汇总

本文介绍了Python的math标准库,包括四种数学常数如e和pi,十六个数值表示函数如ceil和fabs,八个幂对数函数如pow和exp,十六个三角双曲函数如sin和cos,以及四个高等特殊函数如erf和erfc。通过导入math库,可以方便地使用这些数学功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、引用math标准库

  1. 第一种方式:import math
    对math标准库中函数采用math.()形式使用
import math
math.ceil(0,2)
11
  1. 第二种方式:from math import <函数名>
    对math标准库中函数可以直接采用<函数名>()形式使用
from math import floor
floor(10.2)
10

2、math 标准库解析

以下展示全部采用第一种引用(import math)

2.1 math标准库包括4个数学常数

常数数学表示描述
math.piπ圆周率,值为3.141592653589793
math.ee自然对数,值为2.718281828459045
math.inf正无穷大,负无穷大为-math.inf
math.nan非浮点数标记,NAN(Not a Number)

例如
e

#表示一个常量
>>> math.e
2.718281828459045

pi

#数字常量,圆周率
>>> print(math.pi)
3.141592653589793

2.2math标准库包括16个数值表示函数

在这里插入图片描述

例如
ceil(x, /)
Return the ceiling of x as an Integral.
将X的上限作为积分返回。
This is the smallest integer >= x.
这是最小的整数>=x。

#取大于等于x的最小的整数值,如果x是一个整数,则返回x
>>> math.ceil(1.01)
2
>>> math.ceil(1.99)
2
>>> math.ceil(-2.99)
-2
>>> math.ceil(-2.01)
-2

fabs(x, /)
Return the absolute value of the float x.
返回浮点X的绝对值。

>>> math.fabs(-0.001)
0.001
>>> math.fabs(-100)
100.0

fmod(x, y, /)
Return fmod(x, y), according to platform C.
返回FMOD(X,Y),根据平台C。

#得到x/y的余数,其值是一个浮点数
>>> math.fmod(25,4)
1.0
>>> math.fmod(24,7)
3.0

isinf(x, /)
Return True if x is a positive or negative infinity, and False otherwise.
如果x是正的或负的无穷大,则返回真,否则为假。

#如果x是正无穷大或负无穷大,则返回True,否则返回False
>>> math.isinf(10000)
False
>>> math.isinf(0.00001)
False

2.3math标准库包括8个幂对数函数

在这里插入图片描述
例如
pow(x, y, /)
Return xy (x to the power of y).
返回x
y(x到y的幂)。

>>> math.pow(2,5)
32.0

exp(x, /)
Return e raised to the power of x.
返回E提高到X的功率。

>>> math.exp(2)
7.38905609893065

sqrt(x, /)
Return the square root of x.
返回X的平方根。

>>> math.sqrt(25)
5.0
>>> math.sqrt(400)
20.0

log10(x, /)
Return the base 10 logarithm of x.
返回x的基10对数。

>>>math.log10(1000)
3.0
>>> math.log10(100)
2.0

log1p(x, /)
Return the natural logarithm of 1+x (base e).
返回1±x(基E)的自然对数。
The result is computed in a way which is accurate for x near zero.
结果以精确接近x的方式计算。

>>> math.log1p(10)
2.3978952727983707

log2(x, /)
Return the base 2 logarithm of x.
返回x的基2对数。

>>> math.log2(32)
5.0
>>> math.log2(20)
4.321928094887363

2.4math标准库包括16个三角双曲函数

在这里插入图片描述
degrees(x, /)
Convert angle x from radians to degrees.
将角度X从弧度转换成度。

>>> math.degrees(math.pi/4)
45.0

radians(x, /)
Convert angle x from degrees to radians.
将角度x从度转换为弧度。

>>>math.radians(30)
π/6

hypot(x, y)
#得到(x2+y2),平方的值
hypot(x, y)
Return the Euclidean distance, sqrt(xx + yy).

>>>math.hypot(3,4)
5.0

求x的正弦,x必须是弧度
sin(x, /)
Return the sine of x (measured in radians).
返回X的正弦(以弧度测量)。

math.pi/6表示弧度,转换成角度为30>>> math.sin(math.pi/6)
0.5000000000000001

cos(x, /)
Return the cosine of x (measured in radians).
返回X的余弦(用弧度测量)。

#math.pi/4表示弧度,转换成角度为45>>> math.cos(math.pi/4)
0.7071067811865476

tan(x, /)
Return the tangent of x (measured in radians).
返回X的切线(以弧度测量)。

>>> math.tan(math.pi/4)
0.9999999999999999
>>> math.tan(math.pi/6)
0.5773502691896257

2.5math标准库包括4个高等特殊函数

在这里插入图片描述
例如
erf(x, /)
Error function at x.
x的误差函数。

erfc(x, /)
Complementary error function at x.
x的互补误差函数

以上只列举了几个常用函数的例子,其他请参考链接:math- 数学函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值