int16

16-bit integer

int32

32-bit integer

int64

64-bit integer

float32

32-bit floating point number

float64

64-bit floating point number

bigint

Arbitrary precision integer.

decimal

Arbitrary precision number.

anyreal + anyreal

Arithmetic addition.

anyreal - anyreal

Arithmetic subtraction.

-anyreal

Arithmetic negation.

anyreal * anyreal

Arithmetic multiplication.

anyreal / anyreal

Arithmetic division.

anyreal // anyreal

Floor division.

anyreal % anyreal

Remainder from division (modulo).

anyreal ^ anyreal

Power operation.

= != ?= ?!= < > <= >=

Comparison operators

sum()

Return the sum of the set of numbers.

min()

Return the smallest value of the input set.

max()

Return the greatest value of the input set.

round()

Round to the nearest value.

random()

Return a pseudo-random number in the range 0.0 <= x < 1.0.

math::abs()

Return the absolute value of the input x.

math::ceil()

Round up to the nearest integer.

math::floor()

Round down to the nearest integer.

math::ln()

Return the natural logarithm of the input value.

math::lg()

Return the base 10 logarithm of the input value.

math::log()

Return the logarithm of the input value in the specified base.

math::mean()

Return the arithmetic mean of the input set.

math::stddev()

Return the sample standard deviation of the input set.

math::stddev_pop()

Return the population standard deviation of the input set.

math::var()

Return the sample variance of the input set.

math::var_pop()

Return the population variance of the input set.

to_bigint()

Create a bigint value.

to_decimal()

Create a decimal value.

to_int16()

Create an int16 value.

to_int32()

Create an int32 value.

to_int64()

Create an int64 value.

to_float32()

Create a float32 value.

to_float64()

Create a float64 value.

It’s possible to explicitly cast between all numeric types. All numeric types can also be cast to and from str and json.

type
int16
int16

A 16-bit signed integer.

An integer value in range from -32768 to +32767 (inclusive).

type
int32
int32

A 32-bit signed integer.

An integer value in range from -2147483648 to +2147483647 (inclusive).

type
int64
int64

A 64-bit signed integer.

An integer value in range from -9223372036854775808 to +9223372036854775807 (inclusive).

type
float32
float32

A variable precision, inexact number.

Minimal guaranteed precision is at least 6 decimal digits. The approximate range of a float32 is -3.4e+38 to +3.4e+38.

type
float64
float64

A variable precision, inexact number.

Minimal guaranteed precision is at least 15 decimal digits. The approximate range of a float64 is -1.7e+308 to +1.7e+308.

type
bigint
bigint

Arbitrary precision integer.

The EdgeDB philosophy is that using bigint type should be an explicit opt-in, but once used, the values should not be accidentally cast into a numeric type with less precision.

In accordance with this the mathematical functions are designed to keep the separation between bigint values and the rest of the numeric types.

All of the following types can be explicitly cast into bigint: str, json, int16, int32, int64, float32, float64, and decimal.

A bigint literal is an integer literal followed by ‘n’:

Copy
db> 
SELECT 42n IS bigint;
{true}

To represent really big integers it is possible to use the exponent notation (e.g. 1e20n instead of 100000000000000000000n) as long as the exponent is positive and there is no dot anywhere.

Copy
db> 
SELECT 1e+100n IS bigint;
{true}

When a float literal is followed by ‘n’ it produces a decimal instead:

Copy
db> 
SELECT 1.23n IS decimal;
{true}
Copy
db> 
SELECT 1.0e+100n IS decimal;
{true}

Caution is advised when casting bigint values into json. The JSON specification does not have a limit on significant digits, so a bigint number can be losslessly represented in JSON. However, JSON decoders in many languages will read all such numbers as some kind of 32- or 64-bit number type, which may result in errors or precision loss. If such loss is unacceptable, then consider casting the value into str and decoding it on the client side into a more appropriate type.

type
decimal
decimal

Any number of arbitrary precision.

The EdgeDB philosophy is that using a decimal type should be an explicit opt-in, but once used, the values should not be accidentally cast into a numeric type with less precision.

In accordance with this the mathematical functions are designed to keep the separation between decimal values and the rest of the numeric types.

All of the following types can be explicitly cast into decimal: str, json, int16, int32, int64, float32, float64, and bigint.

A decimal literal is a float literal followed by ‘n’:

Copy
db> 
SELECT 1.23n IS decimal;
{true}
Copy
db> 
SELECT 1.0e+100n IS decimal;
{true}

Note that an integer literal (without a dot or exponent) followed by ‘n’ produces a bigint. A literal without a dot and with a positive exponent makes a bigint, too:

Copy
db> 
SELECT 42n IS bigint;
{true}
Copy
db> 
SELECT 12e+34n IS bigint;
{true}

Caution is advised when casting decimal values into json. The JSON specification does not have a limit on significant digits, so a decimal number can be losslessly represented in JSON. However, JSON decoders in many languages will read all such numbers as some kind of floating point values, which may result in precision loss. If such loss is unacceptable, then consider casting the value into str and decoding it on the client side into a more appropriate type.

operator
anyreal + anyreal
anyreal + anyreal -> anyreal

Arithmetic addition.

Copy
db> 
SELECT 2 + 2;
{4}
operator
anyreal - anyreal
anyreal - anyreal -> anyreal

Arithmetic subtraction.

Copy
db> 
SELECT 3 - 2;
{1}
operator
-anyreal
- anyreal -> anyreal

Arithmetic negation.

Copy
db> 
SELECT -5;
{-5}
operator
anyreal * anyreal
anyreal * anyreal -> anyreal

