12.4. Mathematical 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”.

Figure 12.4. Graph

ABS

ABS returns the absolute value of 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.0

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 match (a), (e) where a.name = 'Alice' and e.name = 'Eskil' return a.age, e.age, abs(a.age - e.age)

ACOS

ACOS returns the arccosine of the expression, in radians.

Syntax: ACOS( expression )

Arguments:

  • expression: A numeric expression.

Query. 

RETURN acos(0.5)

The arccosine of 0.5.

Result

acos(0.5)
1 row

1.0471975511965979

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return acos(0.5)

ASIN

ASIN returns the arcsine of the expression, in radians.

Syntax: ASIN( expression )

Arguments:

  • expression: A numeric expression.

Query. 

RETURN asin(0.5)

The arcsine of 0.5.

Result

asin(0.5)
1 row

0.5235987755982989

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return asin(0.5)

ATAN

ATAN returns the arctangent of the expression, in radians.

Syntax: ATAN( expression )

Arguments:

  • expression: A numeric expression.

Query. 

RETURN atan(0.5)

The arctangent of 0.5.

Result

atan(0.5)
1 row

0.4636476090008061

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return atan(0.5)

ATAN2

ATAN2 returns the arctangent2 of a set of coordinates, in radians.

Syntax: ATAN2( expression , expression)

Arguments:

  • expression: A numeric expression for y.
  • expression: A numeric expression for x.

Query. 

RETURN atan2(0.5, 0.6)

The arctangent2 of 0.5, 0.6.

Result

atan2(0.5, 0.6)
1 row

0.6947382761967033

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return atan2(0.5, 0.6)

COS

COS returns the cosine of the expression.

Syntax: COS( expression )

Arguments:

  • expression: A numeric expression.

Query. 

RETURN cos(0.5)

The cosine of 0.5 is returned.

Result

cos(0.5)
1 row

0.8775825618903728

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return cos(0.5)

COT

COT returns the cotangent of the expression.

Syntax: COT( expression )

Arguments:

  • expression: A numeric expression.

Query. 

RETURN cot(0.5)

The cotangent of 0.5 is returned.

Result

cot(0.5)
1 row

1.830487721712452

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return cot(0.5)

DEGREES

DEGREES converts radians to degrees.

Syntax: DEGREES( expression )

Arguments:

  • expression: A numeric expression.

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 (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return degrees(3.14159)

E

E returns the constant, e.

Syntax: E()

Arguments:

Query. 

RETURN e()

The constant e is returned (the base of natural log).

Result

e()
1 row

2.718281828459045

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return e()

EXP

EXP returns the value e raised to the power of the expression.

Syntax: EXP( expression )

Arguments:

  • expression: A numeric expression.

Query. 

RETURN exp(2)

The exp of 2 is returned: e2.

Result

exp(2)
1 row

7.38905609893065

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return exp(2)

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 (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return floor(0.9)

HAVERSIN

HAVERSIN returns half the versine of the expression.

Syntax: HAVERSIN( expression )

Arguments:

  • expression: A numeric expression.

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 (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 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 distance between Berlin and San Mateo is returned (about 9129 km).

Result

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

9129.969740051658

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 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

LOG

LOG returns the natural logarithm of the expression.

Syntax: LOG( expression )

Arguments:

  • expression: A numeric expression.

Query. 

RETURN log(27)

The log of 27 is returned.

Result

log(27)
1 row

3.295836866004329

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return log(27)

LOG10

LOG10 returns the base 10 logarithm of the expression.

Syntax: LOG10( expression )

Arguments:

  • expression: A numeric expression.

Query. 

RETURN log10(27)

The log10 of 27 is returned.

Result

log10(27)
1 row

1.4313637641589874

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return log10(27)

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 (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return pi()

RADIANS

RADIANS converts degrees to radians.

Syntax: RADIANS( expression )

Arguments:

  • expression: A numeric expression.

Query. 

RETURN radians(180)

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

Result

radians(180)
1 row

3.141592653589793

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return radians(180)

RAND

RAND returns a random double between 0 and 1.0.

Syntax: RAND( expression )

Arguments:

  • expression: A numeric expression.

Query. 

RETURN rand() AS x1

A random number is returned.

Result

x1
1 row

0.2969139531839424

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return rand() as x1

ROUND

ROUND returns the numerical expression, rounded to the nearest integer.

Syntax: ROUND( expression )

Arguments:

  • expression: A numerical expression.

Query. 

RETURN round(3.141592)

Result

round(3.141592)
1 row

3.0

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 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 numerical expression

Query. 

RETURN sign(-17), sign(0.1)

Result

sign(-17)sign(0.1)
1 row

-1.0

1.0

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return sign(-17), sign(0.1)

SIN

SIN returns the sine of the expression.

Syntax: SIN( expression )

Arguments:

  • expression: A numeric expression.

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 (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return sin(0.5)

SQRT

SQRT returns the square root of a number.

Syntax: SQRT( expression )

Arguments:

  • expression: A numerical expression

Query. 

RETURN sqrt(256)

Result

sqrt(256)
1 row

16.0

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return sqrt(256)

TAN

TAN returns the tangent of the expression.

Syntax: TAN( expression )

Arguments:

  • expression: A numeric expression.

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 (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 return tan(0.5)