[Python] Module : 수학 모듈
파이썬의 수학 모듈을 사용하여 설치없이 import 로 다양한 계산을 할 수 있다.
수론 및 표현 함수
함수 | 기능 |
math.ceil(x) | x를 올림하여 정수값을 반환. |
math.comb(n, k) | n개의 항목에서 k개를 선택하는 경우의 수를 반환. (nCk) |
math.copysign(x, y) | x의 크기(절댓값)와 y의 부호를 갖는 float를 반환. |
math.fabs(x) | x의 절댓값을 반환. |
math.factorial(n) | x의 팩토리얼을 반환. n! = (1 * 2 * 3 ... * n-1 * n). |
math.floor(x) | x를 내림하여 정수값을 반환. |
math.fmod(x, y) | x % y (x를 y로 나눈 나머지)를 반환. float로 작업할 때 선호. |
math.frexp(x) | x의 가수와 지수를 (m, e) 쌍으로 변환. x == m * 2**e. |
math.fsum(iterable) | iterable에 있는 값의 정확한 부동 소수점 합을 반환. |
math.gcd(*integers) | 정수 인자의 최대 공약수를 반환. (gcd() return 0). |
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) | a와 b가 같은 값인지 boolean으로 반환. |
math.isfinite(x) | x가 무한대나 NaN이 아니면 True 반환. |
math.isinf(x) | x가 양 또는 음의 무한대이면 True 반환. |
math.isnan(x) | x가 NaN이면 True 반환. |
math.isqrt(n) | 음이 아닌 정수 n의 정수 제곱근을 반환. |
math.lcm(*integers) | 정수 인자의 최소 공배수를 반환. (lcm() return 1). |
math.ldexp(x, i) | x * (2**i)를 반환. |
math.modf(x) | x의 소수와 정수 부분을 반환. 둘다 x의 부호를 가지며 float이다. |
math.nextafter(x, y, steps=1) | x의 다음 단계의 부동 소수점 값을 반환 (y방향으로).
|
math.perm(n, k=None) | n 개의 항목에서 k 개의 항목을 선택하는 경우의 수를 반환. (nPk) |
math.prod(iterable, *, start=1) | iterable에 있는 모든 요소의 곱을 계산. 곱의 기본 start 값 = 1 |
math.remainder(x, y) | x % y 를 반환. |
math.trunc(x) | x의 소수점을 제거한 정수값을 반환. |
math.ceil(x)
x를 올림하여 정수값을 반환.
import math
print(math.ceil(1.4))
print(math.ceil(5.3))
print(math.ceil(-5.3))
print(math.ceil(22.6))
print(math.ceil(10.0))
# Result
2
6
-5
23
10
math.comb(n, k)
n개의 항목에서 k개를 선택하는 경우의 수를 반환. (nCk)
import math
print(math.comb(7, 5))
# Result
21
math.copysign(x, y)
x의 크기(절댓값)와 y의 부호를 갖는 float를 반환.
import math
print(math.copysign(4, -1))
print(math.copysign(-8, 97.21))
print(math.copysign(-43, -76))
# Result
-4.0
8.0
-43.0
math.fabs(x)
x의 절댓값을 반환.
import math
print(math.fabs(-66.43))
print(math.fabs(-7))
# Result
66.43
7.0
math.factorial(n)
x의 팩토리얼을 반환. n! = (1 * 2 * 3 ... * n-1 * n).
import math
print(math.factorial(9))
print(math.factorial(6))
print(math.factorial(12))
# Result
362880
720
479001600
math.floor(x)
x를 내림하여 정수값을 반환.
import math
print(math.floor(0.6))
print(math.floor(1.4))
print(math.floor(5.3))
print(math.floor(-5.3))
print(math.floor(22.6))
print(math.floor(10.0))
# Result
0
1
5
-6
22
10
math.fmod(x, y)
x % y (x를 y로 나눈 나머지)를 반환. float로 작업할 때 선호.
import math
print(math.fmod(20, 4))
print(math.fmod(20, 3))
print(math.fmod(15, 6))
print(math.fmod(-10, 3))
print(math.fmod(0, 0))
# Result
0.0
2.0
3.0
-1.0
math.frexp(x)
x의 가수와 지수를 (m, e) 쌍으로 변환. x == m * 2**e.
import math
print(math.frexp(4))
print(math.frexp(-4))
print(math.frexp(7))
# Result
(0.5, 3)
(-0.5, 3)
(0.875, 3)
math.fsum(iterable)
iterable에 있는 값의 정확한 부동 소수점 합을 반환.
import math
print(math.fsum([1, 2, 3, 4, 5]))
print(math.fsum([100, 400, 340, 500]))
print(math.fsum([1.7, 0.3, 1.5, 4.5]))
# Result
15.0
1340.0
8.0
math.gcd(*integers)
정수 인자의 최대 공약수를 반환. (gcd() return 0).
import math
print (math.gcd(3, 6))
print (math.gcd(6, 12))
print (math.gcd(12, 36))
print (math.gcd(-12, -36))
print (math.gcd(5, 12))
print (math.gcd(10, 0))
print (math.gcd(0, 34))
print (math.gcd(0, 0))
# Result
3
6
12
12
1
10
34
0
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
a와 b가 같은 값인지 boolean으로 반환.
import math
print(math.isclose(1.233, 1.4566))
print(math.isclose(1.233, 1.233))
print(math.isclose(1.233, 1.24))
print(math.isclose(1.233, 1.233000001))
# Result
False
True
False
True
math.isfinite(x)
x가 무한대나 NaN이 아니면 True 반환.
import math
print(math.isfinite(2000))
print(math.isfinite(-45.34))
print(math.isfinite(+45.34))
print(math.isfinite(math.inf))
print(math.isfinite(float("nan")))
print(math.isfinite(float("inf")))
print(math.isfinite(float("-inf")))
print(math.isfinite(-math.inf))
print(math.isfinite(0.0))
# Result
True
True
True
False
False
False
False
False
True
math.isinf(x)
x가 양 또는 음의 무한대이면 True 반환.
import math
print (math.isinf (56))
print (math.isinf (-45.34))
print (math.isinf (+45.34))
print (math.isinf (math.inf))
print (math.isinf (float("nan")))
print (math.isinf (float("inf")))
print (math.isinf (float("-inf")))
print (math.isinf (-math.inf))
# Result
False
False
False
True
False
True
True
True
math.isnan(x)
x가 NaN이면 True 반환.
import math
print (math.isnan (56))
print (math.isnan (-45.34))
print (math.isnan (+45.34))
print (math.isnan (math.inf))
print (math.isnan (float("nan")))
print (math.isnan (float("inf")))
print (math.isnan (float("-inf")))
print (math.isnan (math.nan))
# Result
False
False
False
False
True
False
False
True
math.isqrt(n)
음이 아닌 정수 n의 정수 제곱근을 반환.
import math
print (math.sqrt(10))
print (math.sqrt(12))
print (math.sqrt(68))
print (math.sqrt(100))
print (math.isqrt(10))
print (math.isqrt(12))
print (math.isqrt(68))
print (math.isqrt(100))
# Result
3.1622776601683795
3.4641016151377544
8.246211251235321
10.0
3
3
8
10
math.lcm(*integers)
정수 인자의 최소 공배수를 반환. (lcm() return 1).
import math
print(math.lcm(2,3,4))
print(math.lcm(3,4,5))
# Result
12
60
math.ldexp(x, i)
x * (2**i)를 반환.
import math
print(math.ldexp(9, 3))
print(math.ldexp(-5, 2))
print(math.ldexp(15, 2))
# Result
72.0
-20.0
60.0
math.modf(x)
x의 소수와 정수 부분을 반환. 둘다 x의 부호를 가지며 float이다.
import math
print(math.modf(3.1415))
print(math.modf(4.2894))
print(math.modf(8.2595))
# Result
(0.14150000000000018, 3.0)
(0.28939999999999966, 4.0)
(0.2594999999999992, 8.0)
math.nextafter(x, y, steps=1)
x의 다음 단계의 부동 소수점 값을 반환 (y방향으로).
import math
print(math.nextafter(1,2))
print(math.nextafter(1,-1))
print(math.nextafter(2,0.0))
# Result
1.0000000000000002
0.9999999999999999
1.9999999999999998
math.perm(n, k=None)
n 개의 항목에서 k 개의 항목을 선택하는 경우의 수를 반환. (nPk)
import math
print (math.perm(7, 5))
# Result
2520
math.prod(iterable, *, start=1)
iterable에 있는 모든 요소의 곱을 계산. 곱의 기본 start 값 = 1
import math
sequence = (2, 2, 2)
print(math.prod(sequence))
# Result
8
math.remainder(x, y)
x % y 를 반환.
import math
print (math.remainder(9, 2))
print (math.remainder(9, 3))
print (math.remainder(18, 4))
# Result
1.0
0.0
2.0
math.trunc(x)
x의 소수점을 제거한 정수값을 반환.
import math
print(math.trunc(2.77))
print(math.trunc(8.32))
print(math.trunc(-99.29))
# Result
2
8
-99