Mathematical functions and operators

更新时间:
复制 MD 格式

AnalyticDB for PostgreSQL supports the mathematical functions and operators of PostgreSQL. For the full PostgreSQL reference, see Mathematical Functions and Operators.

Mathematical operators

OperatorDescriptionExampleResult
+Addition2 + 35
-Subtraction2 - 3-1
*Multiplication2 * 36
/Division. For integer operands, truncates the result toward zero.4 / 22
%Modulo (remainder)5 % 41
^Exponentiation (left-associative)2.0 ^ 3.08
|/Square root|/ 25.05
||/Cube root||/ 27.03
!Factorial5 !120
!!Factorial as a prefix operator!! 5120
@Absolute value@ -5.05
&Bitwise AND91 & 1511
|Bitwise OR32 | 335
#Bitwise XOR17 # 520
~Bitwise NOT~1-2
<<Left shift (fills vacant bits with zeros)1 << 416
>>Right shift (fills vacant bits with zeros)8 >> 22

Mathematical functions

Rounding and truncation

FunctionReturn typeDescriptionExampleResult
abs(x)Same as inputAbsolute valueabs(-17.4)17.4
ceil(DOUBLE PRECISION or NUMERIC)Same as inputRounds up to the nearest integer greater than or equal to the inputceil(-42.8)-42
ceiling(DOUBLE PRECISION or NUMERIC)Same as inputSame as ceilceiling(-95.3)-95
floor(DOUBLE PRECISION or NUMERIC)Same as inputRounds down to the nearest integer less than or equal to the inputfloor(-42.8)-43
round(DOUBLE PRECISION or NUMERIC)Same as inputRounds to the nearest integerround(42.4)42
round(v NUMERIC, s INT)NUMERICRounds to s decimal placesround(42.4382, 2)42.44
trunc(DOUBLE PRECISION or NUMERIC)Same as inputTruncates toward zerotrunc(42.8)42
trunc(v NUMERIC, s INT)NUMERICTruncates to s decimal placestrunc(42.4382, 2)42.43
sign(DOUBLE PRECISION or NUMERIC)Same as inputReturns the sign of a number: -1 for negative, 0 for zero, +1 for positivesign(-8.4)-1
mod(y, x)Same as inputRemainder of y divided by xmod(9,4)1
div(y NUMERIC, x NUMERIC)NUMERICInteger quotient of y divided by xdiv(9,4)2
scale(NUMERIC)INTEGERNumber of decimal digits in the fractional partscale(8.41)2

Exponents and roots

FunctionReturn typeDescriptionExampleResult
cbrt(DOUBLE PRECISION)DOUBLE PRECISIONCube rootcbrt(27.0)3
exp(DOUBLE PRECISION or NUMERIC)Same as inputExponential (e^x)exp(1.0)2.71828182845905
power(a DOUBLE PRECISION, b DOUBLE PRECISION)DOUBLE PRECISIONa raised to the power of bpower(9.0, 3.0)729
power(a NUMERIC, b NUMERIC)NUMERICa raised to the power of bpower(9.0, 3.0)729
sqrt(DOUBLE PRECISION or NUMERIC)Same as inputSquare rootsqrt(2.0)1.4142135623731

Logarithmic

FunctionReturn typeDescriptionExampleResult
ln(DOUBLE PRECISION or NUMERIC)Same as inputNatural logarithmln(2.0)0.693147180559945
log(DOUBLE PRECISION or NUMERIC)Same as inputCommon (base-10) logarithmlog(100.0)2
log10(DOUBLE PRECISION or NUMERIC)Same as inputCommon (base-10) logarithm. Same as log.log10(100.0)2
log(b NUMERIC, x NUMERIC)NUMERICLogarithm of x to base blog(2.0, 64.0)6.0000000000

Angle conversion

FunctionReturn typeDescriptionExampleResult
degrees(DOUBLE PRECISION)DOUBLE PRECISIONConverts radians to degreesdegrees(0.5)28.6478897565412
pi()DOUBLE PRECISIONThe value of πpi()3.14159265358979
radians(DOUBLE PRECISION)DOUBLE PRECISIONConverts degrees to radiansradians(45.0)0.785398163397448

Histogram

FunctionReturn typeDescriptionExampleResult
width_bucket(operand DOUBLE PRECISION, b1 DOUBLE PRECISION, b2 DOUBLE PRECISION, count INT)INTReturns the bucket number for operand in a histogram of count equal-width buckets spanning b1 to b2. Returns 0 or count+1 for out-of-range values.width_bucket(5.35, 0.024, 10.06, 5)3
width_bucket(operand NUMERIC, b1 NUMERIC, b2 NUMERIC, count INT)INTSame as above with NUMERIC arguments.width_bucket(5.35, 0.024, 10.06, 5)3
width_bucket(operand anyelement, thresholds anyarray)INTReturns the bucket number based on an array of lower bounds. Returns 0 for values below the first bound. The thresholds array must be sorted in ascending order.width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[])2

Example: Group sales data into price buckets

CREATE TABLE home_sales (
    sale_date DATE,
    price NUMERIC(11, 2)
);

INSERT INTO home_sales (sale_date, price) VALUES
    ('2024-01-15', 290000.00),
    ('2024-02-20', 410000.00),
    ('2024-03-05', 180000.00),
    ('2024-04-12', 575000.00);

SELECT
    sale_date,
    price,
    width_bucket(price, 100000, 600000, 5) AS price_bucket
FROM home_sales
ORDER BY sale_date;

Result:

sale_datepriceprice_bucket
2024-01-15290000.002
2024-02-20410000.004
2024-03-05180000.001
2024-04-12575000.005

Usage notes for `width_bucket`:

  • For the range-based variants, values below b1 return 0, and values above b2 return count+1.

  • The thresholds array variant requires the array to be sorted in ascending order. Unsorted arrays produce undefined results.

Random functions

FunctionReturn typeDescription
random()DOUBLE PRECISIONReturns a random value in the range [0.0, 1.0)
setseed(dp)voidSets the seed for subsequent random() calls. Valid range: [-1.0, 1.0].

Trigonometric functions

Each function has two variants: one that accepts and returns radians, and one that accepts and returns degrees (the d-suffixed version).

Function (radians)Function (degrees)Description
acos(x)acosd(x)Arccosine
asin(x)asind(x)Arcsine
atan(x)atand(x)Arctangent
atan2(y, x)atan2d(y, x)Arctangent of y/x
cos(x)cosd(x)Cosine
cot(x)cotd(x)Cotangent
sin(x)sind(x)Sine
tan(x)tand(x)Tangent

Hyperbolic functions

FunctionDescriptionExampleResult
sinh(x)Hyperbolic sinesinh(0)0
cosh(x)Hyperbolic cosinecosh(0)1
tanh(x)Hyperbolic tangenttanh(0)0
asinh(x)Inverse hyperbolic sineasinh(0)0
acosh(x)Inverse hyperbolic cosineacosh(1)0
atanh(x)Inverse hyperbolic tangentatanh(0)0