#範例14-1:math函數 import math #印出所有math函數名稱 print(dir(math)) #常數pi, e print('常數 pi=', math.pi) print('常數 e=', math.e) #三角函數 #角度 = 30度 #degrees(x) 傳回弧度 x 的角度 (degree) #radians(x) 傳回角度 x 的弧度 (radian) d1 = 30 r1 = math.radians(d1) print('sin(30度) = ', math.sin(r1)) #反三角函數: arc Sin(0.5) = pi/6 r2 = math.asin(0.5) d2 = math.degrees(r2) print('ashin(0.5)= radians =',r2) print('ashin(0.5)= degrees =',d2) #factorial(x) 傳回 x 階乘 =x! d3 = math.factorial(5) print('5!=', d3) #絕對值 d4 = abs(-9) d5 = math.fabs(-7) print('絕對值 = abs(-9)=', d4) print('絕對值 = math.fabs(-7)=', d5) # 自然指數 = math.exp(1) print('自然指數 = math.exp(1) = ', math.exp(1)) print('自然指數 = math.e = ', math.e) #求20,16的最大公因數 print('求20,16的最大公因數=', math.gcd(20,16)) #四捨五入,無條件捨去,無條件進位 print('6.7的四捨五入 =', round(6.7)) print('6.7的無條件捨去 =', math.floor(6.7)) print('6.7的無條件進位 =', math.ceil(6.7)) #5的3次方 print('5的3次方=', 5**3) print('5的3次方=', math.pow(5,3)) #開根號 print('25開根號 = ', math.sqrt(25))