복잡한 연산이 필요한 경우에는 math 모듈을 사용한다.
math 모듈을 사용하기 전에는 math 모듈부터 import 해야한다.
import math
math.ceil(3.14)
#4
math.copysign(3.14, -3)
#-3.14
math.fabs(-3.14)
#3.14
math.factorial(5)
# 120
math.floor(3.78)
#3
math.frexp(100)
(0.78125, 7)
0.78125*2**7
# 100.0
math.ldexp(0.78125, 7)
# 100
math.frexp()
의 반대math.gcd(6,8)
# 2
math.modf(3.14)
# (0.14000000000000012, 3.0)
math.modf()
함수는 입력값을 저수와 소수 부분으로 분리해 반환modf()
함수는 부동소수점의 값을 그대로 반환함math.trunc(3.14)
# 3
math.floor
와 비슷해 보이지만 다르다math.trunc(-3.14)
math.floor(-3.14)
# -3
# -4
trunc()
함수는 0으로 향하지만floor()
함수는 무조건 아래로만 향함math.log(10,10)
# 1
math.log(a,b)
는 로그 함수math.log1p(x)
math.log2(x)
math.log10()
math.pow(3,2)
# 9
math.pow(x,y)
는 x에 y 승을 계산한 결괏값을 반환math.sqrt(25)
# 5.0
math.acos(x) # 아크코사인
math.asin(x) # 아크사인
math.atan(x) # 아크탄젠트
math.atan2(y,x) # y/x 아크탄젠트
math.cos(x) # 코사인
math.sin(x) # 사인
math.tan(x) # 탄젠트
math.degrees(x)
# 라디안으로 표현된 각도를 60분법 각도로 변환
math.radians(x)
# 60분법으로 표현된 각도를 라디안 각도로 변환