Pythonのmathモジュールで指定した値のsin(サイン)、cos(コサイン)、tan(タンジェント)を取得する方法

Pythonのmathモジュールを使用して、指定した値のsin(サイン)、cos(コサイン)、tan(タンジェント)を計算するには、それぞれmath.sin()math.cos()math.tan()関数を使用します。以下はそれぞれの方法です。

python
import math

# 角度(ラジアン)を指定する場合
angle_in_radians = math.radians(45)  # 45度をラジアンに変換

# sin、cos、tanを計算する
sin_result = math.sin(angle_in_radians)
cos_result = math.cos(angle_in_radians)
tan_result = math.tan(angle_in_radians)

# 結果を表示する
print("sin(45度):", sin_result)
print("cos(45度):", cos_result)
print("tan(45度):", tan_result)

このコードでは、まず角度をラジアンに変換し、math.sin()math.cos()math.tan()関数を使用してそれぞれの三角関数を計算し、その結果を表示しています。必要に応じて、角度を別の値に変更して計算できます。

なお、math.radians()関数は角度をラジアンに変換するために使用されます。角度をラジアンに変換することで、math.sin()math.cos()math.tan()関数が正しく動作します。