16-bit integer | |
32-bit integer | |
64-bit integer | |
32-bit floating point number | |
64-bit floating point number | |
Arbitrary precision integer. | |
Arbitrary precision number. | |
Arithmetic addition. | |
Arithmetic subtraction. | |
Arithmetic negation. | |
Arithmetic multiplication. | |
Arithmetic division. | |
Floor division. | |
Remainder from division (modulo). | |
Power operation. | |
Comparison operators | |
Return the sum of the set of numbers. | |
Return the smallest value of the input set. | |
Return the greatest value of the input set. | |
Round to the nearest value. | |
Return a pseudo-random number in the range 0.0 <= x < 1.0. |
Return the absolute value of the input x. | |
Round up to the nearest integer. | |
Round down to the nearest integer. | |
Return the natural logarithm of the input value. | |
Return the base 10 logarithm of the input value. | |
Return the logarithm of the input value in the specified base. | |
Return the arithmetic mean of the input set. | |
Return the sample standard deviation of the input set. | |
Return the population standard deviation of the input set. | |
Return the sample variance of the input set. | |
Return the population variance of the input set. |
Bitwise AND operator for 2 intergers. | |
Bitwise OR operator for 2 intergers. | |
Bitwise exclusive OR operator for 2 intergers. | |
Bitwise negation operator for 2 intergers. | |
Bitwise left-shift operator for intergers. | |
Bitwise arithemtic right-shift operator for intergers. |
Create a bigint value. | |
Create a decimal value. | |
Create an int16 value. | |
Create an int32 value. | |
Create an int64 value. | |
Create a float32 value. | |
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
.
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’:
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.
db>
select 1e+100n is bigint;
{true}
When a float literal is followed by ‘n’ it produces a
decimal
instead:
db>
select 1.23n is decimal;
{true}
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.
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’:
db>
select 1.23n is decimal;
{true}
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:
db>
select 42n is bigint;
{true}
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.
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.
db>
select 10 // 4;
{2}
db>
select math::floor(10 / 4);
{2}
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:
db>
select 3.7 // 1.1;
{3.0}
db>
select 3.7n // 1.1n;
{3.0n}
db>
select 37 // 11;
{3}
Regular division, floor division, and %
are
related in the following way: A // B = (A - (A % B)) / B
.
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:
db>
select 10 % 4;
{2}
db>
select 10n % 4;
{2n}
db>
select -10 % 4;
{2}
db> ... ...
# floating arithmetic is inexact, so
# we get 0.3999999999999999 instead of 0.4
select 3.7 % 1.1;
{0.3999999999999999}
db>
select 3.7n % 1.1n;
{0.4n}
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:
db>
select 10 % 0;
DivisionByZeroError: division by zero
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:
db>
select round(1.2);
{1}
db>
select round(1.5);
{2}
db>
select round(2.5);
{2}
decimal
tie is rounded away from 0:
db>
select round(1.2n);
{1n}
db>
select round(1.5n);
{2n}
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.
db>
select round(163.278n, 2);
{163.28n}
db>
select round(163.278n, 1);
{163.3n}
db>
select round(163.278n, 0);
{163n}
db>
select round(163.278n, -1);
{160n}
db>
select round(163.278n, -2);
{200n}
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
.
db>
select bit_not(17);
{-18}
db>
select -17 = bit_not(17) + 1;
{true}
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
.
db>
select bit_lshift(123, 2);
{492}
db>
select bit_lshift(123, 65);
{0}
It is possible to affect the sign bit by left-shifting an integer.
db>
select bit_lshift(123, 60);
{-5764607523034234880}
In general left-shifting an integer in small increments produces the same result as shifting it in one step.
db>
select bit_lshift(bit_lshift(123, 1), 3);
{1968}
db>
select bit_lshift(123, 4);
{1968}
It is an error to attempt to shift by a negative number of bits.
db>
select bit_lshift(123, -2);
edgedb error: InvalidValueError: bit_lshift(): cannot shift by negative amount
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.
db>
select bit_rshift(123, 2);
{30}
db>
select bit_rshift(123, 65);
{0}
db>
select bit_rshift(-123, 2);
{-31}
db>
select bit_rshift(-123, 65);
{-1}
In general right-shifting an integer in small increments produces the same result as shifting it in one step.
db>
select bit_rshift(bit_rshift(123, 1), 3);
{7}
db>
select bit_rshift(123, 4);
{7}
db>
select bit_rshift(bit_rshift(-123, 1), 3);
{-8}
db>
select bit_rshift(-123, 4);
{-8}
It is an error to attempt to shift by a negative number of bits.
db>
select bit_rshift(123, -2);
edgedb error: InvalidValueError: bit_rshift(): cannot shift by negative amount
Create a decimal
value.
Parse a decimal
from the input s and optional format
specification fmt.
db>
select to_decimal('-000,012,345', 'S099,999,999,999');
{-12345.0n}
db>
select to_decimal('-012.345');
{-12.345n}
db>
select to_decimal('31st', '999th');
{31.0n}
For more details on formatting see here.