Arithmetic multiplication.

Copy
db> 
SELECT 2 * 10;
{20}
operator
anyreal / anyreal
anyreal / anyreal -> anyreal

Arithmetic division.

Copy
db> 
SELECT 10 / 4;
{2.5}

Division by zero results in an error:

Copy
db> 
SELECT 10 / 0;
DivisionByZeroError: division by zero
operator
anyreal // anyreal
anyreal // anyreal -> anyreal

Floor division.

The result is rounded down to the nearest integer. It is equivalent to using regular division and the applying math::floor() to the result.

Copy
db> 
SELECT 10 // 4;
{2}
Copy
db> 
SELECT math::floor(10 / 4);
{2}
Copy
db> 
SELECT -10 // 4;
{-3}

It also works on float, bigint, and decimal types. The type of the result corresponds to the type of the operands:

Copy
db> 
SELECT 3.7 // 1.1;
{3.0}
Copy
db> 
SELECT 3.7n // 1.1n;
{3.0n}
Copy
db> 
SELECT 37 // 11;
{3}

Regular division, floor division, and % are related in the following way: A // B = (A - (A % B)) / B.

operator
anyreal % anyreal
anyreal % anyreal -> anyreal

Remainder from division (modulo).

This is the remainder from floor division. Just as is the case with // the result type of the remainder operator corresponds to the operand type:

Copy
db> 
SELECT 10 % 4;
{2}
Copy
db> 
SELECT 10n % 4;
{2n}
Copy
db> 
SELECT -10 % 4;
{2}
Copy
db> 
... 
... 
# floating arithmetic is inexact, so
# we get 0.3999999999999999 instead of 0.4
SELECT 3.7 % 1.1;
{0.3999999999999999}
Copy
db> 
SELECT 3.7n % 1.1n;
{0.4n}
Copy
db> 
SELECT 37 % 11;
{4}

Regular division, // and % are related in the following way: A // B = (A - (A % B)) / B.

Modulo division by zero results in an error:

Copy
db> 
SELECT 10 % 0;
DivisionByZeroError: division by zero
operator
anyreal ^ anyreal
anyreal ^ anyreal -> anyreal

Power operation.

Copy
db> 
SELECT 2 ^ 4;
{16}
function
round()
std::round(value: int64) -> float64std::round(value: float64) -> float64std::round(value: bigint) -> bigintstd::round(value: decimal) -> decimalstd::round(value: decimal, d: int64) -> decimal

Round to the nearest value.

There’s a difference in how ties (which way 0.5 is rounded) are handled depending on the type of the input value.

float64 tie is rounded to the nearest even number:

Copy
db> 
SELECT round(1.2);
{1}
Copy
db> 
SELECT round(1.5);
{2}
Copy
db> 
SELECT round(2.5);
{2}

decimal tie is rounded away from 0:

Copy
db> 
SELECT round(1.2n);
{1n}
Copy
db> 
SELECT round(1.5n);
{2n}
Copy
db> 
SELECT round(2.5n);
{3n}

Additionally, when rounding a decimal value an optional argument d can be provided to specify to what decimal point the value must to be rounded.

Copy
db> 
SELECT round(163.278n, 2);
{163.28n}
Copy
db> 
SELECT round(163.278n, 1);
{163.3n}
Copy
db> 
SELECT round(163.278n, 0);
{163n}
Copy
db> 
SELECT round(163.278n, -1);
{160n}
Copy
db> 
SELECT round(163.278n, -2);
{200n}
function
random()
std::random() -> float64

Return a pseudo-random number in the range 0.0 <= x < 1.0.

Copy
db> 
SELECT random();
{0.62649393780157}
function
to_bigint()
std::to_bigint(s: str, fmt: OPTIONAL str={}) -> bigint

Create a bigint value.

Parse a bigint from the input s and optional format specification fmt.

Copy
db> 
SELECT to_bigint('-000,012,345', 'S099,999,999,999');
{-12345n}
Copy
db> 
SELECT to_bigint('31st', '999th');
{31n}

For more details on formatting see here.

function
to_decimal()
std::to_decimal(s: str, fmt: OPTIONAL str={}) -> decimal

Create a decimal value.

Parse a decimal from the input s and optional format specification fmt.

Copy
db> 
SELECT to_decimal('-000,012,345', 'S099,999,999,999');
{-12345.0n}
Copy
db> 
SELECT to_decimal('-012.345');
{-12.345n}
Copy
db> 
SELECT to_decimal('31st', '999th');
{31.0n}

For more details on formatting see here.

function
to_int16()
std::to_int16(s: str, fmt: OPTIONAL str={}) -> int16

Create an int16 value.

Parse an int16 from the input s and optional format specification fmt.

For more details on formatting see here.

function
to_int32()
std::to_int32(s: str, fmt: OPTIONAL str={}) -> int32

Create an int32 value.

Parse an int32 from the input s and optional format specification fmt.

For more details on formatting see here.

function
to_int64()
std::to_int64(s: str, fmt: OPTIONAL str={}) -> int64

Create an int64 value.

Parse an int64 from the input s and optional format specification fmt.

For more details on formatting see here.

function
to_float32()
std::to_float32(s: str, fmt: OPTIONAL str={}) -> float32

Create a float32 value.

Parse a float32 from the input s and optional format specification fmt.

For more details on formatting see here.

function
to_float64()
std::to_float64(s: str, fmt: OPTIONAL str={}) -> float64

Create a float64 value.

Parse a float64 from the input s and optional format specification fmt.

For more details on formatting see here.

Light
Dark
System