3.4.5. Mathematical functions - numeric

These functions all operate on numerical expressions only, and will return an error if used on any other values. See also Section 3.2.5.3, “Mathematical operators”.

Functions:

The following graph is used for the examples below:

Figure 3.24. Graph
alt

3.4.5.1. abs()

abs() returns the absolute value of a number.

Syntax: abs(expression)

Arguments:

Name Description

expression

A numeric expression.

Query. 

MATCH (a),(e)
WHERE a.name = 'Alice' AND e.name = 'Eskil'
RETURN a.age, e.age, abs(a.age - e.age)

The absolute value of the age difference is returned.

Table 3.152. Result
a.age e.age abs(a.age - e.age)

1 row

38

41

3

Try this query live.  CREATE (alice:A {name:'Alice', age: 38, eyes: 'brown'}), (bob:B {name: 'Bob', age: 25, eyes: 'blue'}), (charlie:C {name: 'Charlie', age: 53, eyes: 'green'}), (daniel:D {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil:E {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH (a), (e) WHERE a.name = 'Alice' AND e.name = 'Eskil' RETURN a.age, e.age, abs(a.age - e.age)

3.4.5.2. ceil()

ceil() returns the smallest integer greater than or equal to the argument.

Syntax: ceil(expression)

Arguments:

Name Description

expression

A numeric expression.

Query. 

RETURN ceil(0.1)

The ceil of 0.1.

Table 3.153. Result
ceil(0.1)

1 row

1.0

Try this query live.  CREATE (alice:A {name:'Alice', age: 38, eyes: 'brown'}), (bob:B {name: 'Bob', age: 25, eyes: 'blue'}), (charlie:C {name: 'Charlie', age: 53, eyes: 'green'}), (daniel:D {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil:E {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) RETURN ceil(0.1)

3.4.5.3. floor()

floor() returns the greatest integer less than or equal to the expression.

Syntax: floor(expression)

Arguments:

Name Description

expression

A numeric expression.

Query. 

RETURN floor(0.9)

The floor of 0.9 is returned.

Table 3.154. Result
floor(0.9)

1 row

0.0

Try this query live.  CREATE (alice:A {name:'Alice', age: 38, eyes: 'brown'}), (bob:B {name: 'Bob', age: 25, eyes: 'blue'}), (charlie:C {name: 'Charlie', age: 53, eyes: 'green'}), (daniel:D {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil:E {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) RETURN floor(0.9)

3.4.5.4. rand()

rand() returns a random number in the range from 0 (inclusive) to 1 (exclusive), [0,1). The numbers returned follow an approximate uniform distribution.

Syntax: rand()

Query. 

RETURN rand()

A random number is returned.

Table 3.155. Result
rand()

1 row

0.8767513676341224

Try this query live.  CREATE (alice:A {name:'Alice', age: 38, eyes: 'brown'}), (bob:B {name: 'Bob', age: 25, eyes: 'blue'}), (charlie:C {name: 'Charlie', age: 53, eyes: 'green'}), (daniel:D {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil:E {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) RETURN rand()

3.4.5.5. round()

round() returns the numerical expression, rounded to the nearest integer.

Syntax: round(expression)

Arguments:

Name Description

expression

A numeric expression that represents the angle in radians.

Query. 

RETURN round(3.141592)

3.0 is returned.

Table 3.156. Result
round(3.141592)

1 row

3.0

Try this query live.  CREATE (alice:A {name:'Alice', age: 38, eyes: 'brown'}), (bob:B {name: 'Bob', age: 25, eyes: 'blue'}), (charlie:C {name: 'Charlie', age: 53, eyes: 'green'}), (daniel:D {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil:E {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) RETURN round(3.141592)

3.4.5.6. sign()

sign() returns the signum of a number — zero if the expression is zero, -1 for any negative number, and 1 for any positive number.

Syntax: sign(expression)

Arguments:

Name Description

expression

A numeric expression.

Query. 

RETURN sign(-17), sign(0.1)

The signs of -17 and 0.1 are returned.

Table 3.157. Result
sign(-17) sign(0.1)

1 row

-1

1

Try this query live.  CREATE (alice:A {name:'Alice', age: 38, eyes: 'brown'}), (bob:B {name: 'Bob', age: 25, eyes: 'blue'}), (charlie:C {name: 'Charlie', age: 53, eyes: 'green'}), (daniel:D {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil:E {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) RETURN sign(-17), sign(0.1)