13.4. Mathematical functions

13.4.1. ABS
13.4.2. ACOS
13.4.3. ASIN
13.4.4. ATAN
13.4.5. ATAN2
13.4.6. COS
13.4.7. COT
13.4.8. DEGREES
13.4.9. E
13.4.10. EXP
13.4.11. FLOOR
13.4.12. HAVERSIN
13.4.13. LOG
13.4.14. LOG10
13.4.15. PI
13.4.16. RADIANS
13.4.17. RAND
13.4.18. ROUND
13.4.19. SIGN
13.4.20. SIN
13.4.21. SQRT
13.4.22. TAN

These functions all operate on numerical expressions only, and will return an error if used on any other values.

See also Section 8.4.1, “Mathematical operators”.

Figure 13.4. Graph


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


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


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


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


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


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


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


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


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


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


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


13.4.12. HAVERSIN

HAVERSIN returns the half 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


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


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


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


13.4.15. PI

PI returns the mathmatical constant pi.

Syntax: PI()

Arguments:

Query. 

RETURN pi()

The constant pi is returned.

Result

pi()
1 row

3.141592653589793


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


13.4.17. 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.2649907229190529


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


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


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


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


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