Mathematical functions
Perform advanced mathematical calculations.
Propel provides a comprehensive set of mathematical functions to perform various calculations and transformations.
These functions cover basic arithmetic operations, trigonometry, logarithms, rounding, and more advanced mathematical concepts.
ClickHouse function reference
e
Returns the mathematical constant e (Euler’s number).
Syntax:
Returns:
The value of e (approximately 2.71828…). [Float64
]
Example:
Result:
This function is useful in mathematical calculations involving exponential growth or decay, such as compound interest in taco sales projections or the rate of salsa consumption at a taco party.
The e()
function always returns the same value and does not take any arguments.
pi
Returns the mathematical constant π (pi).
Syntax:
Returns:
A Float64
value representing π (approximately 3.14159265358979323846).
Example:
Result:
This example demonstrates the use of pi()
to obtain the value of π and rounds it to 5 decimal places for easier reading.
The pi()
function is useful in various mathematical and geometric calculations, such as computing the area or circumference of a circle.
exp
Returns e (Euler’s constant) raised to the power of the given argument.
Syntax:
Arguments:
x
(numeric): The exponent. Can be any numeric type.
Returns:
- Type:
Float32
orFloat64
depending on the input type. - The value of e^x.
Example:
Result:
This example calculates e^2 and e^(-1), which are common values in mathematical and statistical calculations.
The exp
function is particularly useful in various mathematical and statistical computations, such as calculating compound interest, modeling population growth, or computing probabilities in logistic regression.
Example:
Result:
In this example, we use the exp
function to model the growth in popularity of different taco varieties. The growth_rate
represents the natural log of the popularity multiplier, so exp(growth_rate)
gives us the actual multiplier. We then use this to calculate the projected popularity based on the initial popularity and the growth rate.
log
Returns the natural logarithm of the argument.
Syntax:
Alias:
- ln(x)
Arguments:
x
(numeric): A number. Type:UInt*
,Int*
,Float*
, orDecimal*
.
Returns:
- The natural logarithm of
x
. Type:Float*
.
Example:
Result:
This example calculates the natural logarithm of e (Euler’s number), which is approximately 1.
- If
x
is negative or zero, the function will returnnan
. - For very small values of
x
close to 1, consider usinglog1p(x)
for better numerical accuracy.
Example:
Result:
In this example, we calculate the natural logarithm of taco prices. This could be useful for various statistical analyses or for creating a logarithmic scale of prices.
exp2
Returns 2 raised to the power of the given argument.
Syntax:
Arguments:
x
(numeric): The exponent. Can be any numeric type:UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
.
Returns:
- 2^x
- Type:
Float32
orFloat64
depending on the input type
Example:
Result:
This example calculates 2^3, which represents the number of possible taco combinations with 3 binary toppings (e.g., cheese, lettuce, salsa).
For integer results, consider using the intExp2
function instead.
intExp2
Calculates 2 raised to the power of the given argument.
Syntax:
Arguments:
x
(numeric): The exponent. Can be any numeric type.
Returns:
The result of 2^x as a UInt64
.
Example:
Result:
In this example, we calculate 2^3, which represents the number of possible taco combinations with 3 binary toppings (e.g., cheese, lettuce, salsa).
This function is similar to exp2
, but returns an integer result. It’s useful when you need an exact integer power of 2, such as for bit manipulations or when dealing with data sizes.
log2
Calculates the binary logarithm (base-2 logarithm) of the argument.
Syntax:
Arguments:
x
(numeric): A number to calculate the binary logarithm for. Can be any of these types:UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
.
Returns:
- The binary logarithm of
x
. - Type:
Float64
Example:
Result:
In this example:
log2(8)
returns 3 because 2³ = 8log2(0.5)
returns -1 because 2⁻¹ = 0.5
- If
x
is negative or zero, the function will returnnan
(Not a Number). - This function is particularly useful in computer science and information theory contexts, such as calculating the number of bits needed to represent a number or measuring information content.
exp10
Returns 10 raised to the power of the given argument.
Syntax:
Arguments:
x
(numeric): The exponent. Can be any numeric type:UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
.
Returns:
- 10 raised to the power of
x
.
Type: Float32
or Float64
, depending on the input type.
Example:
Result:
This example calculates 10^3 and 10^(-1), demonstrating both positive and negative exponents.
For integer results, consider using intExp10
instead, which returns a UInt64
.
intExp10
Returns 10 raised to the power of the given argument.
Syntax:
Arguments:
x
(numeric): The exponent. Can be any numeric type.
Returns:
The result of 10^x as a UInt64
.
Example:
Result:
In this example, we calculate 10^3, which represents the number of possible taco combinations if we had 10 choices for each of 3 taco components.
This function is similar to exp10
, but returns an integer result. It’s useful when you need an exact integer value for powers of 10, which is common in certain calculations or when dealing with large numbers.
log10
Calculates the base-10 logarithm of a number.
Syntax:
Arguments:
x
(numeric): The input value. Can be any numeric type:UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
.
Returns:
- The base-10 logarithm of
x
. Type:Float64
.
Example:
Result:
In this example:
log10(100)
returns 2, as 10^2 = 100.log10(taco_price)
calculates the logarithm of the taco price, assuming the ‘Spicy Carnitas’ taco costs $6.00 (as 10^0.778151 ≈ 6).
The log10
function is particularly useful for dealing with data that spans multiple orders of magnitude, such as comparing taco sales across different restaurant sizes or market populations.
sqrt
Calculates the square root of a number.
Syntax:
Arguments:
x
(numeric): The input value. Can be any numeric type:UInt*
,Int*
,Float*
, orDecimal*
.
Returns:
- The square root of
x
. Type:Float32
orFloat64
, depending on the input type.
Example:
Result:
In this example, we calculate the square root of the price for the ‘Carne Asada Supreme’ taco. If the price was $6, the square root would be approximately 2.45.
- For negative input values, the function returns
NaN
(Not a Number). - For
NULL
input, the function returnsNULL
.
cbrt
Returns the cubic root of the argument.
Syntax:
Arguments:
x
(numeric): A numeric value. Supported types:Int
,UInt
,Float
, orDecimal
.
Returns:
- The cubic root of
x
. - Type:
Float32
orFloat64
, depending on the input type.
Example:
Result:
This example calculates the cubic root of 27 (which is 3) and the cubic root of 8 (which is 2).
The cbrt
function is particularly useful when dealing with volumes or three-dimensional calculations. For example, you could use it to calculate the side length of a cubic taco box given its volume:
This query would return the side length of a cubic box for each taco order, assuming the volume is given in cubic centimeters.
erf
Returns the error function of the argument.
Syntax:
Arguments:
x
(numeric): A numeric value. SupportsUInt*
,Int*
,Float*
, andDecimal*
types.
Returns:
The error function value of x
. Type: Float64
.
Description:
The error function erf(x)
is a special mathematical function related to the probability that a random variable with normal distribution takes a value between 0 and x√2.
For non-negative x, erf(x√2)
is the probability that a random variable with a normal distribution of mean 0 and variance 1/2 falls in the range [−x, x].
Example:
Result:
This example calculates the probability that a value from a standard normal distribution falls within 2 standard deviations of the mean.
The erf
function is closely related to the normal distribution and is often used in statistical analysis and machine learning applications.
erfc
Returns the complementary error function of the argument.
Syntax:
Arguments:
x
(numeric): The input value. Type:Float32
,Float64
,Int8
,Int16
,Int32
,Int64
,UInt8
,UInt16
,UInt32
,UInt64
orDecimal
.
Returns:
A number close to 1−erf(x) without loss of precision for large x values. Type: Float64
.
Example:
Result:
This example calculates the complementary error function for the value 1.5.
The erfc
function is particularly useful in statistics and probability theory, especially when dealing with normally distributed data. It’s often used in scenarios where high precision is required for large input values, as it avoids the loss of significance that can occur when subtracting erf(x) from 1 for large x
lgamma
Returns the logarithm of the gamma function.
Syntax:
Arguments:
x
(numeric): A numeric value. Can be any of:Int8
,UInt8
,Int16
,UInt16
,Int32
,UInt32
,Int64
,UInt64
Float32
,Float64
Decimal32
,Decimal64
,Decimal128
,Decimal256
Returns:
- The logarithm of the gamma function of
x
. - Type:
Float64
Example:
Result:
This example calculates the logarithm of the gamma function for 5.5, which is useful in various statistical and mathematical computations.
The lgamma
function is particularly useful when dealing with large factorials or in statistical distributions where the gamma function appears, such as in the chi-squared distribution or in Bayesian statistics.
Syntax:
Arguments:
x
(numeric): A numeric value. Type:(U)Int*
,Float*
, orDecimal*
.
Returns:
- The gamma function of
x
. Type:Float64
.
Example:
Result:
This example calculates the gamma function of 5.5, which is approximately 52.34277778.
The gamma function is an extension of the factorial function to real and complex numbers. For positive integers, tgamma(n)
is equal to (n-1)!
.
Example:
Result:
In this example, we calculate a “taco gamma score” based on the taco rating. This could be used as a fun way to emphasize the differences between ratings, as the gamma function grows very quickly for larger inputs.
sin
Returns the sine of the argument.
Syntax:
Arguments:
x
(numeric): The angle in radians. AcceptsUInt*
,Int*
,Float*
, orDecimal*
types.
Returns:
- The sine of
x
. Type:Float64
.
Example:
Result:
This example calculates the sine of 45 degrees (π/4 radians), which is approximately 0.7071.
The sin
function operates on radians, not degrees. To convert degrees to radians, you can use the radians
function or multiply by π/180.
cos
Returns the cosine of the argument.
Syntax:
Arguments:
x
(numeric): The angle in radians. Accepts numeric types:Int
,UInt
,Float
, orDecimal
.
Returns:
- The cosine of
x
. - Type:
Float64
Example:
Result:
This example calculates the cosine of π, which is -1.
Example:
Result:
In this example, we calculate the cosine for different taco rotation angles. The taco_angle
is in radians, where:
- 0 radians (0°) gives a cosine of 1
- π/2 radians (90°) gives a cosine very close to 0
- π radians (180°) gives a cosine of -1
The slight deviation from 0 for π/2 is due to floating-point precision limitations.
tan
Returns the tangent of the argument.
Syntax:
Arguments:
x
(numeric): The angle in radians. Accepts numeric types:Int
,UInt
,Float
, orDecimal
.
Returns:
- The tangent of
x
. - Type:
Float64
Example:
Result:
This example calculates the tangent of π/4 radians (45 degrees), which is approximately 1, representing the slope of a perfectly diagonal taco shell.
The tan
function can produce very large values for angles close to π/2 + πn, where n is an integer. Be cautious when working with angles near these values.
asin
Returns the arc sine of the argument.
Syntax:
Arguments:
x
(numeric): The value to calculate the arc sine for. Acceptable types:UInt*
,Int*
,Float*
, orDecimal*
.
Returns:
- The arc sine of
x
in radians. Type:Float32
orFloat64
.
Example:
Result:
This example calculates the arc sine of 0.5, which is approximately π/6 radians or 30 degrees.
- The function expects input values in the range [-1, 1].
- For values outside this range, the function returns
NaN
(Not a Number). - The returned value is in radians, not degrees.
Example:
Result:
In this whimsical example, we’re using asin
to calculate a “filling angle” based on the weight of taco fillings. The angle could represent how high the filling might stack in the taco shell, with heavier fillings resulting in a higher angle.
acos
Returns the arc cosine of the argument.
Syntax:
Arguments:
x
(numeric): A number between -1 and 1. Can be any numeric type:Int
,Float
, orDecimal
.
Returns:
- The arc cosine of
x
in radians, ranging from 0 to π. - Type:
Float64
Example:
Result:
In this example, we calculate the arc cosine of 0.5, which could represent the angle of a folded taco shell. The result is approximately 1.047 radians or about 60 degrees.
If the input value is outside the range [-1, 1], the function returns nan
(Not a Number).
atan
Returns the arc tangent of the argument.
Syntax:
Arguments:
x
(numeric): The input value. Can be any numeric type:UInt*
,Int*
,Float*
, orDecimal*
.
Returns:
- The arc tangent of
x
in radians. Type:Float64
.
Example:
Result:
In this example, we calculate the arc tangent of 1, which represents the angle (in radians) of a perfectly folded taco shell at 45 degrees.
The returned value is in the range -π/2 to π/2 radians.
pow
Calculates x raised to the power of y.
Syntax:
Alias:
- power(x, y)
Arguments:
x
(UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
): Base.y
(UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
): Exponent.
Returns:
- The result of raising x to the power of y.
Type: Float64
Example:
Result:
This example calculates 2 raised to the power of 3, which equals 8.
Example:
Result:
In this example, we calculate a “spiciness factor” for different taco types by raising 2 to the power of their spice level. This creates an exponential scale to represent the perceived increase in spiciness.
The pow
function is particularly useful when you need to perform exponential calculations or create non-linear scales based on input values.
Syntax:
Arguments:
x
(Float64
): The angle, in radians. Values from the interval: −∞ < x < +∞.
Returns:
- The hyperbolic cosine of
x
. Values from the interval: 1 ≤ cosh(x) < +∞. - Type:
Float64
.
Example:
Result:
This example calculates the hyperbolic cosine of 0, which is always 1.
The cosh
function is commonly used in various mathematical and physical calculations, particularly in problems involving hyperbolic geometry or signal processing.
acosh
Calculates the inverse hyperbolic cosine of a number.
Syntax
Arguments
x
(Float64
): The input value. Must be greater than or equal to 1.
Returns
- The inverse hyperbolic cosine of
x
. Type:Float64
.
Description
The acosh
function returns the inverse hyperbolic cosine of x
. This is the value whose hyperbolic cosine is x
.
For input values less than 1, the function returns NaN
(Not a Number).
Example
Result:
Example:
Result:
This example shows how the “complexity” of stacking tacos increases as the stack height grows, using the acosh
function as a metaphor for stack complexity.
sinh
Returns the hyperbolic sine of the argument.
Syntax:
Arguments:
x
(Float64
): The angle, in radians. Values from the interval: −∞ < x < +∞.
Returns:
- The hyperbolic sine of
x
. Type:Float64
. - Values from the interval: −∞ < sinh(x) < +∞.
Example:
Result:
This example calculates the hyperbolic sine of 0, which is 0.
The sinh
function is particularly useful in various mathematical and physical calculations, especially those involving exponential growth or decay, such as in signal processing or solving differential equations.
asinh
Calculates the inverse hyperbolic sine of a number.
Syntax
Arguments
x
(numeric): The value to calculate the inverse hyperbolic sine for. Can be any numeric type:Int
,Float
, orDecimal
.
Returns
- The inverse hyperbolic sine of
x
. - Type:
Float64
Description
The asinh
function computes the inverse hyperbolic sine of x
. It is defined for all real numbers and is the inverse function of hyperbolic sine (sinh
).
For small values of x
, asinh(x)
is approximately equal to x
.
Example
Result:
In this example:
asinh(0)
returns exactly 0.asinh(1)
is approximately 0.8813736, rounded to 7 decimal places.asinh(-2)
is approximately -1.4436355, rounded to 7 decimal places.
The asinh
function is particularly useful in statistical and scientific computations, especially when dealing with hyperbolic geometries or certain types of distributions.
tanh
Returns the hyperbolic tangent of the argument.
Syntax:
Arguments:
x
(numeric): The angle, in radians. Values from the interval: −∞ < x < +∞. SupportsUInt*
,Int*
,Float*
, orDecimal*
types.
Returns:
- Values from the interval: −1 < tanh(x) < 1.
- Type:
Float64
Example:
Result:
This example calculates the hyperbolic tangent of 0, which is 0.
The tanh
function is commonly used in machine learning, particularly in neural networks as an activation function. It maps input values to output values between -1 and 1, with a steeper slope near 0.
Example:
Result:
In this example, we use tanh
to normalize taco spiciness ratings. The original ratings range from -2 (very mild) to 2 (extremely spicy), and tanh
maps these to values between -1 and 1, providing a normalized measure of spiciness that can be useful for comparisons or further calculations.
atanh
Returns the inverse hyperbolic tangent of the argument.
Syntax:
Arguments:
x
(numeric): Hyperbolic tangent of angle. Values from the interval: -1 < x < 1. SupportsUInt*
,Int*
,Float*
, orDecimal*
types.
Returns:
- The angle, in radians. Values from the interval: -∞ < atanh(x) < +∞.
- Type:
Float64
Example:
Result:
This example calculates the inverse hyperbolic tangent of 0.5, which represents the angle (in radians) whose hyperbolic tangent is 0.5.
The atanh
function is particularly useful in various mathematical and scientific computations, especially in fields like physics and engineering where hyperbolic functions are commonly used.
atan2
Returns the angle in radians between the positive x-axis and the ray from the origin to the point (y, x) in the Euclidean plane.
Syntax:
Arguments:
y
(Float64
): y-coordinate of the point.x
(Float64
): x-coordinate of the point.
Returns:
- The angle θ in radians, such that -π < θ ≤ π.
- Type:
Float64
Example:
Result:
This example calculates the angle between the x-axis and a line from (0,0) to (1,2), both in radians and degrees. It’s like finding the angle of a taco shell when held at that position!
The atan2
function is particularly useful in scenarios where you need to calculate angles or directions, such as in navigation systems or when working with polar coordinates.
hypot
Calculates the length of the hypotenuse of a right-angle triangle.
Syntax:
Arguments:
x
(Float64
): The length of the first cathetus.y
(Float64
): The length of the second cathetus.
Returns:
The length of the hypotenuse. [Float64
]
Description:
This function computes the length of the hypotenuse of a right-angle triangle using the formula: sqrt(x^2 + y^2)
. It’s designed to avoid problems that can occur when squaring very large or very small numbers.
Example:
Result:
In this example, we calculate the length of the hypotenuse for a right-angle triangle with catheti of length 3 and 4 (representing a 3-4-5 triangle often used in carpentry).
Example:
Result:
This example uses hypot
to calculate the straight-line distance between taco stands and the center of Los Angeles (latitude 34.0522, longitude -118.2437). The results are ordered to show the closest stands first.
This example assumes latitude and longitude are stored as decimal degrees. In real-world applications, more sophisticated geospatial functions would typically be used for accurate distance calculations on a spherical surface.
log1p
Calculates log(1+x)
. This calculation is more accurate than log(1+x)
for small values of x.
Syntax:
Arguments:
x
(numeric): A value from the interval: -1 < x < +∞. Can be any numeric type:Int
,Float
, orDecimal
.
Returns:
- The natural logarithm of 1 plus the argument. Type:
Float64
. - Values range from -∞ < log1p(x) < +∞.
Example:
Result:
This example calculates log1p(0)
, which is equivalent to log(1+0)
, resulting in 0.
Example:
Result:
In this example, we calculate log1p
of taco prices. This transformation can be useful for various statistical analyses or machine learning models where you want to compress the range of prices while maintaining the ability to differentiate between small price differences.
The log1p
function is particularly useful when dealing with values close to zero, as it provides better numerical stability compared to calculating log(1+x)
directly.
sign
Returns the sign of a numeric value.
Syntax:
Arguments:
x
(numeric): A numeric value. Supports all numeric types in ClickHouse.
Returns:
-1
for x < 00
for x = 01
for x > 0
Type: Int8
Example:
Result:
In this example:
- The
sign
function compares each taco price to $5. -1
indicates the taco is cheaper than $5.0
indicates the taco costs exactly $5.1
indicates the taco is more expensive than $5.
This function is useful for quickly categorizing numeric values based on their sign, which can be helpful in financial calculations, comparisons, or data analysis tasks.
sigmoid
Returns the sigmoid function value for the given input.
Syntax:
Arguments:
x
(numeric): The input value. Can be any numeric type:UInt*
,Int*
,Float*
, orDecimal*
.
Returns:
- The sigmoid function value for
x
, which is always between 0 and 1. Type:Float64
.
Example:
Result:
This example demonstrates how the sigmoid function maps input values to the range (0, 1), with 0 mapping to 0.5 and increasingly positive or negative values approaching 1 or 0 respectively.
The sigmoid function is commonly used in machine learning, particularly in logistic regression and neural networks, as an activation function. It’s useful for transforming any input into a probability-like output between 0 and 1.
degrees
Converts radians to degrees.
Syntax:
Arguments:
x
(numeric): Input value in radians. Can be any numeric type:Int*
,UInt*
,Float*
, orDecimal*
.
Returns:
- The angle in degrees. Type:
Float64
.
Example:
Result:
This example converts π radians to degrees, resulting in 180 degrees.
Example:
Result:
In this example, we convert the rotation angle of tacos on a spinner from radians to degrees, filtering for tacos that have rotated more than 180 degrees.
radians
Converts degrees to radians.
Syntax:
Arguments:
x
(numeric): Input in degrees. Can be any numeric type:UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
.
Returns:
The value in radians. Type: Float64
.
Example:
Result:
This example converts 180 degrees to radians, resulting in π (approximately 3.14159).
The radians
function is useful when you need to convert angle measurements from degrees to radians for trigonometric calculations or when working with circular data in a taco analysis, such as calculating the optimal angle for folding a taco shell.
factorial
Computes the factorial of an integer value.
Syntax:
Arguments:
n
(UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
): An integer value.
Returns:
The factorial of the input value. [UInt64
]
Details:
- The factorial of 0 is 1.
- For any negative input value, the function returns 1.
- The maximum allowed input value is 20. Values of 21 or greater will cause an exception.
Example:
Result:
In this example:
five_factorial
calculates 5! which is 5 * 4 * 3 * 2 * 1 = 120.zero_factorial
demonstrates that 0! is defined as 1.negative_factorial
shows that the function returns 1 for negative inputs.max_factorial
calculates 20!, which is the largest factorial the function can compute.
Attempting to calculate the factorial of a number greater than 20 will result in an exception. For example, SELECT factorial(21)
would throw an error.
widthBucket
Returns the bucket number into which the operand
value falls in a histogram with count
equal-width buckets spanning the range from low
to high
.
Syntax:
Alias:
- WIDTH_BUCKET
Arguments:
operand
(numeric): The value to be bucketed. Can be any native number type.low
(numeric): The lower bound of the range. Can be any native number type.high
(numeric): The upper bound of the range. Can be any native number type.count
(UInt64
): The number of buckets. Must be an unsigned native integer and cannot be zero.
Returns:
- The bucket number (1-based) as a
UInt64
. - Returns 0 if
operand < low
. - Returns
count + 1
ifoperand >= high
.
Example:
Result:
In this example:
- Bucket 1 represents tacos priced from 3.99
- Bucket 2 represents tacos priced from 6.99
- Bucket 3 represents tacos priced from 9.99
- Bucket 4 represents tacos priced $10.00 and above
This function is useful for creating histograms or for grouping continuous data into discrete buckets for analysis.
proportionsZTest
Performs a two-proportion Z-test, which is a statistical test for comparing proportions from two populations.
Syntax:
Arguments:
successes_x
(UInt64
): Number of successes in population x.successes_y
(UInt64
): Number of successes in population y.trials_x
(UInt64
): Number of trials in population x.trials_y
(UInt64
): Number of trials in population y.conf_level
(Float64
): Confidence level for the test (e.g., 0.95 for 95% confidence).pool_type
(String
): Method for estimating standard error. Can be either ‘unpooled’ or ‘pooled’.
Returns:
A tuple containing four Float64
values:
z_stat
- The Z statistic.p_val
- The p-value.ci_low
- The lower bound of the confidence interval.ci_high
- The upper bound of the confidence interval.
- In the pooled version, the two proportions are averaged, and only one proportion is used to estimate the standard error.
- In the unpooled version, the two proportions are used separately.
Example:
Result:
In this example, we’re comparing the success rates of two different taco recipes. The function returns the Z-statistic, p-value, and confidence interval for the difference in proportions.