Numeric functions
The mathematical functions of Raft DB: rounding, powers, logarithms, trigonometry and number bases.
The numeric functions take numbers; text that reads as a number is converted, other text counts as 0 (strict mode does not raise a warning for it in a SELECT). NULL in gives NULL out. Functions that cannot compute a value (SQRT(-1), LN(0), ACOS(2)) answer NULL, as MySQL does outside strict division checks.
Exact arithmetic (+ - * / % DIV on whole numbers and DECIMAL) is done in 128 bits, so a result is exact up to that size; the functions below that give a double (POW, EXP, the logarithms and the trigonometric functions) are floating point. The operators are on Arithmetic and logical operators.
Summary of functions#
| Function | What it does | Dialect |
|---|---|---|
ABS | Returns the absolute value of x. | MySQL and MariaDB |
ACOS | Returns the arc cosine of x in radians. | MySQL and MariaDB |
ASIN | Returns the arc sine of x in radians. | MySQL and MariaDB |
ATAN | With one argument returns the arc tangent of x; with two arguments the arc tangent of y / x, using the signs of both to find the quadrant. | MySQL and MariaDB |
ATAN2 | The angle, in radians, whose tangent is y / x; the sign of each argument picks the quadrant the angle lies in. | MySQL and MariaDB |
BIT_COUNT | Returns the number of bits set in the whole number n. | MySQL and MariaDB |
CEIL | Returns the smallest whole number not less than x. | MySQL and MariaDB |
CONV | Converts the number n, written as a string in base from_base, to a string in base to_base (2 to 36; a negative to_base writes a signed result). | MySQL and MariaDB |
COS | Returns the cosine of x (in radians). | MySQL and MariaDB |
COT | Returns the cotangent of x. | MySQL and MariaDB |
CRC32 | Returns the 32-bit cyclic redundancy check of a string as an unsigned number. | MySQL and MariaDB |
DEGREES | Converts x from radians to degrees. | MySQL and MariaDB |
EXP | Returns e raised to the power x. | MySQL and MariaDB |
FLOOR | Returns the largest whole number not greater than x. | MySQL and MariaDB |
GREATEST | Returns the largest argument. | MySQL and MariaDB |
INTERVAL | Returns 0 if n is less than n1, 1 if it is less than n2, and so on: the number of the arguments n1, n2, ... that are less than or equal to n. | MySQL and MariaDB |
LEAST | Returns the smallest argument. | MySQL and MariaDB |
LN | Returns the natural logarithm of x; NULL if x is not greater than 0. | MySQL and MariaDB |
LOG | With one argument returns the natural logarithm of x; with two arguments the logarithm of x in base b. | MySQL and MariaDB |
LOG10 | Returns the base-10 logarithm of x. | MySQL and MariaDB |
LOG2 | Returns the base-2 logarithm of x. | MySQL and MariaDB |
MOD | Returns the remainder of n divided by m. | MySQL and MariaDB |
PI | Returns the value of pi as a double (3.141592653589793). | MySQL and MariaDB |
POW | Returns x raised to the power y (a double). | MySQL and MariaDB |
RADIANS | Converts x from degrees to radians. | MySQL and MariaDB |
RAND | Returns a random double from 0 up to but not including 1. | MySQL and MariaDB |
ROUND | Rounds x to d decimals (0 by default; a negative d rounds to the left of the decimal point). | MySQL and MariaDB |
SIGN | Returns -1, 0 or 1 as x is negative, zero or positive. | MySQL and MariaDB |
SIN | Returns the sine of x (in radians). | MySQL and MariaDB |
SQRT | Returns the square root of x; NULL if x is negative. | MySQL and MariaDB |
TAN | Returns the tangent of x (in radians). | MySQL and MariaDB |
TRUNCATE | Returns x with the decimals after the d-th cut off (no rounding). | MySQL and MariaDB |
ABS#
ABS(x)Returns the absolute value of x.
Example:
SELECT ABS(-32);Result: 32
ACOS#
ACOS(x)Returns the arc cosine of x in radians. NULL if x is outside -1 to 1.
Example:
SELECT ACOS(1), ACOS(2);Result: 0, NULL
ASIN#
ASIN(x)Returns the arc sine of x in radians. NULL if x is outside -1 to 1.
Example:
SELECT ASIN(1);Result: 1.5707963267948966
ATAN#
ATAN(x)
ATAN(y, x)With one argument returns the arc tangent of x; with two arguments the arc tangent of y / x, using the signs of both to find the quadrant.
Example:
SELECT ATAN(1);Result: 0.7853981633974483
ATAN2#
ATAN2(y, x)The angle, in radians, whose tangent is y / x; the sign of each argument picks the quadrant the angle lies in.
Example:
SELECT ATAN2(1, 1);Result: 0.7853981633974483
BIT_COUNT#
BIT_COUNT(n)Returns the number of bits set in the whole number n.
Example:
SELECT BIT_COUNT(29);Result: 4
CEIL#
CEIL(x)
CEILING(x)Returns the smallest whole number not less than x. CEILING is a synonym.
Example:
SELECT CEIL(1.23), CEIL(-1.23);Result: 2, -1
CONV#
CONV(n, from_base, to_base)Converts the number n, written as a string in base from_base, to a string in base to_base (2 to 36; a negative to_base writes a signed result).
Example:
SELECT CONV('a', 16, 2);Result: 1010
COS#
COS(x)Returns the cosine of x (in radians).
Example:
SELECT COS(0);Result: 1
COT#
COT(x)Returns the cotangent of x.
Example:
SELECT COT(1);Result: 0.6420926159343306
CRC32#
CRC32(expr)Returns the 32-bit cyclic redundancy check of a string as an unsigned number.
Example:
SELECT CRC32('MySQL');Result: 3259397556
DEGREES#
DEGREES(x)Converts x from radians to degrees.
Example:
SELECT DEGREES(PI());Result: 180
EXP#
EXP(x)Returns e raised to the power x.
Example:
SELECT EXP(0);Result: 1
FLOOR#
FLOOR(x)Returns the largest whole number not greater than x.
Example:
SELECT FLOOR(-1.5);Result: -2
GREATEST#
GREATEST(value1, value2, ...)Returns the largest argument. The result is NULL if any argument is NULL.
Example:
SELECT GREATEST(1, 5, 3);Result: 5
INTERVAL#
INTERVAL(n, n1, n2, ...)Returns 0 if n is less than n1, 1 if it is less than n2, and so on: the number of the arguments n1, n2, ... that are less than or equal to n. The list must be in increasing order. Returns -1 for NULL. (This is the function; the INTERVAL expr unit of date arithmetic is on Date and time functions.)
Example:
SELECT INTERVAL(23, 1, 15, 17, 30, 44, 200);Result: 3
LEAST#
LEAST(value1, value2, ...)Returns the smallest argument. The result is NULL if any argument is NULL.
Example:
SELECT LEAST(2, 0);Result: 0
LN#
LN(x)Returns the natural logarithm of x; NULL if x is not greater than 0.
Example:
SELECT LN(1);Result: 0
LOG#
LOG(x)
LOG(b, x)With one argument returns the natural logarithm of x; with two arguments the logarithm of x in base b. NULL for x not greater than 0 and for b not greater than 0 or equal to 1.
Example:
SELECT LOG(10);Result: 2.302585092994046
LOG10#
LOG10(x)Returns the base-10 logarithm of x.
Example:
SELECT LOG10(100);Result: 2
LOG2#
LOG2(x)Returns the base-2 logarithm of x.
Example:
SELECT LOG2(8);Result: 3
MOD#
MOD(n, m)
n % m
n MOD mReturns the remainder of n divided by m. The result is NULL if m is 0.
Example:
SELECT MOD(10, 3), 10 % 3;Result: 1, 1
PI#
PI()Returns the value of pi as a double (3.141592653589793).
Example:
SELECT PI();Result: about 3.14159
POW#
POW(x, y)
POWER(x, y)Returns x raised to the power y (a double). POWER is a synonym.
Example:
SELECT POW(2, 10);Result: 1024
RADIANS#
RADIANS(x)Converts x from degrees to radians.
Example:
SELECT RADIANS(180);Result: 3.141592653589793
RAND#
RAND()Returns a random double from 0 up to but not including 1. A seed argument is accepted and ignored: the values are not repeatable.
Example:
SELECT RAND() < 1;Result: 1
ROUND#
ROUND(x [, d])Rounds x to d decimals (0 by default; a negative d rounds to the left of the decimal point). An exact number (a DECIMAL or a literal such as 2.5) rounds half away from zero; a FLOAT or DOUBLE rounds half to even, as the C library does.
Example:
SELECT ROUND(2.5), ROUND(2.5e0), ROUND(1.298, 1);Result: 3, 2, 1.3
SIGN#
SIGN(x)Returns -1, 0 or 1 as x is negative, zero or positive.
Example:
SELECT SIGN(-5);Result: -1
SIN#
SIN(x)Returns the sine of x (in radians).
Example:
SELECT SIN(0);Result: 0
SQRT#
SQRT(x)Returns the square root of x; NULL if x is negative.
Example:
SELECT SQRT(16);Result: 4
TAN#
TAN(x)Returns the tangent of x (in radians).
Example:
SELECT TAN(0);Result: 0
TRUNCATE#
TRUNCATE(x, d)Returns x with the decimals after the d-th cut off (no rounding).
Example:
SELECT TRUNCATE(1.999, 1);Result: 1.9