Skip to main content
Numeric operators and mathematical functions for computing over user and event properties. Unless noted otherwise, each function works on both the PostgreSQL and ClickHouse backends.

% / mod

Modulo: the remainder left after dividing one number by another. % is the operator form and mod is the equivalent function. Syntax
Arguments
  • a — the dividend.
  • b — the divisor.
Returned value
  • The remainder of a divided by b. The result takes the sign of the dividend a (truncated division). Dividing by zero raises an error.
Example
Bucket users into 10 groups by a numeric id

abs

Returns the absolute (non-negative) value of a number. Syntax
Arguments
  • x — a numeric value.
Returned value
  • The absolute value of x, in the same numeric domain as the input.
Example
Distance of lifetime value from a target

floor / ceil / round / trunc

Rounding functions. floor rounds down, ceil rounds up, round rounds to the nearest value (ties away from zero), and trunc truncates toward zero. round and trunc optionally take a number of decimal places. Syntax
Arguments
  • x — a numeric value.
  • n — (optional, for round and trunc) the number of decimal places to keep. Defaults to 0.
Returned value
  • The rounded value of x. floor returns the largest integer not greater than x; ceil the smallest integer not less than x; round the nearest value at n decimals; trunc the value with digits beyond n decimals dropped.
Example
Rounding a value four ways

greatest / least

Return the largest or smallest value among their arguments. Syntax
Arguments
  • x1, x2, ... — two or more comparable values.
Returned value
  • greatest: the maximum of the arguments. least: the minimum of the arguments.
Example
Clamp lifetime value to at least 0

power

Raises a base to an exponent. Syntax
Arguments
  • a — the base.
  • b — the exponent.
Returned value
  • a raised to the power b, as a floating-point number.
Example
Two raised to the tenth power

sqrt / cbrt

sqrt returns the square root and cbrt the cube root of a number. Syntax
Arguments
  • x — a numeric value. For sqrt, x must be non-negative.
Returned value
  • The square root (sqrt) or cube root (cbrt) of x, as a floating-point number.
Example
Square root and cube root

sign

Returns the sign of a number. Syntax
Arguments
  • x — a numeric value.
Returned value
  • -1 for negative, 0 for zero, and 1 for positive x.
Example
Sign of a value

negate

Returns the argument with its sign flipped, equivalent to the unary minus. Syntax
Arguments
  • x — a numeric value.
Returned value
  • The value -x, always signed.
Example
Negate a number

max2 / min2

Return the larger or smaller of exactly two numbers. Syntax
Arguments
  • a, b — numeric values.
Returned value
  • max2: the greater of a and b. min2: the lesser of a and b. Returned as a floating-point number.
Example
Larger and smaller of two numbers

moduloOrZero

Like mod, but returns 0 instead of raising an error when the divisor is zero. Syntax
Arguments
  • a — the dividend.
  • b — the divisor.
Returned value
  • The remainder of a divided by b, or 0 when b is 0.
Example
Safe modulo

positiveModulo

Returns the remainder of a division, always as a non-negative number. Syntax
Arguments
  • a — the dividend.
  • b — the divisor.
Returned value
  • The non-negative remainder of a divided by b. For example, positiveModulo(-7, 3) is 2 rather than -1.
Example
Always-positive remainder

intDiv / intDivOrZero

Integer division. intDiv returns the whole-number quotient; intDivOrZero behaves the same but yields 0 on division by zero (or when dividing the minimum negative value by -1) instead of raising an error. Syntax
Arguments
  • a — the dividend.
  • b — the divisor.
Returned value
  • The integer part of a divided by b. intDiv raises an error when b is 0; intDivOrZero returns 0 in that case.
Example
Integer division

hypot

Returns the length of the hypotenuse of a right triangle given the two other sides, that is, the Euclidean distance from the origin. Syntax
Arguments
  • a, b — numeric values representing the two legs.
Returned value
  • The square root of a² + b², as a floating-point number.
Example
Hypotenuse of a 3-4-5 triangle

exp / exp2 / exp10

Exponential functions. exp uses base e, exp2 uses base 2, and exp10 uses base 10. Syntax
Arguments
  • x — the exponent, a numeric value.
Returned value
  • exp: e raised to x. exp2: 2 raised to x. exp10: 10 raised to x. All returned as floating-point numbers.
Example
Exponentials of a value

ln / log10 / log / log2 / log1p

Logarithms. ln is the natural logarithm (base e); log10 and log are both base-10; log2 is base-2; log1p computes ln(1 + x) with extra precision near zero. Syntax
Arguments
  • x — a positive numeric value (for log1p, x must be greater than -1).
Returned value
  • The logarithm of x in the corresponding base, as a floating-point number. Note that log(x) is base-10 (matching PostgreSQL), not the natural logarithm.
Example
Logarithms of a value

sin / cos / tan

Trigonometric functions taking an angle in radians. Syntax
Arguments
  • x — an angle in radians.
Returned value
  • The sine, cosine, or tangent of x, as a floating-point number.
Example
Trig functions

asin / acos / atan

Inverse trigonometric functions, returning an angle in radians. Syntax
Arguments
  • x — a numeric value. For asin and acos, x must be in the range −1 to 1.
Returned value
  • The arcsine, arccosine, or arctangent of x, in radians, as a floating-point number.
