ラベル 数学関数math の投稿を表示しています。 すべての投稿を表示
ラベル 数学関数math の投稿を表示しています。 すべての投稿を表示

2020年4月12日日曜日

【標準ライブラリ】数学関数math・指数関数と対数関数

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

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

import math


1. 指数関数・対数関数の記述例

 10を底とした対数(常用対数)を求める場合はlog10を使用する。

import math

print(math.log10(100))

実行結果

2.0


 e(≈2.718281828459)を底とした対数(自然対数)を求める場合はlogを使用する。

import math

print(math.log(100))

実行結果

4.605170185988092


 2を底とした対数を求める場合はlog2を使用する。

import math

print(math.log2(100))

実行結果

6.643856189774724


 べき乗の計算。xのy乗はpow(x, y)で求める。

import math

print(math.pow(10, 2))

実行結果

100.0


 べき乗の計算はPython組み込み関数powでも同様に可能。また、演算子**でも可能。

import math

print(pow(10, 2))

実行結果

100

import math

print(10 ** 2)

実行結果

100


2. リファレンス

Python 標準ライブラリ > math --- 数学関数 > 指数関数と対数関数

使用バージョン:Python 3.7.0

【標準ライブラリ】数学関数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