Python math モジュール
Python math モジュール多数の一般的な数学関数を提供しており、以下の処理を行うために使用できます:
- 三角関数の計算(sin / cos / tan)
- 対数と指数の演算(log / exp / pow)
- 整数化と丸め(ceil / floor / trunc)
- 組み合わせ・順列・階乗(comb / perm / factorial)
- 距離と幾何計算(dist / hypot / sqrt)
これは Python で数学演算を行う際に最もよく使用される標準ライブラリの 1 つです。
を利用する必要がありますmathモジュールで完了します。
mathモジュール内の関数は、特に明記されていない限り、戻り値はすべて浮動小数点数です。
複素数を計算する必要がある場合は、使用してくださいcmathモジュール内の同名の関数。
math 関数を使用するには、最初にインポートする必要があります:
import math
math モジュールの内容を確認する:
>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
なぜ math モジュールを使うのか?
Python には多くの基本演算子が備わっていますが(例:+、-、*、/)、より複雑な数学計算には、例えば:
- 平方根
- 三角関数
- 自然対数
- 浮動小数点精度の処理
Python math モジュール使用例
import math
print(math.sqrt(16)) # 4.0
print(math.pow(2, 3)) # 8.0
print(math.ceil(4.2)) # 5
print(math.floor(4.9)) # 4
print(math.sin(math.pi/2))# 1.0
print(math.sqrt(16)) # 4.0
print(math.pow(2, 3)) # 8.0
print(math.ceil(4.2)) # 5
print(math.floor(4.9)) # 4
print(math.sin(math.pi/2))# 1.0
出力結果は:
4.0 8.0 5 4 1.0
math モジュール定数
| 定数 | 説明 |
|---|---|
| math.e | オイラー数(2.7182...)を返します。 |
| math.inf | 正の無限大の浮動小数点数を返します。 |
| math.nan | 浮動小数点値 NaN(not a number)を返します。 |
| math.pi | π は一般に円周率を指します。円周率 PI(3.1415...) |
| math.tau | 数学定数 τ = 6.283185...、利用可能な精度まで正確です。Tau は円周定数であり、2π に等しく、円の周長と半径の比です。 |
math モジュール関数
| 関数 | 説明 |
|---|---|
| math.acos(x) | x の逆余弦(アークコサイン)を返します。結果の範囲は 0 から pi の間です。 |
| math.acosh(x) | x の逆双曲余弦(アークハイパボリックコサイン)を返します。 |
| math.asin(x) | x の逆正弦(アークサイン)を返します。結果の範囲は -pi/2 から pi/2 の間です。 |
| math.asinh(x) | x の逆双曲正弦(アークハイパボリックサイン)を返します。 |
| math.atan(x) | x の逆正接(アークタンジェント)を返します。結果の範囲は -pi/2 から pi/2 の間です。 |
| math.atan2(y, x) | 指定された X および Y 座標値の逆正接(アークタンジェント)を返します。結果は -pi と pi の間です。 |
| math.atanh(x) | x の逆双曲正接(アークハイパボリックタンジェント)を返します。 |
| math.ceil(x) | x を最も近い整数に切り上げます |
| math.comb(n, k) | 重複なし・順序なしで n 個の中から k 個を選ぶ方法の総数を返します。 |
| math.copysign(x, y) | x の絶対値と y の符号に基づく浮動小数点数を返します。 |
| math.cos() | x ラジアンの余弦(コサイン)を返します。 |
| math.cosh(x) | x の双曲余弦(ハイパボリックコサイン)を返します。 |
| math.degrees(x) | 角度 x をラジアンから度に変換します。 |
| math.dist(p, q) | 座標シーケンス(または反復可能オブジェクト)として与えられた 2 点 p と q の間のユークリッド距離を返します。2 点は同じ次元を持つ必要があります。 |
| math.erf(x) | 数値の誤差関数を返します |
| math.erfc(x) | x における相補誤差関数を返します |
| math.exp(x) | e の x 乗(E)を返します。x、ここで e = 2.718281... は自然対数の底です。 |
| math.expm1() | E を返しますx- 1、e の x 乗、Ex、ここで e = 2.718281... は自然対数の底です。これは通常、math.e ** x や pow(math.e, x) よりも正確です。 |
| math.fabs(x) | x の絶対値を返します。 |
| math.factorial(x) | x の階乗を返します。x が整数でない場合、または負の場合は ValueError が発生します。 |
| math.floor() | 数値を最も近い整数に切り下げます |
| math.fmod(x, y) | x/y の余りを返します |
| math.frexp(x) | x の仮数と指数を (m, e) のペアで返します。m は浮動小数点数、e は整数であり、正確に x == m * 2**e となります。x がゼロの場合は (0.0, 0) を返し、それ以外の場合は 0.5 <= abs(m) < 1 を返します。 |
| math.fsum(iterable) | 反復可能オブジェクト(タプル、配列、リストなど)内の要素の合計を浮動小数点値として返します。 |
| math.gamma(x) | x におけるガンマ関数の値を返します。 |
| math.gcd() | 指定された整数引数の最大公約数を返します。 |
| math.hypot() | ユークリッドノルム sqrt(sum(x**2 for x in coordinates)) を返します。これは原点から座標で指定された点までのベクトルの長さです。 |
| math.isclose(a,b) | 2 つの値が互いに近いかどうかを確認します。a と b の値が近い場合は True を返し、それ以外の場合は False を返します。 |
| math.isfinite(x) | x が有限かどうかを判定します。x が無限大でも NaN でもない場合は True を返し、それ以外の場合は False を返します。 |
| math.isinf(x) | x が無限大かどうかを判定します。x が正または負の無限大の場合は True を返し、それ以外の場合は False を返します。 |
| math.isnan() | 数値が NaN かどうかを判定します。x が NaN(not a number)の場合は True を返し、それ以外の場合は False を返します。 |
| math.isqrt() | 平方根を最も近い整数に切り下げます |
| math.ldexp(x, i) | x * (2**i) を返します。これは基本的にmath.frexp() の逆関数です。 |
| math.lgamma() | x の絶対値におけるガンマ関数の自然対数を返します。 |
| math.log(x[, base]) | 1 つの引数の場合、x の自然対数(底は e)を返します。 |
| math.log10(x) | x の底を 10 とする対数を返します。 |
| math.log1p(x) | 1+x の自然対数(底は e)を返します。 |
| math.log2(x) | x の底を 2 とする対数を返します |
| math.perm(n, k=None) | 重複なし・順序ありで n 個の中から k 個を選ぶ方法の総数を返します。 |
| math.pow(x, y) | x の y 乗を返します。 |
| math.prod(iterable) | 反復可能オブジェクト内のすべての要素の積を計算します。 |
| math.radians(x) | 角度 x を度からラジアンに変換します。 |
| math.remainder(x, y) | IEEE 754 スタイルの x を y で割った余りを返します。 |
| math.sin(x) | x ラジアンの正弦(サイン)を返します。 |
| math.sinh(x) | x の双曲正弦(ハイパボリックサイン)を返します。 |
| math.sqrt(x) | x の平方根を返します。 |
| math.tan(x) | x ラジアンの正接(タンジェント)を返します。 |
| math.tanh(x) | x の双曲正接(ハイパボリックタンジェント)を返します。 |
| math.trunc(x) | x を切り捨てた整数部分を返します。つまり、小数部分を削除して整数部分を返します。 |
| math.cbrt(x) | x の立方根を返します。 |
| math.exp2(x) | 2 の x 乗、すなわち 2**x を返します。 |
| math.lcm(*integers) | 複数の整数の最小公倍数を返します。 |
| math.modf(x) | x の小数部分と整数部分を返します。結果はタプル (fractional, integer) です。 |
| math.nextafter(x, y[, steps]) | x から y の方向に指定された浮動小数点ステップ数だけ進んだ後の値を返します。 |
| math.sumprod(p, q) | 2 つの反復可能オブジェクトの対応する要素の積の合計を返します。 |
| math.ulp(x) | x の最小有効浮動小数点単位(Unit in the Last Place)を返します。 |
バージョン互換性の説明
以下の関数は Python 3.8+ / 3.9+ / 3.11+ で新たに追加されました:
math.comb():Python 3.8+math.perm():Python 3.8+math.lcm():Python 3.9+math.cbrt():Python 3.11+math.exp2():Python 3.11+math.sumprod():Python 3.12+