溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

postgresql——數(shù)學(xué)函數(shù)介紹

發(fā)布時間:2020-08-10 05:22:06 來源:網(wǎng)絡(luò) 閱讀:1859 作者:一個笨小孩 欄目:數(shù)據(jù)庫

1、數(shù)學(xué)函數(shù)

   數(shù)學(xué)函數(shù)主要用來處理數(shù)值數(shù)據(jù),主要的數(shù)學(xué)函數(shù)有:絕對值函數(shù)、三角函數(shù)(包括正弦函數(shù)、正切函數(shù)、余切函數(shù)等)、對數(shù)函數(shù)、隨機(jī)數(shù)函數(shù)等。在有錯誤產(chǎn)生時,數(shù)學(xué)函數(shù)會返回空值null。


1.1、絕對值函數(shù)ABS(x)和返回圓周率的函數(shù)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、平方根函數(shù)sqrt(x)和求余函數(shù)mod(x,y)

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

mod(x,y)返回x被y除后的余數(shù)。mod()對于帶有小數(shù)部分的數(shù)值也起作用,返回除法運(yùn)算后的精確余數(shù)。


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

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

 sqrt |       sqrt       

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

    3 | 6.32455532033676

(1 row)


注意:負(fù)數(shù)沒有平方根,如果所求值為負(fù)數(shù),將會提示錯誤信息。


例子:進(jìn)行求余運(yùn)算:

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

 mod | mod | mod 

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

   7 |   4 | 3.5

(1 row)



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

ceil(x)和ceiling(x)意義相同,返回不小于x的最小整數(shù)值,返回值轉(zhuǎn)化為一個bigint。


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

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

 ceil | ceiling 

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

   -3 |       4

(1 row)


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

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

 floor | floor 

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

    -4 |     3

(1 row)


1.4、四舍五入函數(shù)round(x)和round(x,y)

round(x)返回最接近于參數(shù)x的整數(shù),對x值進(jìn)行四舍五入。

round(x,y)返回最接近于參數(shù)x的數(shù),其值保留到小數(shù)點(diǎn)后面y位,若y為負(fù)值,則將保留x值到小數(shù)點(diǎn)左邊y位。


例子:使用round(x)函數(shù)對操作數(shù)進(jìn)行四舍五入,如:

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)函數(shù)對操作數(shù)進(jìn)行四舍五入,如

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、符號函數(shù)sign(x)

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


例子:

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

 sign | sign | sign 

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

   -1 |    0 |    1

(1 row)


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

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

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


例子:使用pow,power函數(shù)進(jìn)行乘方運(yùn)算,如:

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、對數(shù)運(yùn)算函數(shù):log(x)

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

testdb=# select log(3);

        log        

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

 0.477121254719662

(1 row)


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

radians(x)將參數(shù)x由角度轉(zhuǎn)化為弧度。

如:

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

     radians     |     radians      

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

 1.5707963267949 | 3.14159265358979

(1 row)


degrees(x)將參數(shù)x由弧度轉(zhuǎn)換為角度,如:

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

 degrees | degrees 

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

     180 |      90

(1 row)


1.9、正弦函數(shù):sin(x)和反正弦函數(shù):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的范圍內(nèi),則會彈出錯誤信息:輸入超出范圍。


1.10、余弦函數(shù):cos(x)和反余弦函數(shù):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的范圍之內(nèi),則會彈出錯誤信息。

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

 acos |      acos       | round 

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

    0 | 1.5707963267949 |     1

(1 row)



1.11、正切函數(shù):tan(x),反正切函數(shù):atan(x),余切函數(shù):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)


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI