python中内置数学函数详解和实例应用之三角函数_初级阶段(二)

本文介绍了Python中用于三角函数运算的内置函数,包括sin、cos、tan及其反函数,还有atan2、hypot、degrees和radians等,详细解释了它们的功能和使用方法,并提供了相关示例。

学习目标:

利用python进行三角函数运算


学习内容:

python中的三角函数合集和应用实例

转载请注明出处!


学习产出:

环境:python 3.7, 利用shell进行实例操作。

Note: 使用三角函数前需要import math

1.1, math.sin() 输出的x弧度的正弦值,数值在 -1 到 1 之间。

>>> math.sin(0)
0.0
>>> math.sin(math.pi / 2)
1.0
>>> round(math.sin(math.pi / 4), 2)
0.71
>>> bool(round(math.sin(math.pi / 4), 2) == round((pow(2, 1 / 2)) / 2, 2))
True

1.2,math.cos()输出x的弧度的余弦值,数值在-1 到 1 之间。

>>> math.cos(0)
1.0
>>> math.cos(math.sin(math.pi))
1.0
>>>math.cos(math.pi / 3)
0.5000000000000001
>>> bool(math.cos(math.sin(math.pi)) == math.cos(math.sin(0)))
True
>>> math.sin(10) ** 2 + math.cos(10) ** 2
1.0  		# 即sin(x) * sin(x) + cos(x) * cos(x) = 1

1.3 math.tan()输出x弧度的正切值,数值在 -1 到 1 之间。
Note: python中没有math.cot()函数.

>>> math.tan(math.pi / 2)
1.633123935319537e+16
>>> math.tan(math.pi / 4)
0.9999999999999999
>>> math.sin(math.pi / 4) / math.cos(math.pi / 4)
1.0
>>> bool(math.tan(math.pi / 4) == math.sin(math.pi / 4) / math.cos(math.pi / 4))
False   # 注意到了吗?
>>> bool(round(math.tan(math.pi / 4), 10) == math.sin(math.pi / 4) / math.cos(math.pi / 4))	
True	# 和上一个不同

1.4, math.acos()输出以弧度为单位返回 x 的反余弦值, 数值在0到pi之间。

>>> math.acos(-1)
3.141592653589793
>>> math.acos(1)
0.0
>>> math.cos(0) * math.acos(0)
1.5707963267948966
>>> math.cos(math.acos(0.55))
0.55
>>> bool(math.acos(-1) == math.pi)
True

1.5,math.asin()输出以弧度为单位返回 x 的反正弦值, 数值在(-pi/2)到(pi/2)之间.

>>> math.asin(-1)
-1.5707963267948966
>>> math.asin(0)
0.0
>>> math.asin(1) + math.asin(-1)
0.0
>>> math.sin(math.asin(0.55))
0.55
>>> bool(math.asin(1) - math.asin(-1) == math.pi)
True
>>> bool(math.asin(0.55) + math.acos(0.55) == math.pi / 2)
True		#还记得这个公式吗?
>>> bool(2 * math.asin(1) + math.acos(-1) == 2 * math.pi)
True
>>> bool(2 * math.asin(-1) - math.acos(-1) == (-2 * math.pi))
True

1.6,math.atan()输出以弧度为单位返回 x 的反正切值, 数值在(-pi/2)到(pi/2)之间.
Note: python中没有math.acot()函数.

>>> math.atan(1)
0.7853981633974483
>>> math.atan(1) == math.pi / 4
True
>>> math.atan(-1) == - math.atan(1)
True
>>> math.tan(math.atan(0.55))
0.55
>>> math.atan(math.pi / 4) - math.atan(math.pi / 8) - math.atan((math.pi / 4 - math.pi / 8) / (1 + (math.pi / 4) * (math.pi / 8)))
0.0
>>> math.atan(math.pi / 4) + math.atan(math.pi / 8) - math.atan((math.pi / 4 + math.pi / 8) / (1 - (math.pi / 4) * (math.pi / 8)))
-2.220446049250313e-16 		#理论上和上一个式子相同, 限制一下小数位试一下:
>>> round(math.atan(math.pi / 4) + math.atan(math.pi / 8) - math.atan((math.pi / 4 + math.pi / 8) / (1 - (math.pi / 4) * (math.pi / 8))), 10)
-0.0

1.7, math.atan2(y, x)输出以弧度为单位返回 atan(y / x) 。结果是在 -pi 和 pi 之间。从原点到点 (x, y) 的平面矢量使该角度与正X轴成正比。atan2() 的点的两个输入的符号都是已知的,因此它可以计算角度的正确象限。

>>> math.atan2(1, 0)
1.5707963267948966
>>> math.atan2(1, -1) + math.atan2(1, 1)
3.141592653589793
>>> math.atan2(-1, -1) + math.atan2(-1, 1)
-3.141592653589793
>>> math.atan2(1, -1) + math.atan2(1, 1) == math.pi
True
>>> math.atan2(1, -1) + math.atan2(-1, -1)
0.0
>>> math.atan2(1, 0) ** 2 + math.atan2(-1, 0) ** 2 == math.pi ** 2 / 2
True

1.8, math.hypot(x, y)输出欧几里德范数, sqrt(xx + yy) 。 这是从原点到点 (x, y) 的向量长度, 换一种说法,即已知直角三角形的两条直角边的长度输出这个直角三角形斜边的长度(从数值上可以简单理解同勾股定理)。

>>> math.hypot(5, 12)
13.0
>>> math.hypot(-5, -12)
13.0
>>> math.hypot(-5, -12) - math.hypot(5, 12)
0.0
>>> math.hypot(-5, 12) == math.sqrt((-5) ** 2 + 12 ** 2)
True
>>> math.hypot(6, 8) / math.hypot(3, 4) == 2
True

1.9,math.degrees() 角度转换,即输出角度 x 从弧度转换为度数。

>>> math.degrees(math.pi / 2)
90.0
>>> math.degrees(45)
2578.3100780887044
>>> math.degrees(3.1415926)
179.99999692953102
>>> math.degrees(math.pi / 2) - math.degrees(math.pi / 4)
45.0
>>> math.degrees(1) - math.degrees(-1)
114.59155902616465
>>> math.degrees(1) + math.degrees(-1)
0.0
>>> math.degrees(1) + math.degrees(2) == math.degrees(3)
True
>>> math.degrees(3) * math.degrees(4) == math.degrees(2) * math.degrees(6)
True

1.10, math.radians()弧度转换,即输出角度 x 从度数转换为弧度, 弧度=角度/180*π.

>>> math.radians(180) == math.pi
True
>>> math.radians(180 / math.pi)
1.0
>>> math.radians(90) - math.radians(45)
0.7853981633974483
>>> math.radians(90) - math.radians(45) == math.pi / 4
True
>>> math.radians(3) * math.radians(30)
0.02741556778080377
>>> math.radians(3) * math.radians(30) == math.radians(2) * math.radians(45)
False

先整理到这里,有出入的地方欢迎各位大神指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值