2025年9月25日: PostgreSQL 18 釋出!
支援的版本:當前 (18) / 17 / 16 / 15 / 14 / 13
開發版本:開發
不支援的版本: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

按位與

91 & 1511

integral_type | integral_typeintegral_type

按位或

32 | 335

integral_type # integral_typeintegral_type

按位異或

17 # 520

~ integral_typeintegral_type

按位非

~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

gamma ( double precision ) → double precision

Gamma 函式

gamma(0.5)1.772453850905516

gamma(6)120

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

lgamma ( double precision ) → double precision

Gamma 函式絕對值的自然對數

lgamma(1000)5905.220423209181

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

最小刻度(小數點後有效數字位數),能夠精確表示給定值所需的

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(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

透過移除尾隨零來減小值的刻度(小數點後有效數字位數)

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

在具有 count 個等寬儲存桶(跨越 lowhigh 的範圍)的直方圖中,返回 operand 所屬的儲存桶編號。儲存桶的下限是包含的,上限是不包含的。小於 low 的輸入返回 0,大於或等於 high 的輸入返回 count+1。如果 low > high,行為會反轉,儲存桶 1 現在是緊鄰 low 的那個,並且包含的邊界現在位於上側。

width_bucket(5.35, 0.024, 10.06, 5)3

width_bucket(9, 10, 0, 10)2

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(包含)和 1.0(不包含)之間的隨機值

random()0.897124072839091

random ( min integer, max integer ) → integer

random ( min bigint, max bigint ) → bigint

random ( min numeric, max numeric ) → numeric

返回一個介於 min(包含)和 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 之間(包含)

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


提交更正

如果您在文件中發現任何不正確之處、與您對特定功能的體驗不符或需要進一步澄清的內容,請使用此表單報告文件問題。