三角比・三角関数の値を出力するPythonコード
三角関数の値をPythonで計算してみよう。
三角関数の値(NumPy)
数値処理を行うためにnumpy
モジュールをnp
として利用する。
np.sin()
, np.cos()
, np.tan()
関数の引数に弧度(rad)を入力して, 三角関数の値を出力する。
弧度の入力にはnp.pi
で $\pi$ の値を取得する。
度数法で入力する場合は, np.radians()
関数を利用して弧度に変換する。
Pythonコード入力例. $\displaystyle \sin \frac{\pi}{6}$ と $\sin 30^{\circ}$, $\cos 30^{\circ}$, $\tan 30^{\circ}$ の値を出力する。
import numpy as np
a = np.pi/6
b = np.radians(30)
print(np.sin(a))
print(np.sin(b))
print(np.cos(b))
print(np.tan(b))

三角関数の値(math)
数学の関数を利用するためにmath
モジュールを利用する。
math.sin()
, math.cos()
, math.tan()
関数の引数に弧度(rad)を入力して, 三角関数の値を出力する。
弧度の入力にはmath.pi
で $\pi$ の値を取得する。
度数法で入力する場合は, math.radians()
関数を利用して弧度に変換する。
Pythonコード入力例. $\displaystyle \sin \frac{\pi}{6}$ と $\sin 30^{\circ}$, $\cos 30^{\circ}$, $\tan 30^{\circ}$ の値を出力する。
import math
a = math.pi/6
b = math.radians(30)
print(math.sin(a))
print(math.sin(b))
print(math.cos(b))
print(math.tan(b))
