2020年4月12日日曜日

【標準ライブラリ】数学関数math・三角関数

 Python標準ライブラリの数学関数ライブラリmathによる三角関数の計算について解説する。

ライブラリmathを使う際には以下のようにインポートが必要。

import math


1. 三角関数の記述例

 正弦(sin)を求めるsin。ラジアン単位の角度を与える。

import math

print(math.sin(0.2))

実行結果

0.19866933079506122


円周率πを使う際はpaiを用いる。
sin(π/2)を求める例。

import math

print(math.sin(math.pi/2))

実行結果

1.0


余弦(cos)を求めるcos

import math

print(math.cos(math.pi))

実行結果

-1.0


正接(tan)を求めるtan

import math

print(math.tan(math.pi/4))

実行結果

0.9999999999999999


逆三角関数は戻り値がラジアンとなる。
逆正弦(arcsin)を求めるasin

import math

print(math.asin(1))

実行結果

1.5707963267948966


逆余弦(arccos)を求めるacos

import math

print(math.acos(1))

実行結果

0.0


逆正接(arctan)を求めるatan

import math

print(math.atan(1))

実行結果

0.7853981633974483


2. リファレンス

Python 標準ライブラリ > math --- 数学関数 > 三角関数

使用バージョン:Python 3.7.0

0 件のコメント:

コメントを投稿