支援的版本:目前 (17) / 16 / 15 / 14 / 13
開發版本:devel
不支援的版本:12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1

9.3. 數學函式與運算子 #

數學運算子可用於許多 PostgreSQL 類型。對於沒有標準數學慣例的類型(例如,日期/時間類型),我們將在後續章節中描述實際行為。

表格 9.4 顯示了標準數值類型可用的數學運算子。除非另有說明,否則顯示為接受 numeric_type 的運算子可用於所有類型 smallintintegerbigintnumericrealdouble precision。顯示為接受 integral_type 的運算子可用於類型 smallintintegerbigint。除非另有說明,否則每個運算子的形式都傳回與其參數相同的資料類型。涉及多個參數資料類型的呼叫(例如 integer + numeric)會使用這些列表中稍後出現的類型來解析。

表格 9.4. 數學運算子

運算子

描述

範例

numeric_type + numeric_typenumeric_type

加法

2 + 35

+ numeric_typenumeric_type

一元正號(無運算)

+ 3.53.5

numeric_type - numeric_typenumeric_type

減法

2 - 3-1

- numeric_typenumeric_type

負號

- (-4)4

numeric_type * numeric_typenumeric_type

乘法

2 * 36

numeric_type / numeric_typenumeric_type

除法(對於整數類型,除法將結果截斷為零)

5.0 / 22.5000000000000000

5 / 22

(-5) / 2-2

numeric_type % numeric_typenumeric_type

模數(餘數);可用於 smallintintegerbigintnumeric

5 % 41

numeric ^ numericnumeric

double precision ^ double precisiondouble precision

指數運算

2 ^ 38

與典型的數學實踐不同,預設情況下,多次使用 ^ 將從左到右關聯

2 ^ 3 ^ 3512

2 ^ (3 ^ 3)134217728

|/ double precisiondouble precision

平方根

|/ 25.05

||/ double precisiondouble precision

立方根

||/ 64.04

@ numeric_typenumeric_type

絕對值

@ -5.05.0

integral_type & integral_typeintegral_type

位元 AND

91 & 1511

integral_type | integral_typeintegral_type

位元 OR

32 | 335

integral_type # integral_typeintegral_type

位元互斥 OR (XOR)

17 # 520

~ integral_typeintegral_type

位元 NOT

~1-2

integral_type << integerintegral_type

位元左移

1 << 416

integral_type >> integerintegral_type

位元右移

8 >> 22


表 9.5 顯示可用的數學函數。這些函數中的許多都以多種形式提供,並具有不同的引數型別。 除非另有說明,否則給定形式的函數會傳回與其引數相同的資料型別;跨型別的情況以與上述運算符號說明相同的方式解決。 使用 double precision 資料的函數主要是在主機系統的 C 程式庫之上實現的;因此,邊界情況下的準確性和行為可能會因主機系統而異。

表 9.5. 數學函數

函數

描述

範例

abs ( numeric_type ) → numeric_type

絕對值

abs(-17.4)17.4

cbrt ( double precision ) → double precision

立方根

cbrt(64.0)4

ceil ( numeric ) → numeric

ceil ( double precision ) → double precision

大於或等於引數的最近整數

ceil(42.2)43

ceil(-42.8)-42

ceiling ( numeric ) → numeric

ceiling ( double precision ) → double precision

大於或等於引數的最近整數 (與 ceil 相同)

ceiling(95.3)96

degrees ( double precision ) → double precision

將弧度轉換為度

degrees(0.5)28.64788975654116

div ( y numeric, x numeric ) → numeric

y/x 的整數商數 (向零截斷)

div(9, 4)2

erf ( double precision ) → double precision

誤差函數

erf(1.0)0.8427007929497149

erfc ( double precision ) → double precision

互補誤差函數 (1 - erf(x),對於大型輸入,不會損失精度)

erfc(1.0)0.15729920705028513

exp ( numeric ) → numeric

exp ( double precision ) → double precision

指數 (e 提升到給定的冪)

exp(1.0)2.7182818284590452

factorial ( bigint ) → numeric

階乘

factorial(5)120

floor ( numeric ) → numeric

floor ( double precision ) → double precision

