Mathematical functions - numeric
These functions all operate on numeric expressions only, and will return an error if used on any other values. See also Mathematical operators.
Functions:
The following graph is used for the examples below:
abs()
abs()
returns the absolute value of the given number.
Syntax:
abs(expression)
Returns:
The type of the value returned will be that of |
Arguments:
Name | Description |
---|---|
|
A numeric expression. |
Considerations:
|
If |
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.
a.age | e.age | abs(a.age - e.age) |
---|---|---|
|
|
|
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 |
---|---|
|
A numeric expression. |
Considerations:
|
RETURN ceil(0.1)
The ceil of 0.1
is returned.
ceil(0.1) |
---|
|
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 |
---|---|
|
A numeric expression. |
Considerations:
|
RETURN floor(0.9)
The floor of 0.9
is returned.
floor(0.9) |
---|
|
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. |
RETURN rand()
A random number is returned.
rand() |
---|
|
Rows: 1 |
round()
round()
returns the value of the given number rounded to the nearest integer, with ties always rounded towards positive infinity.
Syntax:
round(expression)
Returns:
A Float. |
Arguments:
Name | Description |
---|---|
|
A numeric expression to be rounded. |
Considerations:
|
RETURN round(3.141592)
3.0
is returned.
round(3.141592) |
---|
|
Rows: 1 |
RETURN round(-1.5)
Ties are rounded towards positive infinity, therfore -1.0
is returned.
round(-1.5) |
---|
|
Rows: 1 |
round(), with precision
round()
returns the value of the given number rounded to the closest value of given precision, with ties always being rounded away from zero (using rounding mode HALF_UP
).
The exception is for precision 0, where ties are rounded towards positive infinity to align with round() without precision.
Syntax:
round(expression, precision)
Returns:
A Float. |
Arguments:
Name | Description |
---|---|
|
A numeric expression to be rounded. |
|
A numeric expression specifying precision. |
Considerations:
|
RETURN round(3.141592, 3)
3.142
is returned.
round(3.141592, 3) |
---|
|
Rows: 1 |
RETURN round(-1.5, 0)
To align with round(-1.5)
, -1.0
is returned.
round(-1.5, 0) |
---|
|
Rows: 1 |
RETURN round(-1.55, 1)
The default is to round away from zero when there is a tie, therefore -1.6
is returned.
round(-1.55, 1) |
---|
|
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 |
---|---|
|
A numeric expression to be rounded. |
|
A numeric expression specifying precision. |
|
A string expression specifying rounding mode. |
Modes:
Mode |
Description |
---|---|
|
Round away from zero. |
|
Round towards zero. |
|
Round towards positive infinity. |
|
Round towards negative infinity. |
|
Round towards closest value of given precision, with ties always being rounded away from zero. |
|
Round towards closest value of given precision, with ties always being rounded towards zero. |
|
Round towards closest value of given precision, with ties always being rounded to the even neighbor. |
Considerations:
For the rounding modes, a tie means that the two closest values of the given precision are at the same distance from the given value. E.g. for precision 1, 2.15 is a tie as it has equal distance to 2.1 and 2.2, while 2.151 is not a tie, as it is closer to 2.2. |
|
RETURN round(1.249, 1, 'UP') AS positive,
round(-1.251, 1, 'UP') AS negative,
round(1.25, 1, 'UP') AS positiveTie,
round(-1.35, 1, 'UP') AS negativeTie
The rounded values using precision 1 and rounding mode UP
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
RETURN round(1.249, 1, 'DOWN') AS positive,
round(-1.251, 1, 'DOWN') AS negative,
round(1.25, 1, 'DOWN') AS positiveTie,
round(-1.35, 1, 'DOWN') AS negativeTie
The rounded values using precision 1 and rounding mode DOWN
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
RETURN round(1.249, 1, 'CEILING') AS positive,
round(-1.251, 1, 'CEILING') AS negative,
round(1.25, 1, 'CEILING') AS positiveTie,
round(-1.35, 1, 'CEILING') AS negativeTie
The rounded values using precision 1 and rounding mode CEILING
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
RETURN round(1.249, 1, 'FLOOR') AS positive,
round(-1.251, 1, 'FLOOR') AS negative,
round(1.25, 1, 'FLOOR') AS positiveTie,
round(-1.35, 1, 'FLOOR') AS negativeTie
The rounded values using precision 1 and rounding mode FLOOR
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
RETURN round(1.249, 1, 'HALF_UP') AS positive,
round(-1.251, 1, 'HALF_UP') AS negative,
round(1.25, 1, 'HALF_UP') AS positiveTie,
round(-1.35, 1, 'HALF_UP') AS negativeTie
The rounded values using precision 1 and rounding mode HALF_UP
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
RETURN round(1.249, 1, 'HALF_DOWN') AS positive,
round(-1.251, 1, 'HALF_DOWN') AS negative,
round(1.25, 1, 'HALF_DOWN') AS positiveTie,
round(-1.35, 1, 'HALF_DOWN') AS negativeTie
The rounded values using precision 1 and rounding mode HALF_DOWN
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
Rows: 1 |
RETURN round(1.249, 1, 'HALF_EVEN') AS positive,
round(-1.251, 1, 'HALF_EVEN') AS negative,
round(1.25, 1, 'HALF_EVEN') AS positiveTie,
round(-1.35, 1, 'HALF_EVEN') AS negativeTie
The rounded values using precision 1 and rounding mode HALF_EVEN
are returned.
positive | negative | positiveTie | negativeTie |
---|---|---|---|
|
|
|
|
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 |
---|---|
|
A numeric expression. |
Considerations:
|
RETURN sign(-17), sign(0.1)
The signs of -17
and 0.1
are returned.
sign(-17) | sign(0.1) |
---|---|
|
|
Rows: 1 |