13.4. Math functions

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

The following graph is used for the examples below:

Figure 13.4. Graph

Number functions

abs()

abs() returns the absolute value for a number.

Syntax: abs( expression )

Arguments:

  • 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.

Result

a.agee.ageabs(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)

ceil()

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

Syntax: ceil( expression )

Arguments:

  • expression: A numeric expression.

Query 

RETURN ceil(0.1)

The ceil of 0.1.

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)

floor()

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

Syntax: floor( expression )

Arguments:

  • expression: A numeric expression.

Query 

RETURN floor(0.9)

The floor of 0.9 is returned.

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)

round()

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

Syntax: round( expression )

Arguments:

  • expression: A numeric expression that represents the angle in radians.

Query 

RETURN round(3.141592)

3.0 is returned.

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)

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:

  • expression: A numeric expression.

Query 

RETURN sign(-17), sign(0.1)

The signs of -17 and 0.1 are returned.

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)

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()

Arguments:

Query 

RETURN rand()

A random number is returned.

Result

rand()

1 row

0.03960974390331995

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()

Logarithmic functions

log()

log() returns the natural logarithm of the expression.

Syntax: log( expression )

Arguments:

  • expression: A numeric expression.

Query 

RETURN log(27)

The natural logarithm of 27 is returned.

Result

log(27)

1 row

3.295836866004329

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 log(27)

log10()

log10() returns the common logarithm (base 10) of the expression.

Syntax: log10( expression )

Arguments:

  • expression: A numeric expression.

Query 

RETURN log10(27)

The common logarithm of 27 is returned.

Result

log10(27)

1 row

1.4313637641589874

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 log10(27)

exp()

exp() returns e^n, where e is the base of the natural logarithm, and n is the value of the argument expression.

Syntax: e( expression )

Arguments:

  • expression: A numeric expression.

Query 

RETURN exp(2)

e to the power of 2 is returned.

Result

exp(2)

1 row

7.38905609893065

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 exp(2)

e()

e() returns the base of the natural logarithm, e.

Syntax: e()

Arguments:

Query 

RETURN e()

The base of the natural logarithm, e, is returned.

Result

e()

1 row

2.718281828459045

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 e()

sqrt()

sqrt() returns the square root of a number.

Syntax: sqrt( expression )

Arguments:

  • expression: A numeric expression.

Query 

RETURN sqrt(256)

The square root of 256 is returned.

Result

sqrt(256)

1 row

16.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 sqrt(256)

Trigonometric functions

All trigonometric functions operate on radians, unless otherwise specified.

sin()

sin() returns the sine of the expression.

Syntax: sin( expression )

Arguments:

  • expression: A numeric expression that represents the angle in radians.

Query 

RETURN sin(0.5)

The sine of 0.5 is returned.

Result

sin(0.5)

1 row

0.479425538604203

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 sin(0.5)

cos()

cos() returns the cosine of the expression.

Syntax: cos( expression )

Arguments:

  • expression: A numeric expression that represents the angle in radians.

Query 

RETURN cos(0.5)

The cosine of 0.5.

Result

cos(0.5)

1 row

0.8775825618903728

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 cos(0.5)

tan()

tan() returns the tangent of the expression.

Syntax: tan( expression )

Arguments:

  • expression: A numeric expression that represents the angle in radians.

Query 

RETURN tan(0.5)

The tangent of 0.5 is returned.

Result

tan(0.5)

1 row

0.5463024898437905

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 tan(0.5)

cot()

cot() returns the cotangent of the expression.

Syntax: cot( expression )

Arguments:

  • expression: A numeric expression that represents the angle in radians.

Query 

RETURN cot(0.5)

The cotangent of 0.5.

Result

cot(0.5)

1 row

1.830487721712452

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 cot(0.5)

asin()

asin() returns the arcsine of the expression, in radians.

Syntax: asin( expression )

Arguments:

  • expression: A numeric expression that represents the angle in radians.

Query 

RETURN asin(0.5)

The arcsine of 0.5.

Result

asin(0.5)

1 row

0.5235987755982989

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 asin(0.5)

acos()

acos() returns the arccosine of the expression, in radians.

Syntax: abs( expression )

Arguments:

  • expression: A numeric expression that represents the angle in radians.

Query 

RETURN acos(0.5)

The arccosine of 0.5.

Result

acos(0.5)

1 row

1.0471975511965979

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 acos(0.5)

atan()

atan() returns the arctangent of the expression, in radians.

Syntax: atan( expression )

Arguments:

  • expression: A numeric expression that represents the angle in radians.

Query 

RETURN atan(0.5)

The arctangent of 0.5.

Result

atan(0.5)

1 row

0.4636476090008061

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 atan(0.5)

atan2()

atan2() returns the arctangent2 of a set of coordinates, in radians.

Syntax: atan2( expression1, expression2 )

Arguments:

  • expression1: A numeric expression for y that represents the angle in radians.
  • expression2: A numeric expression for x that represents the angle in radians.

Query 

RETURN atan2(0.5, 0.6)

The arctangent2 of 0.5 and 0.6.

Result

atan2(0.5, 0.6)

1 row

0.6947382761967033

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 atan2(0.5, 0.6)

pi()

pi() returns the mathematical constant pi.

Syntax: pi()

Arguments:

Query 

RETURN pi()

The constant pi is returned.

Result

pi()

1 row

3.141592653589793

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 pi()

degrees()

degrees() converts radians to degrees.

Syntax: degrees( expression )

Arguments:

  • expression: A numeric expression that represents the angle in radians.

Query 

RETURN degrees(3.14159)

The number of degrees in something close to pi.

Result

degrees(3.14159)

1 row

179.99984796050427

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 degrees(3.14159)

radians()

radians() returns the common logarithm (base 10) of the expression.

Syntax: radians( expression )

Arguments:

  • expression: A numeric expression that represents the angle in degrees.

Query 

RETURN radians(180)

The number of radians in 180 degrees is returned (pi).

Result

radians(180)

1 row

3.141592653589793

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 radians(180)

haversin()

haversin() returns half the versine of the expression.

Syntax: haversin( expression )

Arguments:

  • expression: A numeric expression that represents the angle in radians.

Query 

RETURN haversin(0.5)

The haversine of 0.5 is returned.

Result

haversin(0.5)

1 row

0.06120871905481362

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 haversin(0.5)

Spherical distance using the haversin function

The haversin() function may be used to compute the distance on the surface of a sphere between two points (each given by their latitude and longitude). In this example the spherical distance (in km) between Berlin in Germany (at lat 52.5, lon 13.4) and San Mateo in California (at lat 37.5, lon -122.3) is calculated using an average earth radius of 6371 km.

Query 

CREATE (ber:City { lat: 52.5, lon: 13.4 }),(sm:City { lat: 37.5, lon: -122.3 })
RETURN 2 * 6371 * asin(sqrt(haversin(radians(sm.lat - ber.lat))+ cos(radians(sm.lat))* cos(radians(ber.lat))* haversin(radians(sm.lon - ber.lon)))) AS dist

The estimated distance between Berlin and San Mateo is returned.

Result

dist

1 row Nodes created: 2 Properties set: 4 Labels added: 2

9129.969740051658

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) CREATE (ber:City {lat: 52.5, lon: 13.4}), (sm:City {lat: 37.5, lon: -122.3}) RETURN 2 * 6371 * asin(sqrt(haversin(radians( sm.lat - ber.lat )) + cos(radians( sm.lat )) * cos(radians( ber.lat )) * haversin(radians( sm.lon - ber.lon )))) AS dist