Numerical Types, Functions, and Operators

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.

Mathematical functions

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.

Bitwise functions

bit_and()

Bitwise AND operator for 2 intergers.

bit_or()

Bitwise OR operator for 2 intergers.

bit_xor()

Bitwise exclusive OR operator for 2 intergers.

bit_not()

Bitwise negation operator for 2 intergers.

bit_lshift()

Bitwise left-shift operator for intergers.

bit_rshift()

Bitwise arithemtic right-shift operator for intergers.

String parsing

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

Numbers - 图1

Numbers - 图2

Numbers - 图3

int16

A 16-bit signed integer.

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

type

int32

Numbers - 图4

Numbers - 图5

Numbers - 图6

int32

A 32-bit signed integer.

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

type

int64

Numbers - 图7

Numbers - 图8

Numbers - 图9

int64

A 64-bit signed integer.

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

type

float32

Numbers - 图10

Numbers - 图11

Numbers - 图12

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

Numbers - 图13

Numbers - 图14

Numbers - 图15

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

Numbers - 图16

Numbers - 图17

Numbers - 图18

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’:

  1. db>
  1. select 42n is bigint;
  1. {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.

  1. db>
  1. select 1e+100n is bigint;
  1. {true}

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

  1. db>
  1. select 1.23n is decimal;
  1. {true}
  1. db>
  1. select 1.0e+100n is decimal;
  1. {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

Numbers - 图19

Numbers - 图20

Numbers - 图21

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’:

  1. db>
  1. select 1.23n is decimal;
  1. {true}
  1. db>
  1. select 1.0e+100n is decimal;
  1. {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:

  1. db>
  1. select 42n is bigint;
  1. {true}
  1. db>
  1. select 12e+34n is bigint;
  1. {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

Numbers - 图22

Numbers - 图23

Numbers - 图24

anyreal + anyreal -> anyreal

Arithmetic addition.

  1. db>
  1. select 2 + 2;
  1. {4}

operator

anyreal - anyreal

Numbers - 图25

Numbers - 图26

Numbers - 图27

anyreal - anyreal -> anyreal

Arithmetic subtraction.

  1. db>
  1. select 3 - 2;
  1. {1}

operator

-anyreal

Numbers - 图28

Numbers - 图29

Numbers - 图30

- anyreal -> anyreal

Arithmetic negation.

  1. db>
  1. select -5;
  1. {-5}

operator

anyreal * anyreal

Numbers - 图31

Numbers - 图32

Numbers - 图33

anyreal * anyreal -> anyreal

Arithmetic multiplication.

  1. db>
  1. select 2 * 10;
  1. {20}

operator

anyreal / anyreal

Numbers - 图34

Numbers - 图35

Numbers - 图36

anyreal / anyreal -> anyreal

Arithmetic division.

  1. db>
  1. select 10 / 4;
  1. {2.5}

Division by zero results in an error:

  1. db>
  1. select 10 / 0;
  1. DivisionByZeroError: division by zero

operator

anyreal // anyreal

Numbers - 图37

Numbers - 图38

Numbers - 图39

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.

  1. db>
  1. select 10 // 4;
  1. {2}
  1. db>
  1. select math::floor(10 / 4);
  1. {2}
  1. db>
  1. select -10 // 4;
  1. {-3}

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

  1. db>
  1. select 3.7 // 1.1;
  1. {3.0}
  1. db>
  1. select 3.7n // 1.1n;
  1. {3.0n}
  1. db>
  1. select 37 // 11;
  1. {3}

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

operator

anyreal % anyreal

Numbers - 图40

Numbers - 图41

Numbers - 图42

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:

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

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

Modulo division by zero results in an error:

  1. db>
  1. select 10 % 0;
  1. DivisionByZeroError: division by zero

operator

anyreal ^ anyreal

Numbers - 图43

Numbers - 图44

Numbers - 图45

anyreal ^ anyreal -> anyreal

Power operation.

  1. db>
  1. select 2 ^ 4;
  1. {16}

function

round()

Numbers - 图46

Numbers - 图47

Numbers - 图48

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:

  1. db>
  1. select round(1.2);
  1. {1}
  1. db>
  1. select round(1.5);
  1. {2}
  1. db>
  1. select round(2.5);
  1. {2}

decimal tie is rounded away from 0:

  1. db>
  1. select round(1.2n);
  1. {1n}
  1. db>
  1. select round(1.5n);
  1. {2n}
  1. db>
  1. select round(2.5n);
  1. {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.

  1. db>
  1. select round(163.278n, 2);
  1. {163.28n}
  1. db>
  1. select round(163.278n, 1);
  1. {163.3n}
  1. db>
  1. select round(163.278n, 0);
  1. {163n}
  1. db>
  1. select round(163.278n, -1);
  1. {160n}
  1. db>
  1. select round(163.278n, -2);
  1. {200n}

function

random()

Numbers - 图49

Numbers - 图50

Numbers - 图51

std::random() -> float64

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

  1. db>
  1. select random();
  1. {0.62649393780157}

function

bit_and()

Numbers - 图52

Numbers - 图53

Numbers - 图54

std::bit_and(l: int16, r: int16) -> int16std::bit_and(l: int32, r: int32) -> int32std::bit_and(l: int64, r: int64) -> int64

Bitwise AND operator for 2 intergers.

  1. db>
  1. select bit_and(17, 3);
  1. {1}

function

bit_or()

Numbers - 图55

Numbers - 图56

Numbers - 图57

std::bit_or(l: int16, r: int16) -> int16std::bit_or(l: int32, r: int32) -> int32std::bit_or(l: int64, r: int64) -> int64

Bitwise OR operator for 2 intergers.

  1. db>
  1. select bit_or(17, 3);
  1. {19}

function

bit_xor()

Numbers - 图58

Numbers - 图59

Numbers - 图60

std::bit_xor(l: int16, r: int16) -> int16std::bit_xor(l: int32, r: int32) -> int32std::bit_xor(l: int64, r: int64) -> int64

Bitwise exclusive OR operator for 2 intergers.

  1. db>
  1. select bit_xor(17, 3);
  1. {18}

function

bit_not()

Numbers - 图61

Numbers - 图62

Numbers - 图63

std::bit_not(r: int16) -> int16std::bit_not(r: int32) -> int32std::bit_not(r: int64) -> int64

Bitwise negation operator for 2 intergers.

Bitwise negation for integers ends up similar to mathematical negation because typically the signed integers use “two’s complement” representation. In this represenation mathematical negation is achieved by aplying bitwise negation and adding 1.

  1. db>
  1. select bit_not(17);
  1. {-18}
  1. db>
  1. select -17 = bit_not(17) + 1;
  1. {true}

function

bit_lshift()

Numbers - 图64

Numbers - 图65

Numbers - 图66

std::bit_lshift(val: int16, n: int64) -> int16std::bit_lshift(val: int32, n: int64) -> int32std::bit_lshift(val: int64, n: int64) -> int64

Bitwise left-shift operator for intergers.

The integer val is shifted by n bits to the left. The rightmost added bits are all 0. Shifting an integer by a number of bits larger than the bit size of the integer results in 0.

  1. db>
  1. select bit_lshift(123, 2);
  1. {492}
  1. db>
  1. select bit_lshift(123, 65);
  1. {0}

It is possible to affect the sign bit by left-shifting an integer.

  1. db>
  1. select bit_lshift(123, 60);
  1. {-5764607523034234880}

In general left-shifting an integer in small increments produces the same result as shifting it in one step.

  1. db>
  1. select bit_lshift(bit_lshift(123, 1), 3);
  1. {1968}
  1. db>
  1. select bit_lshift(123, 4);
  1. {1968}

It is an error to attempt to shift by a negative number of bits.

  1. db>
  1. select bit_lshift(123, -2);
  1. edgedb error: InvalidValueError: bit_lshift(): cannot shift by
  2. negative amount

function

bit_rshift()

Numbers - 图67

Numbers - 图68

Numbers - 图69

std::bit_rshift(val: int16, n: int64) -> int16std::bit_rshift(val: int32, n: int64) -> int32std::bit_rshift(val: int64, n: int64) -> int64

Bitwise arithemtic right-shift operator for intergers.

The integer val is shifted by n bits to the right. In the arithmetic right-shift the sign is preserved. This means that the leftmost added bits are 1 or 0 depending on the sign bit. Shifting an integer by a number of bits larger than the bit size of the integer results in 0 for positive numbers and -1 for negative numbers.

  1. db>
  1. select bit_rshift(123, 2);
  1. {30}
  1. db>
  1. select bit_rshift(123, 65);
  1. {0}
  1. db>
  1. select bit_rshift(-123, 2);
  1. {-31}
  1. db>
  1. select bit_rshift(-123, 65);
  1. {-1}

In general right-shifting an integer in small increments produces the same result as shifting it in one step.

  1. db>
  1. select bit_rshift(bit_rshift(123, 1), 3);
  1. {7}
  1. db>
  1. select bit_rshift(123, 4);
  1. {7}
  1. db>
  1. select bit_rshift(bit_rshift(-123, 1), 3);
  1. {-8}
  1. db>
  1. select bit_rshift(-123, 4);
  1. {-8}

It is an error to attempt to shift by a negative number of bits.

  1. db>
  1. select bit_rshift(123, -2);
  1. edgedb error: InvalidValueError: bit_rshift(): cannot shift by
  2. negative amount

function

to_bigint()

Numbers - 图70

Numbers - 图71

Numbers - 图72

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.

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

For more details on formatting see here.

function

to_decimal()

Numbers - 图73

Numbers - 图74

Numbers - 图75

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.

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

For more details on formatting see here.

function

to_int16()

Numbers - 图76

Numbers - 图77

Numbers - 图78

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

Numbers - 图79

Numbers - 图80

Numbers - 图81

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

Numbers - 图82

Numbers - 图83

Numbers - 图84

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

Numbers - 图85

Numbers - 图86

Numbers - 图87

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

Numbers - 图88

Numbers - 图89

Numbers - 图90

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.