postgresql——數學函數介紹

1、數學函數

   數學函數主要用來處理數值數據,主要的數學函數有:絕對值函數、三角函數(包括正弦函數、正切函數、餘切函數等)、對數函數、隨機數函數等。在有錯誤產生時,數學函數會返回空值null。


1.1、絕對值函數ABS(x)和返回圓周率的函數PI()


例子:求2,-3.3,-33的絕對值

testdb=# select abs(2),abs(-3.3),abs(-33);

 abs | abs | abs 

-----+-----+-----

   2 | 3.3 |  33

(1 row)


例子:返回圓周率值,如下:

testdb=# select pi();

        pi        

------------------

 3.14159265358979

(1 row)


1.2、平方根函數sqrt(x)和求餘函數mod(x,y)

sqrt(x)返回非負數x的二次平方根

mod(x,y)返回x被y除後的餘數。mod()對於帶有小數部分的數值也起作用,返回除法運算後的精確餘數。


例子:求9和40的二次平方根:

testdb=# select sqrt(9),sqrt(40);

 sqrt |       sqrt       

------+------------------

    3 | 6.32455532033676

(1 row)


注意:負數沒有平方根,如果所求值爲負數,將會提示錯誤信息。


例子:進行求餘運算:

testdb=# select mod(31,8),mod(234,10),mod(45.5,6);

 mod | mod | mod 

-----+-----+-----

   7 |   4 | 3.5

(1 row)



1.3、獲取整數的函數ceil(x),ceiling(x)和floor(x)

ceil(x)和ceiling(x)意義相同,返回不小於x的最小整數值,返回值轉化爲一個bigint。


例子:使用ceil和ceiling函數返回最小整數,如下:

testdb=# select ceil(-3.35),ceiling(3.35);

 ceil | ceiling 

------+---------

   -3 |       4

(1 row)


例子:使用floor函數返回最大整數值,如下

testdb=# select floor(-3.35),floor(3.35);

 floor | floor 

-------+-------

    -4 |     3

(1 row)


1.4、四捨五入函數round(x)和round(x,y)

round(x)返回最接近於參數x的整數,對x值進行四捨五入。

round(x,y)返回最接近於參數x的數,其值保留到小數點後面y位,若y爲負值,則將保留x值到小數點左邊y位。


例子:使用round(x)函數對操作數進行四捨五入,如:

testdb=# select round(-1.15),round(-1.68),round(1.15),round(1.68);

 round | round | round | round 

-------+-------+-------+-------

    -1 |    -2 |     1 |     2

(1 row)


例子:使用round(x,y)函數對操作數進行四捨五入,如

testdb=# select round(1.38,1),round(1.38,0),round(231.36,-1),round(231.36,-2);

 round | round | round | round 

-------+-------+-------+-------

   1.4 |     1 |   230 |   200

(1 row)


1.5、符號函數sign(x)

sign(x)返回參數的符號,x的值爲負、零或正時返回結果依次爲:-1,0或1.


例子:

testdb=# select sign(-21),sign(0),sign(21);

 sign | sign | sign 

------+------+------

   -1 |    0 |    1

(1 row)


1.6、冪運算函數pow(x,y),power(x,y)和exp(x)

pow(x,y),power(x,y)函數返回x的y次乘方的結果值;

exp(x)返回e的x乘方後的值;


例子:使用pow,power函數進行乘方運算,如:

testdb=# select pow(2,2),power(2,2),pow(2,-2),power(2,-2);

 pow | power | pow  | power 

-----+-------+------+-------

   4 |     4 | 0.25 |  0.25

(1 row)


例子:使用exp(x)返回e的x乘方後的值

testdb=# select exp(3),exp(-3),exp(0);

       exp        |        exp         | exp 

------------------+--------------------+-----

 20.0855369231877 | 0.0497870683678639 |   1

(1 row)


1.7、對數運算函數:log(x)

log(x)返回x的自然數,x相對於基數e的對數。對數定義域不能爲負數,因此數組爲負數將會彈出錯誤信息。

testdb=# select log(3);

        log        

-------------------

 0.477121254719662

(1 row)


1.8、角度與弧度相互轉換的函數:radians(x)和degrees(x)

radians(x)將參數x由角度轉化爲弧度。

如:

testdb=# select radians(90),radians(180);

     radians     |     radians      

-----------------+------------------

 1.5707963267949 | 3.14159265358979

(1 row)


degrees(x)將參數x由弧度轉換爲角度,如:

testdb=# select degrees(pi()),degrees(pi()/2);

 degrees | degrees 

---------+---------

     180 |      90

(1 row)


1.9、正弦函數:sin(x)和反正弦函數:asin(x)

sin(x)返回x正弦,其中x爲弧度值。


testdb=# select sin(1),round(sin(pi()));

        sin        | round 

-------------------+-------

 0.841470984807897 |     0

(1 row)


asin(x)返回x的反正弦,即正弦爲x的值。若x不在-1到1的範圍內,則會彈出錯誤信息:輸入超出範圍。


1.10、餘弦函數:cos(x)和反餘弦函數:acos(x)

cos(x)返回x的餘弦,其中x爲弧度值。

testdb=# select cos(0),cos(pi()),cos(1);

 cos | cos |       cos        

-----+-----+------------------

   1 |  -1 | 0.54030230586814

(1 row)


acos(x)返回x的反餘弦值,即餘弦是x的值。若x不在-1到1的範圍之內,則會彈出錯誤信息。

testdb=# select acos(1),acos(0),round(acos(0.54030230586814));

 acos |      acos       | round 

------+-----------------+-------

    0 | 1.5707963267949 |     1

(1 row)



1.11、正切函數:tan(x),反正切函數:atan(x),餘切函數:cot(x)

tan(x)返回x的正切,其中x爲給定的弧度值。


例子:

testdb=# select tan(0.3),round(tan(pi()/4));

        tan        | round 

-------------------+-------

 0.309336249609623 |     1

(1 row)



atan(x)返回x的反正切,即正切爲x的值。


例子:

testdb=# select atan(0.309336249609623),atan(1);

 atan |       atan        

------+-------------------

  0.3 | 0.785398163397448

(1 row)


cot(x)返回x的餘切。


例子:

testdb=# select cot(0.3),1/tan(0.3),cot(pi()/4);

       cot        |     ?column?     | cot 

------------------+------------------+-----

 3.23272814376583 | 3.23272814376583 |   1

(1 row)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章