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)


猜你喜欢

转载自blog.51cto.com/fengfeng688/2151136