Python 數學模組函數備忘單
Python 數學標準函式庫的快速參考指南。了解如何使用三角函數、對數函數、舍入函數和算術函數。
捨去和基本算術
用於舍入、符號和基本數學運算的標準數學函數。
| 方法/功能 | 文法 | 描述 |
|---|---|---|
| ceil() | math.ceil(x) | 將數字 x 向上捨入到最接近的整數。 |
| floor() | math.floor(x) | 將數字 x 向下捨去到最接近的整數。 |
| trunc() | math.trunc(x) | 截斷小數部分,僅傳回 x 的整數部分。 |
| abs() | abs(x) | 傳回 x 的絕對值的內建函數。 |
| fmod() | math.fmod(x, y) | 傳回 x/y 的精確浮點餘數。 |
| gcd() | math.gcd(a, b) | 傳回整數 a 和 b 的最大公約數。 |
冪、根和對數
指數和對數計算。
| 方法/功能 | 文法 | 描述 |
|---|---|---|
| sqrt() | math.sqrt(x) | 傳回數字 x 的平方根。 |
| pow() | math.pow(x, y) | 以浮點形式傳回 x 的 y 次方。 |
| exp() | math.exp(x) | 傳回 e 的 x 次方。 |
| log() | math.log(x, [base]) | 傳回 x 以指定底數的對數(預設為自然對數)。 |
| log10() | math.log10(x) | 傳回 x 以 10 為底的對數。 |
互動式簡報腳本
run_all_cheat_methods.py
在編輯器中執行# ceil()
math.ceil(x)
# floor()
math.floor(x)
# trunc()
math.trunc(x)
# abs()
abs(x)
# fmod()
math.fmod(x, y)
# gcd()
math.gcd(a, b)
# sqrt()
math.sqrt(x)
# pow()
math.pow(x, y)
# exp()
math.exp(x)
# log()
math.log(x, [base])
# log10()
math.log10(x)常見問題解答
math.ceil() 和 math.floor() 有什麼不同?
math.ceil() 將數字向上捨入到最接近的整數,而 math.floor() 將數字向下捨去到最接近的整數。
為什麼我應該使用 math.isclose() 而不是直接檢查相等性?
浮點運算有精度限制。 math.isclose() 以容差檢查比較兩個值,看看它們是否近似相等。
相關主題
推薦的 Python 資源
透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。