Python_mathライブラリで使用頻度が高い標準メソッド10選
- 用途: 数値計算、数学的操作。
- 例:
math.sqrt
,math.pow
,math.sin
。
import math
使用率が高いメソッド10選
-
math.sqrt(x)
: 数値の平方根を返します。print(math.sqrt(16))
-
math.pow(x, y)
: xのy乗を計算します。print(math.pow(2, 3))
-
math.factorial(x)
: 数値xの階乗を計算します。print(math.factorial(5))
-
math.gcd(x, y)
: 2つの数値の最大公約数を返します。print(math.gcd(48, 18))
-
math.fabs(x)
: 数値の絶対値を浮動小数点で返します。print(math.fabs(-7.5))
-
math.ceil(x)
: 数値xを超えない最小の整数を返します(切り上げ)。print(math.ceil(2.3))
-
math.floor(x)
: 数値x未満の最大の整数を返します(切り捨て)。print(math.floor(2.7))
-
math.sin(x)
: 角度x(ラジアン)の正弦値を返します。print(math.sin(math.pi / 2))
-
math.log(x, base=e)
: 指定された底(base)におけるxの対数を返します。print(math.log(100, 10))
-
math.isclose(a, b, rel_tol=1e-9, abs_tol=0.0)
: 2つの数値が指定された許容範囲内で等しいかを判定します。print(math.isclose(1.000001, 1.0, rel_tol=1e-6))