Pythonのmathモジュール完全ガイド|定数・数値関数・べき乗・三角関数の使い方
Pythonのmathモジュールとは
Pythonのmathモジュールは、数学計算のための関数や定数をまとめた標準ライブラリです。このモジュールの関数は整数型や浮動小数点型(実数)のオブジェクトを対象としており、複素数には対応していません。複素数を扱いたい場合は、代わりにcmathモジュールを使用します。
mathモジュールを利用するには、まずコード内でインポートします。
import math
mathモジュールの主な定数
mathモジュールには、計算式の中でそのまま使える便利な定数が用意されています。
| 番号 | 定数と説明 |
|---|---|
| 1 | pi 円周率πの値を返します(3.141592…)。 |
| 2 | E 自然対数の底eの値を返します(2.718281…)。 |
| 3 | tau τ(タウ=2π)の値を返します(6.283185…)。 |
| 4 | inf 無限大を表す浮動小数点数を返します。 |
| 5 | nan 非数(Not a Number)を表す特殊な値です。 |
数値処理・数値表現の関数
これらの関数は、数値をさまざまな形式で処理・変換するために使用します。
| 番号 | 関数と説明 |
|---|---|
| 1 | ceil(x) 天井値を返します。x以上の最小の整数です。 |
| 2 | copysign(x, y) xの絶対値にyの符号を付けて返します。 |
| 3 | fabs(x) xの絶対値を返します。 |
| 4 | factorial(x) xの階乗を返します(x ≥ 0)。 |
| 5 | floor(x) 床値を返します。x以下の最大の整数です。 |
| 6 | fsum(iterable) イテラブル内の全要素の合計を高精度で計算します。 |
| 7 | gcd(x, y) xとyの最大公約数を返します。 |
| 8 | isfinite(x) xが無限大でもNaNでもないかどうかを判定します。 |
| 9 | isinf(x) xが無限大かどうかを判定します。 |
| 10 | isnan(x) xが非数(NaN)かどうかを判定します。 |
| 11 | remainder(x, y) xをyで割った余りを求めます。 |
サンプルコード
import math
print('The Floor and Ceiling value of 23.56 are: ' + str(math.ceil(23.56)) + ', ' + str(math.floor(23.56)))
x = 10
y = -15
print('The value of x after copying the sign from y is: ' + str(math.copysign(x, y)))
print('Absolute value of -96 and 56 are: ' + str(math.fabs(-96)) + ', ' + str(math.fabs(56)))
my_list = [12, 4.25, 89, 3.02, -65.23, -7.2, 6.3]
print('Sum of the elements of the list: ' + str(math.fsum(my_list)))
print('The GCD of 24 and 56 : ' + str(math.gcd(24, 56)))
x = float('nan')
if math.isnan(x):
print('It is not a number')
x = float('inf')
y = 45
if math.isinf(x):
print('It is Infinity')
print(math.isfinite(x)) #xは有限の数ではない
print(math.isfinite(y)) #yは有限の数
実行結果
The Floor and Ceiling value of 23.56 are: 24, 23 The value of x after copying the sign from y is: -10.0 Absolute value of -96 and 56 are: 96.0, 56.0 Sum of the elements of the list: 42.13999999999999 The GCD of 24 and 56 : 8 It is not a number It is Infinity False True
べき乗・対数関数
これらの関数は、べき乗や対数に関するさまざまな計算に使用します。
| 番号 | 関数と説明 |
|---|---|
| 1 | pow(x, y) xのy乗の値を返します。 |
| 2 | sqrt(x) xの平方根を求めます。 |
| 3 | exp(x) eのx乗(e ≈ 2.718281)を求めます。 |
| 4 | log(x[, base]) xの対数を返します。baseを指定しない場合、底はe(自然対数)になります。 |
| 5 | log2(x) 底2の対数を返します。 |
| 6 | log10(x) 底10の対数を返します。 |
サンプルコード
import math
print('The value of 5^8: ' + str(math.pow(5, 8)))
print('Square root of 400: ' + str(math.sqrt(400)))
print('The value of 5^e: ' + str(math.exp(5)))
print('The value of Log(625), base 5: ' + str(math.log(625, 5)))
print('The value of Log(1024), base 2: ' + str(math.log2(1024)))
print('The value of Log(1024), base 10: ' + str(math.log10(1024)))
実行結果
The value of 5^8: 390625.0 Square root of 400: 20.0 The value of 5^e: 148.4131591025766 The value of Log(625), base 5: 4.0 The value of Log(1024), base 2: 10.0 The value of Log(1024), base 10: 3.010299956639812
三角関数・角度変換関数
これらの関数は、さまざまな三角関数の計算に使用します。引数の角度は基本的にラジアン単位で指定する点に注意しましょう。
| 番号 | 関数と説明 |
|---|---|
| 1 | sin(x) ラジアン単位のxの正弦(サイン)を返します。 |
| 2 | cos(x) ラジアン単位のxの余弦(コサイン)を返します。 |
| 3 | tan(x) ラジアン単位のxの正接(タンジェント)を返します。 |
| 4 | asin(x) サインの逆演算です。同様にacos、atanも用意されています。 |
| 5 | degrees(x) 角度xをラジアンから度に変換します。 |
| 6 | radians(x) 角度xを度からラジアンに変換します。 |
サンプルコード
import math
print('The value of Sin(60 degree): ' + str(math.sin(math.radians(60))))
print('The value of cos(pi): ' + str(math.cos(math.pi)))
print('The value of tan(90 degree): ' + str(math.tan(math.pi/2)))
print('The angle of sin(0.8660254037844386): ' + str(math.degrees(math.asin(0.8660254037844386))))
実行結果
The value of Sin(60 degree): 0.8660254037844386 The value of cos(pi): -1.0 The value of tan(90 degree): 1.633123935319537e+16 The angle of sin(0.8660254037844386): 59.99999999999999
まとめ
mathモジュールを使えば、丸め処理・最大公約数・階乗といった数値処理から、べき乗・対数・三角関数まで、日常的な数学計算を簡単に実装できます。角度の指定がラジアン単位であること、複素数には対応していないことの2点だけ注意すれば、科学技術計算やデータ分析など幅広い場面で活用できる強力なツールです。
-
Pythonのdecimalモジュール徹底解説!sqrt・exp・log・compareなど主要関数の使い方
Pythonには、10進浮動小数点数(decimal floating point)を扱うためのdecimalモジュールが標準ライブラリとして用意されています。このモジュールを使うと、正しく丸められた高精度な浮動小数点演算が可能になります。 通常のfloat型は内部で2進数によって数値を表現するため、「0.1 + 0.2」が厳密には0.3にならないといった誤差が発生します。decimalモジュールは、金額計算など厳密な精度が求められる場面で特に役立ちます。 利用するには、まずdecimalモジュールをインポートします。 import decimal 本記事では、decimalモジュールで使える
-
Pythonの文字列で数式を計算する方法:eval関数の使い方と注意点
Pythonでは、eval関数を使うことで、文字列として記述された数学式(数式表現)を実際に評価し、計算結果を取得できます。基本的な使い方例えば、(4*5) + 22 のように数式が含まれた文字列がある場合、それをevalに渡すだけで計算結果を得ることができます。>>> s = (4*5) + 22 >>> eval(s) 42evalの評価ルールevalは、括弧が省略された場合の演算子の優先順位などを含め、Pythonの通常の数式評価ルールに従って処理を行います。そのため、同じ式をPythonコードとして直接書いた場合とまったく同じ結果になります。セキュリ