Example
Inverse trig functions

atan2

Returns the angle, in radians, between the positive x-axis and the point (x, y), using the signs of both arguments to determine the correct quadrant. Syntax
Arguments
  • y — the ordinate (y-coordinate).
  • x — the abscissa (x-coordinate).
Returned value
  • The angle of the point (x, y) in radians, as a floating-point number.
Example
Angle of a point

sinh / cosh / tanh

Hyperbolic functions. Syntax
Arguments
  • x — a numeric value.
Returned value
  • The hyperbolic sine, cosine, or tangent of x, as a floating-point number.
Example
Hyperbolic functions

asinh / acosh / atanh

Inverse hyperbolic functions. Syntax
Arguments
  • x — a numeric value. acosh requires x ≥ 1; atanh requires x in the open interval between −1 and 1.
Returned value
  • The inverse hyperbolic sine, cosine, or tangent of x, as a floating-point number.
Example
Inverse hyperbolic functions

degrees / radians

Convert between radians and degrees. degrees converts radians to degrees; radians converts degrees to radians. Syntax
Arguments
  • x — an angle: in radians for degrees, in degrees for radians.
Returned value
  • The converted angle, as a floating-point number.
Example
Convert between radians and degrees

pi

Returns the mathematical constant π. Syntax
Returned value
  • The value of π (approximately 3.14159), as a floating-point number.
Example
Value of pi

e

Returns Euler’s number, the base of the natural logarithm. Syntax
Returned value
  • The value of e (approximately 2.71828), as a floating-point number.
Example
Value of e

factorial

Returns the factorial of a non-negative integer. Syntax
Arguments
  • n — a non-negative integer.
Returned value
  • n!, the product of all positive integers up to n (with factorial(0) equal to 1).
Example
Factorial of 5

gcd / lcm

gcd returns the greatest common divisor and lcm the least common multiple of two integers. Syntax
Arguments
  • a, b — integer values.
Returned value
  • gcd: the largest integer that divides both a and b. lcm: the smallest positive integer that is a multiple of both a and b.
Example
GCD and LCM of two integers

intExp2 / intExp10

Integer exponentials. intExp2 returns 2 raised to x and intExp10 returns 10 raised to x, both as integers. Syntax
Arguments
  • x — the exponent, a non-negative integer.
Returned value
  • intExp2: 2 raised to x as an integer. intExp10: 10 raised to x as an integer.
Example
Integer powers of 2 and 10

roundBankers

Rounds a number to the nearest value, breaking ties by rounding to the nearest even digit (banker’s rounding). This avoids the upward bias of always rounding halves away from zero. Syntax
Arguments
  • x — a numeric value.
  • n — (optional) the number of decimal places to keep. Defaults to 0.
Returned value
  • x rounded to n decimals, with halfway cases rounded to the nearest even digit. For example, roundBankers(2.5) is 2 and roundBankers(3.5) is 4.
Example
Banker's rounding of halves

width_bucket

Returns the index of the histogram bucket that a value falls into, given an equal-width partitioning of a range into a number of buckets. Syntax
Arguments
  • operand — the value to bucket.
  • low — the lower bound of the range.
  • high — the upper bound of the range.
  • count — the number of equal-width buckets to divide the range into.
Returned value
  • An integer bucket index from 1 to count for values within the range; 0 for values below low, and count + 1 for values at or above high.
Example
Bucket lifetime value into 4 bands from 0 to 1000

random

Returns a pseudo-random floating-point number. Syntax
Returned value
  • A pseudo-random floating-point value in the range from 0.0 (inclusive) to 1.0 (exclusive).
Example
A random 10% sample of users

isFinite / isInfinite / isNaN

Test the classification of a floating-point number. Syntax
Arguments
  • x — a floating-point value.
Returned value
  • 1 if the condition holds, otherwise 0. isFinite is true for ordinary finite numbers; isInfinite is true for positive or negative infinity; isNaN is true for “not a number”.
Example
Classify a computed value

ifNotFinite

Returns a value if it is finite, otherwise returns a fallback. Useful for replacing infinities and NaN results from a computation. Syntax
Arguments
  • x — the value to check.
  • alt — the fallback returned when x is not finite (infinity or NaN).
Returned value
  • x when x is finite, otherwise alt.
Example
Guard against a non-finite ratio

erf / erfc

The Gauss error function and its complement. erf(x) gives the probability that a normally distributed variable falls within x standard deviations of the mean (scaled); erfc(x) is 1 - erf(x), computed with better precision for large x. Syntax
Arguments
  • x — a numeric value.
Returned value
  • erf: the error function of x. erfc: the complementary error function of x. Both are floating-point numbers.
Example
Error function and its complement

lgamma / tgamma

Gamma functions, available on ClickHouse only. tgamma(x) is the gamma function Γ(x), which generalizes the factorial (Γ(n) equals (n−1)! for positive integers). lgamma(x) is the natural logarithm of the absolute value of Γ(x), useful when Γ(x) would overflow. Syntax
Arguments
  • x — a numeric value.
Returned value
  • tgamma: the gamma function Γ(x). lgamma: the natural log of the absolute value of Γ(x). Both are floating-point numbers.
Example
Gamma function and log-gamma