小於或等於引數的最近整數

floor(42.8)42

floor(-42.8)-43

gcd ( numeric_type, numeric_type ) → numeric_type

最大公因數 (可將兩個輸入除盡而沒有餘數的最大正數);如果兩個輸入都為零,則傳回 0;適用於 integerbigintnumeric

gcd(1071, 462)21

lcm ( numeric_type, numeric_type ) → numeric_type

最小公倍數 (嚴格大於零,且為兩輸入值的整數倍數的最小數字);如果任一輸入值為零,則傳回 0;適用於 integerbigintnumeric

lcm(1071, 462)23562

ln ( numeric ) → numeric

ln ( double precision ) → double precision

自然對數

ln(2.0)0.6931471805599453

log ( numeric ) → numeric

log ( double precision ) → double precision

以 10 為底的對數

log(100)2

log10 ( numeric ) → numeric

log10 ( double precision ) → double precision

以 10 為底的對數 (與 log 相同)

log10(1000)3

log ( b numeric, x numeric ) → numeric

b 為底的 x 的對數

log(2.0, 64.0)6.0000000000000000

min_scale ( numeric ) → integer

精確表示所提供的值所需的最小 scale (小數位數)

min_scale(8.4100)2

mod ( y numeric_type, x numeric_type ) → numeric_type

y/x 的餘數;適用於 smallintintegerbigintnumeric

mod(9, 4)1

pi ( ) → double precision

π 的近似值

pi()3.141592653589793

power ( a numeric, b numeric ) → numeric

power ( a double precision, b double precision ) → double precision

ab 次方

power(9, 3)729

radians ( double precision ) → double precision

將角度轉換為弧度

radians(45.0)0.7853981633974483

round ( numeric ) → numeric

round ( double precision ) → double precision

四捨五入到最接近的整數。 對於 numeric,捨入方式為遠離零。 對於 double precision,捨入行為取決於平台,但 捨入到最接近的偶數 是最常見的規則。

round(42.4)42

round ( v numeric, s integer ) → numeric

v 四捨五入到 s 個小數位。 捨入方式為遠離零。

round(42.4382, 2)42.44

round(1234.56, -1)1230

scale ( numeric ) → integer

引數的 scale (小數部分的位數)

scale(8.4100)4

sign ( numeric ) → numeric

sign ( double precision ) → double precision

引數的符號 (-1、0 或 +1)

sign(-8.4)-1

sqrt ( numeric ) → numeric

sqrt ( double precision ) → double precision

平方根

sqrt(2)1.4142135623730951

trim_scale ( numeric ) → numeric

透過移除尾隨零來減少值的 scale (小數位數)

trim_scale(8.4100)8.41

trunc ( numeric ) → numeric

trunc ( double precision ) → double precision

截斷為整數 (朝向零)

trunc(42.8)42

trunc(-42.8)-42

trunc ( v numeric, s integer ) → numeric

v 截斷到 s 個小數位

trunc(42.4382, 2)42.43

width_bucket ( operand numeric, low numeric, high numeric, count integer ) → integer

width_bucket ( operand double precision, low double precision, high double precision, count integer ) → integer

傳回 operand 落在哪一個儲存桶 (bucket) 的編號。此儲存桶屬於一個直方圖 (histogram),該直方圖具有 count 個等寬儲存桶,範圍從 lowhigh。若輸入值超出該範圍,則傳回 0count+1

width_bucket(5.35, 0.024, 10.06, 5)3

width_bucket ( operand anycompatible, thresholds anycompatiblearray ) → integer

傳回 operand 落在哪一個儲存桶的編號,給定一個陣列,其中列出儲存桶的下限。若輸入值小於第一個下限,則傳回 0operand 和陣列元素可以是任何具有標準比較運算子的類型。 thresholds 陣列必須經過排序,從小到大排列,否則會得到意想不到的結果。

width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[])2


表 9.6 顯示了用於產生隨機數的函式。

表 9.6. 隨機函式

函數

描述

範例

random ( ) → double precision

傳回範圍在 0.0 <= x < 1.0 的隨機值

random()0.897124072839091

random ( min integer, max integer ) → integer

random ( min bigint, max bigint ) → bigint

