PostgreSQL 函數 - 數學函數和操作符
在計算機科學裏面數學是一個很重要的組成部分,幾乎所有算法都離不開數學的影子。PostgreSQL 提供了各種幾何空間的數學函數和操作符。
我們知道基本的數學操作符有加、減、乘、除、否、取模、指數、開方(|/)、立方(||/)、絕對值(@)、按位與 (&)、按位或 (|)、按位異或(#)、按位求反(~)、按位左移(<<)、按位右移(>>)。這裏講一下位操作符(這裏都是二進制運算)。
91 & 15 → 11
1011011
0001111
--------
0001011
32 | 3 → 35
100000
000011
--------
100011
17 # 5 → 20
10001
00101
--------
10100
~1 → -2
~1的值爲1111111111111110,負數的計算機碼爲首位不變正數取反加1,即1000000000000010
使一個數的最低位爲零,可以表示爲:a&~1。按“與”運算,最低位一定爲0。因爲“~”運算符的優先級比算術運算符、關係運算符、邏輯運算符和其他運算符都高。
1 << 4 → 16
2^4=16
8 >> 2 → 2
8/(2^2)=4
後面附上數學函數 3 大類:
1. 常用數學函數
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
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 ; 適用於 integer,bigint, 和 numeric
gcd(1071, 462) → 21
lcm (numeric_type, numeric_type) → numeric_type
最小公倍數 (兩個輸入的整數倍的最小的嚴格正數);如果任意一個輸入值爲零則返回 0;適用於 integer,bigint,和 numeric
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.0000000000
min_scale (numeric) → integer
精確表示所提供值所需的最小刻度(小數位數)
min_scale(8.4100) → 2
mod (y numeric_type, x numeric_type) → numeric_type
y/x 的餘數;適用於 smallint、integer、bigint、和 numeric
mod(9,4) → 1
pi ( ) → double precision
π的近似值
pi() → 3.141592653589793
power (a numeric, b numeric) → numeric
power (a double precision, b double precision) → doubleprecision
a 的 b 次冪
power(9, 3) → 729
radians (double precision) → double precision
將角度轉換爲弧度
radians(45.0) → 0.7853981633974483
round (numeric) → numeric
round (double precision) → double precision
四捨五入到最近的整數
round(42.4) → 42
round (v numeric, s integer) → numeric
把 v 四捨五入到 s 位小數
round(42.4382, 2) → 42.44
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 doubleprecision, high double precision, count integer) → integer
返回包含 count 等寬柱的柱狀圖中 operand 所在的柱的編號,範圍從 low 到 high。超出該範圍的輸入則返回 0 或計數 + 1。
width_bucket(5.35, 0.024, 10.06, 5) → 3
width_bucket (operand anyelement, thresholds anyarray) →integer
返回一個柱號,這個柱是在給定數組中 operand 將被分配的柱。對於一個低於第一個下界的輸入返回 0。operand 和數組元素可以是具有標準比較操作符的任何類型。thresholds 數組必須被排好序,最小的排在最前面,否則將會得到意想不到的結果。
width_bucket(now(),array['yesterday','today','tomorrow']::timestamptz[]) → 2
2. 隨機函數
random ( ) → double precision
返回一個範圍 0.0 <= x < 1.0 中的隨機值
random() → 0.897124072839091
setseed (double precision) → void
爲後續的 random() 調用設置種子;參數必須在 - 1.0 和 1.0 之間,包括邊界值
setseed(0.12345)
random() 函數使用了一個簡單的線性共軛算法。它的速度很快,但不適合於密碼學應用;關於更安全的替代方案,請參閱 pgcrypto 模塊。如果 setseed() 被調用,生成的序列是相同的。
3. 三角函數
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) → doubleprecision
y/x 的反正切,結果爲弧度
atan2(1,0) → 1.5707963267948966
atan2d (y double precision, x double precision) → doubleprecision
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
4. 雙曲函數
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
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/ZZQdiv_IVcD_ZR1Ge1HjHg