🙌
Pythonの初心者レッスンーーmathモジュール
シーリズの目次
Pythonの初心者レッスンをここにまとめています。
Pythonのmathモジュールは、数学関連の機能を提供する標準ライブラリの一部です。このモジュールには、数学的な計算や関数を実行するためのさまざまな関数や定数が含まれています。具体的には、以下のような機能があります:
- 数学関数: 三角関数(sin、cos、tanなど)、指数関数(exp)、対数関数(log、log10など)、平方根(sqrt)など、一般的な数学関数が提供されています。
- 定数: 円周率 π(math.pi)、自然対数の底 e(math.e)、無限大を表す定数(math.inf)、NaN(Not a Number)を表す定数(math.nan)などが含まれています。
- 浮動小数点数操作: 整数や浮動小数点数の演算を行う関数や、整数と浮動小数点数の変換を行う関数もあります。
- 三角関数: sin、cos、tanなどの三角関数を計算するための関数が提供されています。
- 指数関数と対数関数: exp、log、log10などの指数関数と対数関数を計算するための関数が提供されています。
math関数を使うには先入が必要です。
import math
Pythonのmathモジュールで定義されている主要な定数についての説明をテーブルで示したものです。
定数名 | 説明 | 値 |
---|---|---|
math.pi |
円周率 π | 約 3.141592653589793 |
math.e |
自然対数の底 e | 約 2.718281828459045 |
math.tau |
円周率の2倍 (τ = 2π) | 約 6.283185307179586 |
math.inf |
正の無限大 | |
math.nan |
Not a Number (NaN) |
以下は、Pythonのmathモジュールで提供されているすべての関数とその簡単な説明をテーブルで示したものです。
関数名 | 説明 | 使用例 |
---|---|---|
math.acos(x) |
アークコサインを計算する | math.acos(0.5) |
math.acosh(x) |
双曲線余弦の逆関数を計算する | math.acosh(2) |
math.asin(x) |
アークサインを計算する | math.asin(0.5) |
math.asinh(x) |
双曲線正弦の逆関数を計算する | math.asinh(2) |
math.atan(x) |
アークタンジェントを計算する | math.atan(0.5) |
math.atan2(y, x) |
y / x のアークタンジェントを計算する | math.atan2(1, 2) |
math.atanh(x) |
双曲線正接の逆関数を計算する | math.atanh(0.5) |
math.ceil(x) |
x 以上の最小の整数を返す (天井関数) | math.ceil(3.14) |
math.comb(n, k) |
n 個から k 個を選ぶ組み合わせの数を計算する | math.comb(5, 2) |
math.copysign(x, y) |
y の符号を x にコピーする | math.copysign(-3.0, 1.0) |
math.cos(x) |
コサインを計算する | math.cos(math.pi) |
math.cosh(x) |
双曲線余弦を計算する | math.cosh(0) |
math.degrees(x) |
ラジアンを度に変換する | math.degrees(math.pi / 2) |
math.dist(p, q) |
2 点 p と q のユークリッド距離を計算する | math.dist((1, 2), (4, 6)) |
math.erf(x) |
誤差関数を計算する | math.erf(0) |
math.erfc(x) |
誤差補関数を計算する | math.erfc(0) |
math.exp(x) |
e の x 乗を計算する | math.exp(1) |
math.expm1(x) |
e の x 乗から 1 を引いた値を計算する | math.expm1(1) |
math.fabs(x) |
絶対値を返す | math.fabs(-3.5) |
math.factorial(x) |
x の階乗を計算する | math.factorial(5) |
math.floor(x) |
x 以下の最大の整数を返す (床関数) | math.floor(3.14) |
math.fmod(x, y) |
x を y で割った余りを返す | math.fmod(7, 3) |
math.frexp(x) |
x を仮数部と指数部に分解する | math.frexp(10) |
math.fsum(iterable) |
浮動小数点数のイテラブルの合計を計算する | math.fsum([0.1, 0.2, 0.3]) |
math.gamma(x) |
ガンマ関数を計算する | math.gamma(5) |
math.gcd(a, b) |
a と b の最大公約数を計算する | math.gcd(12, 8) |
math.hypot(x, y) |
x と y のユークリッド距離を計算する | math.hypot(3, 4) |
math.isclose(a, b) |
a と b がほぼ等しいかどうかをチェックする | math.isclose(1.0, 1.1, rel_tol=0.1) |
math.isfinite(x) |
x が有限の数かどうかをチェックする | math.isfinite(3.0) |
math.isinf(x) |
x が無限大かどうかをチェックする | math.isinf(math.inf) |
math.isnan(x) |
x が NaN (Not a Number) かどうかをチェックする | math.isnan(math.nan) |
math.isqrt(n) |
n の平方根の整数部分を計算する | math.isqrt(16) |
math.ldexp(x, i) |
x を 2 の i 乗倍する | math.ldexp(1.5, 2) |
math.lgamma(x) |
自然対数の絶対値のガンマ関数を計算する | math.lgamma(5) |
math.log(x) |
自然対数を計算する (底は e) | math.log(math.e) |
math.log10(x) |
常用対数を計算する (底は 10) | math.log10(100) |
math.log1p(x) |
1 + x の自然対数を計算する | math.log1p(math.e - 1) |
math.perm(n, k) |
n 個から k 個を選ぶ順列の数を計算する | math.perm(5, 2) |
math.pow(x, y) |
x の y 乗を計算する | math.pow(2, 3) |
math.prod(iterable) |
イテラブルの積を計算する | math.prod([2, 3, 4]) |
math.radians(x) |
度をラジアンに変換する | math.radians(180) |
math.remainder(x, y) |
x を y で割った余りを返す (浮動小数点数) | math.remainder(7, 3) |
math.sin(x) |
正弦を計算する | math.sin(math.pi / 2) |
math.sinh(x) |
双曲線正弦を計算する | math.sinh(0) |
math.sqrt(x) |
平方根を計算する | math.sqrt(16) |
math.tan(x) |
正接を計算する | math.tan(math.pi / 4) |
math.tanh(x) |
双曲線正接を計算する | math.tanh(0) |
math.trunc(x) |
x の整数部分を返す | math.trunc(3.14) |
Discussion