random ( min numeric, max numeric ) → numeric

傳回範圍在 min <= x <= max 的隨機值。對於 numeric 類型,結果將具有與 minmax 相同的小數位數,以較多者為準。

random(1, 10)7

random(-0.499, 0.499)0.347

random_normal ( [ mean double precision [, stddev double precision ]] ) → double precision

從具有給定參數的常態分布傳回一個隨機值;mean 預設為 0.0,而 stddev 預設為 1.0

random_normal(0.0, 1.0)0.051285419

setseed ( double precision ) → void

設定後續 random()random_normal() 呼叫的種子;參數必須介於 -1.0 和 1.0 之間,包含 -1.0 和 1.0

setseed(0.12345)


表 9.6 中列出的 random()random_normal() 函式使用確定性的假隨機數產生器。它速度很快,但不適用於密碼學應用;有關更安全的替代方案,請參閱 pgcrypto 模組。 如果呼叫了 setseed(),則可以透過使用相同的引數重新發出 setseed(),來重複目前會話中後續呼叫這些函式的結果序列。 如果在同一個會話中沒有任何先前的 setseed() 呼叫,則對任何這些函式的第一次呼叫會從平台相依的隨機位元來源取得一個種子。

表 9.7 顯示了可用的三角函數。 這些函式每個都有兩種變體,一種以弧度測量角度,另一種以度數測量角度。

表 9.7. 三角函數

函數

描述

範例

acos ( double precision ) → double precision

反餘弦,結果以弧度表示

acos(1)0

acosd ( double precision ) → double precision

反餘弦,結果以度數表示

acosd(0.5)60

asin ( double precision ) → double precision

反正弦,結果以弧度表示

asin(1)1.5707963267948966

asind ( double precision ) → double precision

反正弦,結果以度數表示

asind(0.5)30

atan ( double precision ) → double precision

反正切,結果以弧度表示

atan(1)0.7853981633974483

atand ( double precision ) → double precision

反正切,結果以度數表示

atand(1)45

atan2 ( y double precision, x double precision ) → double precision

y/x 的反正切函數,結果以弧度表示

atan2(1, 0)1.5707963267948966

atan2d ( y double precision, x double precision ) → double precision

y/x 的反正切函數,結果以角度表示

atan2d(1, 0)90

cos ( double precision ) → double precision

餘弦函數,參數以弧度表示

cos(0)1

cosd ( double precision ) → double precision

餘弦函數,參數以角度表示

cosd(60)0.5

cot ( double precision ) → double precision

餘切函數,參數以弧度表示

cot(0.5)1.830487721712452

cotd ( double precision ) → double precision

餘切函數,參數以角度表示

cotd(45)1

sin ( double precision ) → double precision

正弦函數,參數以弧度表示

sin(1)0.8414709848078965

sind ( double precision ) → double precision

正弦函數,參數以角度表示

sind(30)0.5

tan ( double precision ) → double precision

正切函數,參數以弧度表示

tan(1)1.5574077246549023

tand ( double precision ) → double precision

正切函數,參數以角度表示

tand(45)1


注意

另一種使用角度 (以度為單位) 的方式是使用之前顯示的單位轉換函數 radians()degrees()。 但是,最好使用基於角度的三角函數,因為這樣可以避免特殊情況 (例如 sind(30)) 的捨入誤差。

表 9.8 顯示可用的雙曲函數。

表 9.8. 雙曲函數

函數

描述

範例

sinh ( double precision ) → double precision

雙曲正弦函數

sinh(1)1.1752011936438014

cosh ( double precision ) → double precision

雙曲餘弦函數

cosh(0)1

tanh ( double precision ) → double precision

雙曲正切函數

tanh(1)0.7615941559557649

asinh ( double precision ) → double precision

反雙曲正弦函數

asinh(1)0.881373587019543

acosh ( double precision ) → double precision

反雙曲餘弦函數

acosh(1)0

atanh ( double precision ) → double precision

反雙曲正切函數

atanh(0.5)0.5493061443340548


提交更正

如果您在文件中發現任何不正確、與特定功能不符或需要進一步澄清的地方,請使用此表格回報文件問題。