postgresql common functions - mathematical functions

Introduction

Function ( function ) is some predefined code modules that can calculate and process the input, and finally output a
result value. PostgreSQL functions can be divided into two categories: scalar function ( scalar function ) and aggregation function ( aggregation
function ). Scalar functions return a result for each input, and aggregate functions summarize a result for a set of inputs.
Common
system functions fall into the following
categories: mathematical functions, character functions, datetime functions, and type conversion functions
. In addition to using these built-in
functions, PostgreSQL also supports creating custom functions ( UDF )

math function

arithmetic operator

PostgreSQL supports the following arithmetic operators:
insert image description here
insert image description here

absolute value

-- abs(x)函数用于计算 x 的绝对值
select abs(-17.4);

insert image description here

rounding function

ceil(dp)/ceiling(dp)function to calculate dpthe smallest integer greater than or equal to ; floor(dp)function to calculate the largest integer less than or
equal to dp; round(dp)function to round to an integer; trunc(dp)function to round toward zero

select ceil(-42.8), floor(-42.8), round(12.45), trunc(12.8);

insert image description here
Also, round(dp, s)the function rounds to s decimal places; trunc(dp, s)the function truncates to ss decimal places.

square root

power(a, b)Function Compute ato bthe power; sqrt(dp)Function Compute dpsquare root; cbrt(dp)Function Compute dpcube root
insert image description here

Exponential and logarithmic

exp(dp)The function calculates the exponent with the natural constant eas the base, the ln(dp) function calculates the logarithm with the natural constant eas the base,
log(dp)/log10(dp)the function calculates the logarithm 10with the base as the logarithm, and log(b, x)the function calculates the logarithm with the base b as the logarithm.
insert image description here

integer quotient and remainder

div(y, x)The function calculates the integer quotient y divided by , and the function calculates the remainder when divided by .xmod(y, x) yx
insert image description here

Radians and Angles

degrees(dp)The function is used to convert radians to degrees, and radians(dp)the function is used to convert angles to radians.
insert image description here

constant π

select pi();

insert image description here

symbolic function

select sign(-9.7); 

The sign(dp) function returns the sign of the argument, and the possible results are -1, 0, +1.
insert image description here

Generate random numbers

PostgreSQL provides a function random() that returns a random number.
random() returns a random number greater than or equal to 0 and less than 1, and the type is a double-precision floating-point number.

select random();  

insert image description here
setseed(dp)The function can set the seed number for the subsequent random() function, range: -1.0 <= dp <= 1.0.

select setseed(0);
select random(); 

The same seed yields the same random number for reproducible results.

Guess you like

Origin blog.csdn.net/Java_Fly1/article/details/132461892