Mathematics Functions

Mathematical functions and operators

These apply to MonetDB numeric SQL types: tinyint, smallint, int, bigint, hugeint, decimal, double, float and real. Some of these functions and operators also work on SQL types: interval month and interval second.

OperatorDescriptionExample queryResult
+additionSELECT 2 + 3;5
-subtractionSELECT 2 - 3;-1
*multiplicationSELECT 2 * 3;6
/division (integer division truncates the result)SELECT 5 / 2;2
/division (on decimals or floating point)SELECT 5.0 / 2;2.5
%modulo (remainder)SELECT 5 % 4;1
&bitwise AND (integers only)SELECT 91 & 15;11
|bitwise OR (integers only)SELECT 32 | 3;35
^bitwise XOR (integers only)SELECT 17 ^ 5;20
~bitwise NOT (integers only)SELECT ~1;-2
<<bitwise shift left (integers only)SELECT 1 << 4;16
>>bitwise shift right (integers only)SELECT 8 >> 2;2

Mathematical functions

FunctionReturn typeDescriptionExample queryResult
abs(x)same as inputabsolute value of a numberSELECT abs(-17.4);17.4
cbrt(x)doublecube rootSELECT cbrt(2.0);1.2599210498948734
ceil(x)doublenearest integer greater than or equal to argument (same as ceiling(x))SELECT ceil(-42.8);-42
ceiling(x)doublenearest integer greater than or equal to argument (same as ceil(x))SELECT ceiling(-95.3);-95
exp(x)doubleexponentialSELECT exp(1);2.718281828459045
floor(x)real or doublenearest integer less than or equal to argumentSELECT floor(-42.8);-43
get_value_for(schema_nm, seq_nm)bigintget current value of a sequenceSELECT get_value_for('dwh2', 'seq_9201');1234
greatest(x, y, ...)same as inputgreatest (highest) value of all the numeric arguments excluding nullSELECT greatest(4, 555.5, 7e2, null);700
least(x, y, ...)same as inputleast (lowest) value of all the numeric arguments excluding nullSELECT least(4, 555.5, 7e2, null);4
ln(x)doublenatural logarithmSELECT ln(2.0);0.6931471805599453
log(x)doublenatural logarithm, same as ln(x)SELECT log(2.0);0.6931471805599453
log(b, x)doublelogarithm to base b.SELECT log(2, 64.0);6.0
log10(x)doublebase 10 logarithmSELECT log10(100.0);2.0
log2(x)doublebase 2 logarithmSELECT log2(64.0);6.0
mod(x, y)same as inputmodulo (remainder)SELECT mod(5.0, 2.1);0.8
next_value_for(schema_nm, seq_nm)bigintget current value of a sequence and increment itSELECT next_value_for('dwh2', 'seq_9201');1234
nextafter(x, y)same as inputreturns the next representable floating-point value of x in the direction of y.SELECT nextafter(5.99999999999999, 12.1);6.0
power(x, y)doublex raised to the power of ySELECT power(2, 5);32
rand()intrandom integer value between 0 and 2147483648SELECT rand();917632440
rand(seed)intrandom integer value with seed setSELECT rand(-5);1345532277
round(x, d)same as first arground x to d decimal placesSELECT round(42.4382, 2);42.4400
sign(x)tinyintsign of the argument: -1 when x is negative, 0 when x is zero, 1 when x is positiveSELECT sign(-8.4);-1
sqrt(x)doublesquare rootSELECT sqrt(2.0);1.4142135623730951
sys.alpha(pdec double, pradius double)doublecompute alpha 'expansion' of theta for a given declination (used by SkyServer)SELECT sys.alpha(5.0, 1.2);1.2045844792437546
sys.ms_round(x double, prc int, trunc int)doubleround to prc decimal places and ?? truncate to trunc decimal placesSELECT sys.ms_round(1.2359, 2, 0);1.24
sys.ms_trunc(num double, prc int)doubletruncate to prc decimal placesSELECT sys.ms_trunc(1.2359, 2);1.23

Trigonometric and Hyperbolic functions

All these functions (except pi()) take argument(s) of type double.

FunctionReturn typeDescriptionExample queryResult
acos(x)doublearc cosine of angle x in radiansSELECT acos(0.54);1.0003592173949747
asin(x)doublearc sine of angle x in radiansSELECT asin(1.0);1.5707963267948966
atan(x)doublearc tangent of angle x in radiansSELECT atan(1.0);0.7853981633974483
atan(x, y)doublearc tangent of angle x/y in radiansSELECT atan(1.0, 2.0);0.4636476090008061
cos(x)doublecosine of angle x in radiansSELECT cos(12.2);0.9336336440746373
cosh(x)doublehyperbolic cosine of angle x in radiansSELECT cosh(3.0);10.067661995777765
cot(x)doublecotangent of angle x in radiansSELECT cot(16.0);3.326323195635449
degrees(rad)doubleconvert radians angle value to degrees: r*180/pi()SELECT degrees(0.5);28.64788975654116
pi()doubleπ constantSELECT pi();3.141592653589793
radians(degr)doubleconvert degrees angle value to radians: d*pi()/180SELECT radians(45);0.7853981633974483
sin(x)doublesine of angle x in radiansSELECT sin(1.4);0.9854497299884601
sinh(x)doublehyperbolic sine of angle x in radiansSELECT sinh(1.4);1.9043015014515339
tan(x)doubletangent of angle x in radiansSELECT tan(1.4);5.797883715482887
tanh(x)doublehyperbolic tangent of angle x in radiansSELECT tanh(1.4);0.8853516482022625