Mathematical functions - numeric

Functions:

The following graph is used for the examples below:

Graph
  N0 [
    label = "{A|name = \'Alice\'\leyes = \'brown\'\lage = 38\l}"
  ]
  N0 -> N2 [
    color = "#2e3436"
    fontcolor = "#2e3436"
    label = "KNOWS\n"
  ]
  N0 -> N1 [
    color = "#2e3436"
    fontcolor = "#2e3436"
    label = "KNOWS\n"
  ]
  N1 [
    label = "{B|name = \'Bob\'\leyes = \'blue\'\lage = 25\l}"
  ]
  N1 -> N3 [
    color = "#2e3436"
    fontcolor = "#2e3436"
    label = "KNOWS\n"
  ]
  N1 -> N4 [
    color = "#4e9a06"
    fontcolor = "#4e9a06"
    label = "MARRIED\n"
  ]
  N2 [
    label = "{C|name = \'Charlie\'\leyes = \'green\'\lage = 53\l}"
  ]
  N2 -> N3 [
    color = "#2e3436"
    fontcolor = "#2e3436"
    label = "KNOWS\n"
  ]
  N3 [
    label = "{D|name = \'Daniel\'\leyes = \'brown\'\lage = 54\l}"
  ]
  N4 [
    label = "{E|array = \[\'one\', \'two\', \'three\'\]\lname = \'Eskil\'\leyes = \'blue\'\lage = 41\l}"
  ]

abs()

abs() returns the absolute value of the given number.

Syntax: abs(expression)

Returns:

The type of the value returned will be that of expression.

Arguments:

Name Description

expression

A numeric expression.

Considerations:

abs(null) returns null.

If expression is negative, -(expression) (i.e. the negation of expression) is returned.

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 1. Result
a.age e.age abs(a.age - e.age)

38

41

3

Rows: 1

ceil()

ceil() returns the smallest floating point number that is greater than or equal to the given number and equal to a mathematical integer.

Syntax: ceil(expression)

Returns:

A Float.

Arguments:

Name Description

expression

A numeric expression.

Considerations:

ceil(null) returns null.

Query
RETURN ceil(0.1)

The ceil of 0.1 is returned.

Table 2. Result
ceil(0.1)

1.0

Rows: 1

floor()

floor() returns the largest floating point number that is less than or equal to the given number and equal to a mathematical integer.

Syntax: floor(expression)

Returns:

A Float.

Arguments:

Name Description

expression

A numeric expression.

Considerations:

floor(null) returns null.

Query
RETURN floor(0.9)

The floor of 0.9 is returned.

Table 3. Result
floor(0.9)

0.0

Rows: 1

rand()

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

Syntax: rand()

Returns:

A Float.

Query
RETURN rand()

A random number is returned.

Table 4. Result
rand()

0.9664035824403059

Rows: 1

round()

round() returns the value of the given number rounded to the nearest integer, with half-way values always rounded up.

Syntax: round(expression)

Returns:

A Float.

Arguments:

Name Description

expression

A numeric expression to be rounded.

Considerations:

round(null) returns null.

Query
RETURN round(3.141592)

3.0 is returned.

Table 5. Result
round(3.141592)

3.0

Rows: 1

round(), with precision

round() returns the value of the given number rounded with the specified precision, with half-values always being rounded up.

Syntax: round(expression, precision)

Returns:

A Float.

Arguments:

Name Description

expression

A numeric expression to be rounded.

precision

A numeric expression specifying precision.

Considerations:

round(null) returns null.

Query
RETURN round(3.141592, 3)

3.142 is returned.

Table 6. Result
round(3.141592, 3)

3.142

Rows: 1

round(), with precision and rounding mode

round() returns the value of the given number rounded with the specified precision and the specified rounding mode.

Syntax: round(expression, precision, mode)

Returns:

A Float.

Arguments:

Name Description

expression

A numeric expression to be rounded.

precision

A numeric expression specifying precision.

mode

A string expression specifying rounding mode.

Modes:

Mode Description

CEILING

Round towards positive infinity.

DOWN

Round towards zero.

FLOOR

Round towards zero.

HALF_DOWN

Round towards closest value of given precision, with half-values always being rounded down.

HALF_EVEN

Round towards closest value of given precision, with half-values always being rounded to the even neighbor.

HALF_UP

Round towards closest value of given precision, with half-values always being rounded up.

UP

Round away from zero.

Considerations:

round(null) returns null.

Query
RETURN round(3.141592, 2, 'CEILING')

3.15 is returned.

Table 7. Result
round(3.141592, 2, 'CEILING')

3.15

Rows: 1

sign()

sign() returns the signum of the given number: 0 if the number is 0, -1 for any negative number, and 1 for any positive number.

Syntax: sign(expression)

Returns:

An Integer.

Arguments:

Name Description

expression

A numeric expression.

Considerations:

sign(null) returns null.

Query
RETURN sign(-17), sign(0.1)

The signs of -17 and 0.1 are returned.

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

-1

1

Rows: 1