
- Python 基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World 程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境設定
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode 系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python 控制語句
- Python - 控制流
- Python - 決策制定
- Python - If 語句
- Python - If else
- Python - 巢狀 If
- Python - Match-Case 語句
- Python - 迴圈
- Python - for 迴圈
- Python - for-else 迴圈
- Python - While 迴圈
- Python - break 語句
- Python - continue 語句
- Python - pass 語句
- Python - 巢狀迴圈
- Python 函式與模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅關鍵字引數
- Python - 位置引數
- Python - 僅位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python 字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python 列表
- Python - 列表
- Python - 訪問列表項
- Python - 修改列表項
- Python - 新增列表項
- Python - 刪除列表項
- Python - 迴圈遍歷列表
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python 元組
- Python - 元組
- Python - 訪問元組項
- Python - 更新元組
- Python - 解包元組
- Python - 迴圈遍歷元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python 集合
- Python - 集合
- Python - 訪問集合項
- Python - 新增集合項
- Python - 刪除集合項
- Python - 迴圈遍歷集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python 字典
- Python - 字典
- Python - 訪問字典項
- Python - 修改字典項
- Python - 新增字典項
- Python - 刪除字典項
- Python - 字典檢視物件
- Python - 迴圈遍歷字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python 陣列
- Python - 陣列
- Python - 訪問陣列項
- Python - 新增陣列項
- Python - 刪除陣列項
- Python - 迴圈遍歷陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python 檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS 檔案/目錄方法
- Python - OS 路徑方法
- 面向物件程式設計
- Python - OOPs 概念
- Python - 類與物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝器類
- Python - 列舉
- Python - 反射
- Python 錯誤與異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except 塊
- Python - try-finally 塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀 try 塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python 多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 連線執行緒
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python 同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python 網路
- Python - 網路
- Python - 套接字程式設計
- Python - URL 處理
- Python - 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 雜項
- Python - 日期與時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI 程式設計
- Python - XML 處理
- Python - GUI 程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送電子郵件
- Python - 擴充套件
- Python - 工具/實用程式
- Python - GUI
- Python 高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - 猴子補丁
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python 有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
理論與表示函式
ceil() 函式
math 模組中的 ceil() 函式返回 x 的上舍入值,即不小於 x 的最小整數。
語法
以下是 ceil() 函式的語法:
import math math.ceil(x)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後需要使用 math 靜態物件呼叫此函式。
引數
x - 數值表示式。
返回值
此函式返回不小於“x”的最小整數。
示例
以下示例顯示了 ceil() 函式的使用。
from math import ceil, pi a = -45.17 ceil_a = ceil(a) print ("a: ",a, "ceil(a): ", ceil_a) a = 100.12 ceil_a = ceil(a) print ("a: ",a, "ceil(a): ", ceil_a) a = 100.72 ceil_a = ceil(a) print ("a: ",a, "ceil(a): ", ceil_a) a = pi ceil_a = ceil(a) print ("a: ",a, "ceil(a): ", ceil_a)
執行上述程式時,會產生以下輸出:
a: -45.17 ceil(a): -45 a: 100.12 ceil(a): 101 a: 100.72 ceil(a): 101 a: 3.141592653589793 ceil(a): 4
comb() 函式
math 模組中的 comb() 函式返回從“y”個專案中選擇“x”個專案的組合數,不重複且無順序。當 x <= y 時,它計算為 n! / (x! * (x-y)!),當 x > y 時,它計算為零。
語法
以下是 comb() 函式的語法:
math.comb(x, y)
引數
x - 必需。要從中選擇的專案的正整數。
y - 必需。要選擇的專案的正整數。
返回值
如果“x”的值大於 y 的值,則返回 0。如果 x 和 y 為負數,則會發生 ValueError。如果兩個引數都不是整數,則會引發 TypeError。
示例
from math import comb x=7 y=5 combinations = comb(x,y) print ("x: ",x, "y: ", y, "combinations: ", combinations) x=5 y=7 combinations = comb(x,y) print ("x: ",x, "y: ", y, "combinations: ", combinations)
它將產生以下輸出:
x: 7 y: 5 combinations: 21 x: 5 y: 7 combinations: 0
copysign() 函式
math 模組中的 copy() 函式返回一個浮點數,其大小(絕對值)為 x,但符號為 y。
語法
以下是 copysign() 函式的語法:
math.copysign(x, y)
引數
- x - 一個數字
- y - 一個數字
返回值
返回一個浮點數,其大小(絕對值)與 x 相同,但符號與 y 相同。
示例
from math import copysign x=10 y=-20 result = copysign(x,y) print ("x: ",x, "y: ", y, "copysign: ", result) x=-10 y=20 result = copysign(x,y) print ("x: ",x, "y: ", y, "copysign: ", result) x=-10 y= -0.0 result = copysign(x,y) print ("x: ",x, "y: ", y, "copysign: ", result)
它將產生以下輸出 -
x: 10 y: -20 copysign: -10.0 x: -10 y: 20 copysign: 10.0 x: -10 y: -0.0 copysign: -10.0
fabs() 函式
math 模組中的 fabs() 函式返回給定數字的絕對值。即使引數是整數,它也始終返回浮點數。
語法
以下是 fabs() 函式的語法 -
math.fabs(x)
引數
x - 一個數字
返回值
返回一個浮點數,其大小(絕對值)與x相同
示例
from math import fabs x=10.25 result = fabs(x) print ("x: ",x, "fabs value: ", result) x=20 result = fabs(x) print ("x: ",x,"fabs value: ", result) x=-10 result = fabs(x) print ("x: ",x, "fabs value: ", result)
它將產生以下輸出 -
x: 10.25 fabs value: 10.25 x: 20 fabs value: 20.0 x: -10 fabs value: 10.0
factorial() 函式
math 模組中的 factorial() 函式返回給定整數的階乘值。一個數的階乘是從 1 到該數的所有整數的乘積。表示為 x!,其中 4! = 4X3X2X1。
語法
以下是 factorial() 函式的語法 -
math.factorial(x)
引數
x - 一個整數
返回值
返回一個整數,表示從 1 到 x 的所有整數的乘積。對於負數 x,會引發 ValueError。
示例
from math import factorial x=10 result = factorial(x) print ("x: ",x, "x! value: ", result) x=5 result = factorial(x) print ("x: ",x,"x! value: ", result) x=-5 result = factorial(x) print ("x: ",x, "x! value: ", result)
它將產生以下輸出 -
x: 10 x! value: 3628800 x: 5 x! value: 120 Traceback (most recent call last): File "C:\Users\mlath\examples\main.py", line 14, in <module> result = factorial(x) ^^^^^^^^^^^^ ValueError: factorial() not defined for negative values
floor() 函式
floor() 函式返回x的向下取整,即不大於x的最大整數。
語法
以下是 floor() 函式的語法 -
import math math.floor(x)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 數值表示式。
返回值
此函式返回不大於x的最大整數。
示例
以下示例顯示了 floor() 函式的用法。
from math import floor, pi a = -45.17 floor_a = floor(a) print ("a: ",a, "floor(a): ", floor_a) a = 100.12 floor_a = floor(a) print ("a: ",a, "floor(a): ", floor_a) a = 100.72 floor_a = floor(a) print ("a: ",a, "floor(a): ", floor_a) a = pi floor_a = floor(a) print ("a: ",a, "floor(a): ", floor_a)
執行上述程式時,會產生以下輸出:
a: -45.17 floor(a): -46 a: 100.12 floor(a): 100 a: 100.72 floor(a): 100 a: 3.141592653589793 floor(a): 3
fmod() 函式
math 模組中的 fmod() 函式返回與 x%y 相同的結果。但是,fmod() 給出的模除結果比模運算子更精確。
語法
以下是 fmod() 函式的語法 -
import math math.fmod(x, y)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 要被除的正數或負數。
y - 要除以的正數或負數。
返回值
此函式返回x除以y的餘數。
示例
以下示例顯示了 fmod() 函式的用法。
from math import fmod a=10 b=2 c=fmod(a,b) print ("a=",a, "b=",b, "fmod(a,b)=", c) a=10 b=4 c=fmod(a,b) print ("a=",a, "b=",b, "fmod(a,b)=", c) a=0 b=10 c=fmod(a,b) print ("a=",a, "b=",b, "fmod(a,b)=", c) a=10 b=1.5 c=fmod(a,b) print ("a=",a, "b=",b, "fmod(a,b)=", c)
執行上述程式時,會產生以下輸出:
a= 10 b= 2 fmod(a,b)= 0.0 a= 10 b= 4 fmod(a,b)= 2.0 a= 0 b= 10 fmod(a,b)= 0.0 a= 10 b= 1.5 fmod(a,b)= 1.0
frexp() 函式
math 模組中的 frexp() 函式將 x 的尾數和指數作為 (m, e) 對返回。m 是一個浮點數,e 是一個整數,使得 x == m * 2**e 完全成立。
語法
以下是 frexp() 函式的語法 -
import math math.frexp(x)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 正數或負數。
返回值
此函式返回尾數和指數 (m,n),使得 m*2**e 等於x。
示例
以下示例顯示了 frexp() 函式的用法。
from math import frexp x = 8 y = frexp(x) print ("x:", x, "frex(x):",y) print ("Cross-check") m,n=y x = m*2**n print ("frexp(x): ",y, "x:", x)
它將產生以下輸出 -
x: 8 frex(x): (0.5, 4) Cross-check frexp(x): (0.5, 4) x: 8.0
fsum() 函式
math 模組中的 fsum() 函式返回可迭代物件(即列表、元組、陣列)中所有數字項的浮點和。
語法
以下是 fsum() 函式的語法 -
import math math.sum(x)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 由數字組成的可迭代物件。其他型別會導致 TypeError。
返回值
此函式返回可迭代物件中所有項的浮點數和。
示例
以下示例顯示了 fsum() 函式的用法 -
from math import fsum x = [1,2,3,4,5] y = fsum(x) print ("x:", x, "fsum(x):",y) x = (10,11,12) y = fsum(x) print ("x:", x, "fsum(x):",y) x = [1,'a',2] y = fsum(x) print ("x:", x, "fsum(x):",y)
它將產生以下輸出 -
x: [1, 2, 3, 4, 5] fsum(x): 15.0 x: (10, 11, 12) fsum(x): 33.0 Traceback (most recent call last): File "C:\Users\mlath\examples\main.py", line 13, in <module> y = fsum(x) ^^^^^^^ TypeError: must be real number, not str
gcd() 函式
math 模組中的 gcd() 函式返回所有整數的最大公約數。返回值是所有引數的最大正整數約數。如果所有數字都為零,則返回 0。gcd() 沒有引數時返回 0。
語法
以下是 gcd() 函式的語法 -
import math math.gcd(x)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
- x1, x2 - 整數
返回值
此函式返回最大公約數,為整數
示例
以下示例顯示了 gcd() 函式的用法 -
from math import gcd x, y, z = 12, 8, 24 result = gcd(x, y, z) print ("x: {} y: {} z: {}".format(x,y,z), "gcd(x,y, z):",result) x, y, z = 12, 6, 9 result = gcd(x, y, z) print ("x: {} y: {} z: {}".format(x,y,z), "gcd(x,y, z):",result) x, y = 7, 12 result = gcd(x, y) print ("x: {} y: {}".format(x,y), "gcd(x,y):",result) x, y = 0, 12 result = gcd(x, y) print ("x: {} y: {}".format(x,y), "gcd(x,y):",result)
它將產生以下輸出 -
x: 12 y: 8 z: 24 gcd(x,y, z): 4 x: 12 y: 6 z: 9 gcd(x,y, z): 3 x: 7 y: 12 gcd(x,y): 1 x: 0 y: 12 gcd(x,y): 12
isclose() 函式
math 模組中的 isclose() 函式如果兩個數字引數的值彼此接近,則返回 True,否則返回 False。
語法
以下是 isclose() 函式的語法 -
import math math.isclose(x,y)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。接近程度根據給定的絕對和相對容差來確定。
引數
x - 要檢查接近度的第一個值。
y - 要檢查接近度的第二個值。
rel_tol - 相對容差(可選)。
abs_tol - 最小絕對容差。(可選)。
返回值
如果x和y彼此接近,則此函式返回 True,否則返回 False。
示例
以下示例顯示了 isclose() 函式的用法。
from math import isclose x = 2.598 y = 2.597 result = isclose(x, y) print ("x: {} y:{}".format(x,y), "isclose(x,y):",result) x = 5.263 y = 5.263000001 result = isclose(x, y) print ("x: {} y:{}".format(x,y), "isclose(x,y):",result)
它將產生以下輸出 -
x: 2.598 y:2.597 isclose(x,y): False x: 5.263 y:5.263000001 isclose(x,y): True
isfinite() 函式
math 模組中的 isfinite() 函式如果引數既不是無窮大也不是 NaN,則返回 True,否則返回 False。
語法
以下是 isfinite() 函式的語法 -
import math math.isfinite(x)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 數字運算元。
返回值
如果x既不是無窮大也不是NaN,則此函式返回 True,否則返回 False。
示例
以下示例顯示了 isfinite() 函式的用法 -
from math import isfinite x = 1.23E-5 result = isfinite(x) print ("x:", x, "isfinite(x):",result) x = 0 result = isfinite(x) print ("x:", x, "isfinite(x):",result) x = float("Inf") result = isfinite(x) print ("x:", x, "isfinite(x):",result)
它將產生以下輸出 -
x: 1.23e-05 isfinite(x): True x: 0 isfinite(x): True x: inf isfinite(x): False
isinf() 函式
math 模組中的 isinf() 函式如果引數是正無窮大或負無窮大,則返回 True,否則返回 False。它與 isfinite() 函式相反。
語法
以下是 isinf() 函式的語法 -
import math math.isinf(x)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 數字運算元
返回值
如果x是正無窮大或負無窮大,則此函式返回 True,否則返回 False。
示例
以下示例顯示了 isinf() 函式的用法 -
from math import isinf x = 1.23E-5 result = isinf(x) print ("x:", x, "isinf(x):",result) x = float("-Infinity") result = isinf(x) print ("x:", x, "isinf(x):",result) x = float("Inf") result = isinf(x) print ("x:", x, "isinf(x):",result)
它將產生以下輸出 -
x: 1.23e-05 isinf(x): False x: -inf isinf(x): True x: inf isinf(x): True
isnan() 函式
math 模組中的 isnan() 函式如果 x 是 NaN(非數字),則返回 True,否則返回 False。
語法
以下是 isnan() 函式的語法 -
import math math.isnan(x)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 數字運算元
返回值
此函式返回如果x是 NaN(非數字),則返回 True,否則返回 False。
示例
以下示例顯示了 isnan() 函式的用法 -
from math import isnan x = 156.78 result = isnan(x) print ("x:", x, "isnan(x):",result) x = float("-Infinity") result = isnan(x) print ("x:", x, "isnan(x):",result) x = float("NaN") result = isnan(x) print ("x:", x, "isnan(x):",result) x = "NaN" result = isnan(x) print ("x:", x, "isnan(x):",result)
它將產生以下輸出 -
x: 156.78 isnan(x): False x: -inf isnan(x): False x: nan isnan(x): True Traceback (most recent call last): File "C:\Users\mlath\examples\main.py", line 16, in <module> result = isnan(x) ^^^^^^^^ TypeError: must be real number, not str
請注意,“NaN”是一個字串,而不是 NaN 數字運算元。
isqrt() 函式
math 模組中的 isqrt() 函式返回非負整數的整數平方根。這是給定正整數的精確平方根的向下取整。結果值的平方小於或等於引數數字。
語法
以下是 isqrt() 函式的語法 -
import math math.isqrt(x)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 非負整數。
返回值
此函式返回向下舍入到最接近整數的平方根數。
示例
以下示例顯示了 isqrt() 函式的用法 -
from math import isqrt, sqrt x = 12 y = isqrt(x) z = sqrt(x) print ("x:", x, "isqrt(x):",y, "sqrt(x):", z) x = 16 y = isqrt(x) z = sqrt(x) print ("x:", x, "isqrt(x):",y, "sqrt(x):", z) x = -100 y = isqrt(x) z = sqrt(x) print ("x:", x, "isqrt(x):",y, "sqrt(x):", z)
為了比較,還計算了 sqrt() 函式的值。sqrt() 返回浮點數,isqrt() 返回整數。對於 isqrt(),數字必須是非負數。
它將產生以下輸出 -
x: 12 isqrt(x): 3 sqrt(x): 3.4641016151377544 x: 16 isqrt(x): 4 sqrt(x): 4.0 Traceback (most recent call last): File "C:\Users\mlath\examples\main.py", line 14, in <module> y = isqrt(x) ^^^^^^^^ ValueError: isqrt() argument must be nonnegative
lcm() 函式
math 模組中的 lcm() 函式返回兩個或多個整數引數的最小公倍數。對於非零引數,該函式返回所有引數的最小正整數倍數。如果任何引數為零,則返回值為 0。
語法
以下是 lcm() 函式的語法 -
import math math.lcm(x1, x2, . . )
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x1, x2, . . - 整數
返回值
此函式返回所有整數的最小公倍數。
示例
以下示例顯示了 lcm() 函式的用法 -
from math import lcm x = 4 y = 12 z = 9 result = lcm(x,y,z) print ("x: {} y: {} z: {}".format(x,y,z), "lcm(x,y,z):", result) x = 5 y = 15 z = 0 result = lcm(x,y,z) print ("x: {} y: {} z: {}".format(x,y,z), "lcm(x,y,z):", result) x = 4 y = 3 z = 6 result = lcm(x,y,z) print ("x: {} y: {} z: {}".format(x,y,z), "lcm(x,y,z):", result)
它將產生以下輸出 -
x: 4 y: 12 z: 9 lcm(x,y,z): 36 x: 5 y: 15 z: 0 lcm(x,y,z): 0 x: 4 y: 3 z: 6 lcm(x,y,z): 12
ldexp() 函式
math 模組中的 ldexp() 函式返回第一個數字與第二個數字的指數的乘積。因此,ldexp(x,y) 返回 x*2**y。這是 frexp() 函式的反函式。
語法
以下是 ldexp() 函式的語法 -
import math math.lcm(x,y)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 正數或負數。
y - 正數或負數。
返回值
此函式返回 x*2**y
示例
以下示例顯示了 ldexp() 函式的用法 -
from math import ldexp, frexp x = 0.5 y = 4 z = ldexp(x,y) print ("x: {} y: {}".format(x,y), "ldexp(x,y)", z) print ("Cross-check") x,y = frexp(z) print ("ldexp value:", z, "frexp(z):",x,y )
它將產生以下輸出 &minu;
x: 0.5 y: 4 ldexp(x,y) 8.0 Cross-check ldexp value: 8.0 frexp(z): 0.5 4
modf() 函式
modf() 方法將x的小數部分和整數部分作為兩個元素的元組返回。這兩個部分與x具有相同的符號。整數部分作為浮點數返回。
語法
以下是 modf() 方法的語法 -
import math math.modf( x )
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 數值表示式。
返回值
此方法將 x 的小數部分和整數部分作為兩個元素的元組返回。這兩個部分與 x 具有相同的符號。整數部分作為浮點數返回。
示例
以下示例顯示了 modf() 方法的用法 -
from math import modf, pi a = 100.72 modf_a = modf(a) print ("a: ",a, "modf(a): ", modf_a) a = 19 modf_a = modf(a) print ("a: ",a, "modf(a): ", modf_a) a = pi modf_a = modf(a) print ("a: ",a, "modf(a): ", modf_a)
它將產生以下輸出 -
a: 100.72 modf(a): (0.7199999999999989, 100.0) a: 19 modf(a): (0.0, 19.0) a: 3.141592653589793 modf(a): (0.14159265358979312, 3.0)
nextafter() 函式
nextafter() 函式返回x朝向y的下一個浮點值。
如果 y>x,則 x 增大。
如果 y<x,則 x 減小。
語法
以下是 nextafter() 函式的語法 -
import math math.nextafter( x, y )
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x 和 y - 數字運算元。
返回值
此函式返回下一個浮點值。
示例
以下示例顯示了 nextafter() 函式的用法 -
from math import nextafter x=1.5 y=100 z=nextafter(x, y) print ("x: {} y: {}".format(x,y), "nextafter(x,y)", z) x=1.5 y=float("-inf") z=nextafter(x, y) print ("x: {} y: {}".format(x,y), "nextafter(x,y)", z) x=0 y=float("inf") z=nextafter(x, y) print ("x: {} y: {}".format(x,y), "nextafter(x,y)", z)
它將產生以下輸出 -
x: 1.5 y: 100 nextafter(x,y) 1.5000000000000002 x: 1.5 y: -inf nextafter(x,y) 1.4999999999999998 x: 0 y: inf nextafter(x,y) 5e-324
perm() 函式
perm() 函式計算排列。它返回從y個專案中選擇x個專案(不重複且有順序)的方法數量。
排列定義為 xPy = x! / (x-y)!(當 y<=x 時)並且在 y>x 時評估為零。
語法
以下是 perm() 函式的語法 -
import math math.perm( x, y )
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 必需。要從中選擇的專案的正整數。
y - 必需。要選擇的專案的正整數。
返回值
此函式返回xPy的排列值 = x! / (x-y)!。
示例
以下示例顯示了 perm() 函式的用法。
from math import perm x=5 y=3 permutations = perm(x,y) print ("x: ",x, "y: ", y, "permutations: ", permutations) x=3 y=5 permutations = perm(x,y) print ("x: ",x, "y: ", y, "permutations: ", permutations)
它將產生以下輸出 -
x: 5 y: 3 permutations: 60 x: 3 y: 5 permutations: 0
prod() 函式
prod() 函式計算作為引數給定的可迭代物件(列表、元組)中所有數字項的乘積。第二個引數是起始值,預設值為 1。
語法
以下是 prod() 函式的語法 -
import math math.prod(iterable, start)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
iterable - 必需。必須包含數字運算元。
start - 預設值為 1。
返回值
此函式返回可迭代物件中所有項的乘積。
示例
以下示例顯示了 prod() 函式的用法 -
from math import prod x = [2,3,4] product = prod(x) print ("x: ",x, "product: ", product) x = (5,10,15) product = prod(x) print ("x: ",x, "product: ", product) x = [2,3,4] y = 3 product = prod(x, start=y) print ("x: ",x,"start:", y, "product: ", product)
它將產生以下輸出 -
x: [2, 3, 4] product: 24 x: (5, 10, 15) product: 750 x: [2, 3, 4] start: 3 product: 72
remainder() 函式
remainder() 函式返回 x 相對於 y 的餘數。這是 x - n*y 的差值,其中 n 是最接近商 x / y 的整數。如果 x / y 恰好位於兩個連續整數之間,則對於 n 使用最接近的偶數。
語法
以下是 remainder() 函式的語法 -
import math math.remainder(x, y)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式
引數
x, y: 數字運算元。Y 必須非零。
返回值
此函式返回 x/y 的餘數。
示例
以下示例顯示了 remainder() 函式的用法 -
from math import remainder x = 18 y= 4 rem = remainder(x, y) print ("x: ",x, "y:", y, "remainder: ", rem) x = 22 y= 4 rem = remainder(x, y) print ("x: ",x, "y:", y, "remainder: ", rem) x = 15 y= float("inf") rem = remainder(x, y) print ("x: ",x, "y:", y, "remainder: ", rem) x = 15 y= 0 rem = remainder(x, y) print ("x: ",x, "y:", y, "remainder: ", rem)
它將產生以下輸出 -
x: 18 y: 4 remainder: 2.0 x: 22 y: 4 remainder: -2.0 x: 15 y: inf remainder: 15.0 Traceback (most recent call last): File "C:\Users\mlath\examples\main.py", line 20, in <module> rem = remainder(x, y) ^^^^^^^^^^^^^^^ ValueError: math domain error
trunc() 函式
trunc() 函式返回數字的整數部分,去除小數部分。對於正x,trunc() 等效於 floor(),對於負x,等效於 ceil()。
語法
以下是 trunc() 函式的語法 -
import math math.trunc(x)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 數字運算元
返回值
此函式返回運算元的整數部分。
示例
以下示例顯示了 trunc() 函式的用法 -
from math import trunc x = 5.79 rem = trunc(x) print ("x: ",x, "truncated: ", rem) x = 12.04 rem = trunc(x) print ("x: ",x, "truncated: ", rem) x = -19.98 rem = trunc(x) print ("x: ",x, "truncated: ", rem) x = 15 rem = trunc(x) print ("x: ",x, "truncated: ", rem)
它將產生以下輸出 -
x: 5.79 truncated: 5 x: 12.04 truncated: 12 x: -19.98 truncated: -19 x: 15 truncated: 15
ulp() 函式
ULP 代表“最後一個位置的單位”。ulp() 函式返回浮點數 x 的最低有效位的數值。trunc() 對於正 x 等效於 floor(),對於負 x 等效於 ceil()。
不同場景 -
如果 x 是 NaN,則返回 x。
如果 x 是負數,則返回 ulp(-x)。
如果 x 是正無窮大,則返回 x。
如果 x 等於零,則返回最小的正非規格化可表示浮點數。
如果 x 等於最大的正可表示浮點數,則返回 x 的最低有效位的數值。
如果 x 是一個正的有限數,則返回 x 的最低有效位的數值,使得大於 x 的第一個浮點數為 x + ulp(x)。
語法
以下是 ulp() 函式的語法 -
import math math.ulp(x)
注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。
引數
x - 數字運算元
返回值
此函式返回浮點數x的最低有效位。
示例
以下示例顯示了 ulp() 函式的使用 -
from math import ulp x = float('nan') rem = ulp(x) print ("x: ",x, "ULP: ", rem) x = 0.0 rem = ulp(x) print ("x: ",x, "ULP: ", rem) x = -10 rem = ulp(x) print ("x: ",x, "ULP: ", rem) x = 15 rem = ulp(x) print ("x: ",x, "ULP: ", rem)
它將產生以下輸出 -
x: nan ULP: nan x: 0.0 ULP: 5e-324 x: -10 ULP: 1.7763568394002505e-15 x: 15 ULP: 1.7763568394002505e-15