Numeric Functions
Grafito's Cypher supports the standard mathematical functions and operators.
All functions return null when given a null argument.
Arithmetic Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Addition | RETURN 2 + 3 AS r |
5 |
- |
Subtraction | RETURN 5 - 2 AS r |
3 |
* |
Multiplication | RETURN 4 * 3 AS r |
12 |
/ |
Division | RETURN 10 / 3 AS r |
3 |
% |
Modulo | RETURN 10 % 3 AS r |
1 |
^ |
Exponentiation | RETURN 2 ^ 10 AS r |
1024.0 |
Division of two integers performs integer division (10 / 3 is 3); if
either operand is a float the result is a float (10.0 / 4 is 2.5).
Exponentiation always returns a float and is right-associative
(2 ^ 2 ^ 3 is 2 ^ 8 = 256.0).
Dividing or taking the modulo by zero raises an error.
Rounding and Sign
abs()
Absolute value.
ceil() / floor() / round()
ceil() rounds up, floor() rounds down, and round() rounds half away from
zero (so round(2.5) is 3.0 and round(-2.5) is -3.0). All return floats.
sign()
Returns -1, 0, or 1 for negative, zero, or positive numbers.
Powers, Roots and Logarithms
sqrt()
Square root. Raises an error for negative arguments.
exp() / log() / log10()
exp(x) is e raised to x; log(x) is the natural logarithm; log10(x) is the
base-10 logarithm. log() and log10() require a positive argument.
Trigonometry
sin(), cos(), tan(), cot(), asin(), acos(), atan(), and
atan2(y, x) operate in radians. asin() and acos() require an argument in
[-1, 1]. degrees() and radians() convert between units, and haversin()
returns (1 - cos(x)) / 2.
RETURN sin(0) AS r // 0.0
RETURN asin(1) AS r // 1.5707963267948966
RETURN atan2(1, 1) AS r // 0.7853981633974483
RETURN degrees(pi()) AS r // 180.0
RETURN radians(180) AS r // 3.141592653589793
Constants and Randomness
e() / pi()
rand()
Returns a random float in [0, 1).
Numeric Casting
toInteger() and toFloat() convert numbers and numeric strings; see
String Functions for details.
For element-wise list casting, toIntegerList() / toFloatList() turn each
convertible element and leave non-convertible ones as null.
Null Propagation
Every numeric function returns null when any argument is null, so they
compose safely over optional data: