# Numerical Types, Functions, and Operators

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |                                                                |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
| [`int16`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int16)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | 16-bit integer                                                 |
| [`int32`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int32)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | 32-bit integer                                                 |
| [`int64`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int64)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | 64-bit integer                                                 |
| [`float32`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::float32)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | 32-bit floating point number                                   |
| [`float64`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::float64)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | 64-bit floating point number                                   |
| [`bigint`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::bigint)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | Arbitrary precision integer.                                   |
| [`decimal`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::decimal)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | Arbitrary precision number.                                    |
| [`anyreal + anyreal`](https://docs.geldata.com/reference/stdlib/numbers.md#operator::plus)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | Arithmetic addition.                                           |
| [`anyreal - anyreal`](https://docs.geldata.com/reference/stdlib/numbers.md#operator::minus)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | Arithmetic subtraction.                                        |
| [`-anyreal`](https://docs.geldata.com/reference/stdlib/numbers.md#operator::uminus)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | Arithmetic negation.                                           |
| [`anyreal * anyreal`](https://docs.geldata.com/reference/stdlib/numbers.md#operator::mult)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | Arithmetic multiplication.                                     |
| [`anyreal / anyreal`](https://docs.geldata.com/reference/stdlib/numbers.md#operator::div)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | Arithmetic division.                                           |
| [`anyreal // anyreal`](https://docs.geldata.com/reference/stdlib/numbers.md#operator::floordiv)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | Floor division.                                                |
| [`anyreal % anyreal`](https://docs.geldata.com/reference/stdlib/numbers.md#operator::mod)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | Remainder from division (modulo).                              |
| [`anyreal ^ anyreal`](https://docs.geldata.com/reference/stdlib/numbers.md#operator::pow)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | Power operation.                                               |
| [`=`](https://docs.geldata.com/reference/stdlib/generic.md#operator::eq) [`!=`](https://docs.geldata.com/reference/stdlib/generic.md#operator::neq) [`?=`](https://docs.geldata.com/reference/stdlib/generic.md#operator::coaleq) [`?!=`](https://docs.geldata.com/reference/stdlib/generic.md#operator::coalneq) [`<`](https://docs.geldata.com/reference/stdlib/generic.md#operator::lt) [`>`](https://docs.geldata.com/reference/stdlib/generic.md#operator::gt) [`<=`](https://docs.geldata.com/reference/stdlib/generic.md#operator::lteq) [`>=`](https://docs.geldata.com/reference/stdlib/generic.md#operator::gteq) | Comparison operators                                           |
| [`sum()`](https://docs.geldata.com/reference/stdlib/set.md#function::std::sum)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | Return the arithmetic sum of values in a set.                  |
| [`min()`](https://docs.geldata.com/reference/stdlib/set.md#function::std::min)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | Returns the smallest value in the given set.                   |
| [`max()`](https://docs.geldata.com/reference/stdlib/set.md#function::std::max)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | Returns the largest value in the given set.                    |
| [`round()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::round)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | Rounds a given number to the nearest value.                    |
| [`random()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::random)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | Returns a pseudo-random number in the range of 0.0 <= x < 1.0. |

## Mathematical functions

|                                                                                                      |                                                               |
| ---------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
| [`math::abs()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::abs)               | Returns the absolute value of the input.                      |
| [`math::ceil()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::ceil)             | Rounds up a given value to the nearest integer.               |
| [`math::floor()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::floor)           | Rounds down a given value to the nearest integer.             |
| [`math::exp()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::exp)               | Returns the natural logarithm of a given value.               |
| [`math::ln()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::ln)                 | Returns the natural logarithm of a given value.               |
| [`math::lg()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::lg)                 | Returns the base 10 logarithm of a given value.               |
| [`math::log()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::log)               | Returns the logarithm of a given value in the specified base. |
| [`math::mean()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::mean)             | Returns the arithmetic mean of the input set.                 |
| [`math::stddev()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::stddev)         | Returns the sample standard deviation of the input set.       |
| [`math::stddev_pop()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::stddev_pop) | Returns the population standard deviation of the input set.   |
| [`math::var()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::var)               | Returns the sample variance of the input set.                 |
| [`math::var_pop()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::var_pop)       | Returns the population variance of the input set.             |
| [`math::pi()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::pi)                 | Returns the value of pi.                                      |
| [`math::e()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::e)                   | Returns the value of e (euler's number).                      |
| [`math::acos()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::acos)             | Returns the arc cosine of the input.                          |
| [`math::asin()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::asin)             | Returns the arc sine of the input.                            |
| [`math::atan()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::atan)             | Returns the arc tangent of the input.                         |
| [`math::atan2()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::atan2)           | Returns the arc tangent of y / x.                             |
| [`math::cos()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::cos)               | Returns the cosine of the input.                              |
| [`math::cot()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::cot)               | Returns the cotangent of the input.                           |
| [`math::sin()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::sin)               | Returns the sinine of the input.                              |
| [`math::tan()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::tan)               | Returns the tanangent of the input.                           |

## Bitwise functions

|                                                                                                  |                                                       |
| ------------------------------------------------------------------------------------------------ | ----------------------------------------------------- |
| [`bit_and()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::bit_and)       | Bitwise AND operator for 2 integers.                  |
| [`bit_or()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::bit_or)         | Bitwise OR operator for 2 integers.                   |
| [`bit_xor()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::bit_xor)       | Bitwise exclusive OR operator for 2 integers.         |
| [`bit_not()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::bit_not)       | Bitwise negation operator for 2 integers.             |
| [`bit_lshift()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::bit_lshift) | Bitwise left-shift operator for integers.             |
| [`bit_rshift()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::bit_rshift) | Bitwise arithemtic right-shift operator for integers. |
| [`bit_count()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::bit_count)   | Return the number of bits set in the bytes value.     |

## String parsing

|                                                                                                  |                                                       |
| ------------------------------------------------------------------------------------------------ | ----------------------------------------------------- |
| [`to_bigint()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::to_bigint)   | Returns a bigint value parsed from the given string.  |
| [`to_decimal()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::to_decimal) | Returns a decimal value parsed from the given string. |
| [`to_int16()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::to_int16)     | Returns an int16 value parsed from the given input.   |
| [`to_int32()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::to_int32)     | Returns an int32 value parsed from the given input.   |
| [`to_int64()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::to_int64)     | Returns an int64 value parsed from the given input.   |
| [`to_float32()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::to_float32) | Returns a float32 value parsed from the given string. |
| [`to_float64()`](https://docs.geldata.com/reference/stdlib/numbers.md#function::std::to_float64) | Returns a float64 value parsed from the given string. |

It's possible to explicitly [`cast`](https://docs.geldata.com/reference/stdlib/type.md#operator::cast) between all numeric types. All numeric types can also be cast to and from [`str`](https://docs.geldata.com/reference/stdlib/string.md#type::std::str) and [`json`](https://docs.geldata.com/reference/stdlib/json.md#type::std::json).

## Definitions

## Type: int16

```eql
int16
```

A 16-bit signed integer.

`int16` is capable of representing values from `-32768` to `+32767` (inclusive).


## Type: int32

```eql
int32
```

A 32-bit signed integer.

`int32` is capable of representing values from `-2147483648` to `+2147483647` (inclusive).


## Type: int64

```eql
int64
```

A 64-bit signed integer.

`int64` is capable of representing values from `-9223372036854775808` to `+9223372036854775807` (inclusive).


## Type: float32

```eql
float32
```

A variable precision, inexact number.

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


## Type: float64

```eql
float64
```

A variable precision, inexact number.

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


## Type: bigint

```eql
bigint
```

An arbitrary precision integer.

Our philosophy is that use of `bigint` should always be an explicit opt-in and should never be implicit. Once used, these values should not be accidentally cast to a different numerical type that could lead to a loss of precision.

In keeping with this philosophy, [our mathematical functions](https://docs.geldata.com/reference/stdlib/math.md#ref-std-math) are designed to maintain separation between big integer values and the rest of our numeric types.

All of the following types can be explicitly cast into a `bigint` type:

- [`str`](https://docs.geldata.com/reference/stdlib/string.md#type::std::str)
- [`json`](https://docs.geldata.com/reference/stdlib/json.md#type::std::json)
- [`int16`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int16)
- [`int32`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int32)
- [`int64`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int64)
- [`float32`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::float32)
- [`float64`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::float64)
- [`decimal`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::decimal)

A bigint literal is an integer literal, followed by 'n':

```edgeql-repl
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:

```edgeql-repl
db> select 1e+100n is bigint;
{true}
```

When a float literal is followed by `n` it will produce a [`decimal`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::decimal) value instead:

```edgeql-repl
db> select 1.23n is decimal;
{true}

db> select 1.0e+100n is decimal;
{true}
```

> Note: Use caution when casting `bigint` values into [`json`](https://docs.geldata.com/reference/stdlib/json.md#type::std::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-bit 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`](https://docs.geldata.com/reference/stdlib/string.md#type::std::str) and decoding it on the client side into a more appropriate type.


## Type: decimal

```eql
decimal
```

Any number of arbitrary precision.

Our philosophy is that use of `decimal` should always be an explicit opt-in and should never be implicit. Once used, these values should not be accidentally cast to a different numerical type that could lead to a loss of precision.

In keeping with this philosophy, [our mathematical functions](https://docs.geldata.com/reference/stdlib/math.md#ref-std-math) are designed to maintain separation between `decimal` values and the rest of our numeric types.

All of the following types can be explicitly cast into decimal:

- [`str`](https://docs.geldata.com/reference/stdlib/string.md#type::std::str)
- [`json`](https://docs.geldata.com/reference/stdlib/json.md#type::std::json)
- [`int16`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int16)
- [`int32`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int32)
- [`int64`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int64)
- [`float32`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::float32)
- [`float64`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::float64)
- [`bigint`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::bigint)

A decimal literal is a float literal, followed by `n`:

The Gel 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](https://docs.geldata.com/reference/stdlib/math.md#ref-std-math) 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`](https://docs.geldata.com/reference/stdlib/string.md#type::std::str), [`json`](https://docs.geldata.com/reference/stdlib/json.md#type::std::json), [`int16`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int16), [`int32`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int32), [`int64`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int64), [`float32`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::float32), [`float64`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::float64), and [`bigint`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::bigint).

A decimal literal is a float literal followed by 'n':

```edgeql-repl
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`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::bigint) value. A literal without a dot and with a positive exponent makes a [`bigint`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::bigint), too:

```edgeql-repl
db> select 42n is bigint;
{true}

db> select 12e+34n is bigint;
{true}
```

> Note: Use caution when casting `decimal` values into [`json`](https://docs.geldata.com/reference/stdlib/json.md#type::std::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 a [`str`](https://docs.geldata.com/reference/stdlib/string.md#type::std::str) and decoding it on the client side into a more appropriate type.


## Operator: anyreal + anyreal

```eql
anyreal + anyreal -> anyreal
```

Arithmetic addition.

```edgeql-repl
db> select 2 + 2;
{4}
```


## Operator: anyreal - anyreal

```eql
anyreal - anyreal -> anyreal
```

Arithmetic subtraction.

```edgeql-repl
db> select 3 - 2;
{1}
```


## Operator: -anyreal

```eql
- anyreal -> anyreal
```

Arithmetic negation.

```edgeql-repl
db> select -5;
{-5}
```


## Operator: anyreal * anyreal

```eql
anyreal * anyreal -> anyreal
```

Arithmetic multiplication.

```edgeql-repl
db> select 2 * 10;
{20}
```


## Operator: anyreal / anyreal

```eql
anyreal / anyreal -> anyreal
```

Arithmetic division.

```edgeql-repl
db> select 10 / 4;
{2.5}
```

Division by zero will result in an error:

```edgeql-repl
db> select 10 / 0;
DivisionByZeroError: division by zero
```


## Operator: anyreal // anyreal

```eql
anyreal // anyreal -> anyreal
```

Floor division.

In floor-based division, the result of a standard division operation is rounded down to its nearest integer. It is the equivalent to using regular division and then applying [`math::floor()`](https://docs.geldata.com/reference/stdlib/math.md#function::math::floor) to the result.

```edgeql-repl
db> select 10 // 4;
{2}
db> select math::floor(10 / 4);
{2}
db> select -10 // 4;
{-3}
```

It also works on [`float`](https://docs.geldata.com/reference/stdlib/abstract.md#type::std::anyfloat), [`bigint`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::bigint), and [`decimal`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::decimal) types. The type of the result corresponds to the type of the operands:

```edgeql-repl
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 [`%`](https://docs.geldata.com/reference/stdlib/numbers.md#operator::mod) operations are related in the following way: `A // B = (A - (A % B)) / B`.


## Operator: anyreal % anyreal

```eql
anyreal % anyreal -> anyreal
```

Remainder from division (modulo).

This is commonly referred to as a "modulo" operation.

This is the remainder from floor division. Just as is the case with [`//`](https://docs.geldata.com/reference/stdlib/numbers.md#operator::floordiv) the result type of the remainder operator corresponds to the operand type:

```edgeql-repl
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, [`//`](https://docs.geldata.com/reference/stdlib/numbers.md#operator::floordiv) and [`%`](https://docs.geldata.com/reference/stdlib/numbers.md#operator::mod) operations are related in the following way: `A // B = (A - (A % B)) / B`.

Modulo division by zero will result in an error:

```edgeql-repl
db> select 10 % 0;
DivisionByZeroError: division by zero
```


## Operator: anyreal ^ anyreal

```eql
anyreal ^ anyreal -> anyreal
```

Power operation.

```edgeql-repl
db> select 2 ^ 4;
{16}
```


## Function: round()

```eql
std::round(value: int64) -> float64
std::round(value: float64) -> float64
std::round(value: bigint) -> bigint
std::round(value: decimal) -> decimal
std::round(value: decimal, d: int64) -> decimal
```

Rounds a given number to the nearest value.

The function will round a `.5` value differently depending on the type of the parameter passed.

The [`float64`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::float64) tie is rounded to the nearest even number:

```edgeql-repl
db> select round(1.2);
{1}

db> select round(1.5);
{2}

db> select round(2.5);
{2}
```

But the [`decimal`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::decimal) tie is rounded away from zero:

```edgeql-repl
db> select round(1.2n);
{1n}

db> select round(1.5n);
{2n}

db> select round(2.5n);
{3n}
```

Additionally, when rounding a [`decimal`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::decimal) value, you may pass the optional argument *d* to specify the precision of the rounded result:

```edgeql-repl
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}
```


## Function: random()

```eql
std::random() -> float64
```

Returns a pseudo-random number in the range of `0.0 <= x < 1.0`.

```edgeql-repl
db> select random();
{0.62649393780157}
```


## Function: bit_and()

```eql
std::bit_and(l: int16, r: int16) -> int16
std::bit_and(l: int32, r: int32) -> int32
std::bit_and(l: int64, r: int64) -> int64
```

Bitwise AND operator for 2 integers.

```edgeql-repl
db> select bit_and(17, 3);
{1}
```


## Function: bit_or()

```eql
std::bit_or(l: int16, r: int16) -> int16
std::bit_or(l: int32, r: int32) -> int32
std::bit_or(l: int64, r: int64) -> int64
```

Bitwise OR operator for 2 integers.

```edgeql-repl
db> select bit_or(17, 3);
{19}
```


## Function: bit_xor()

```eql
std::bit_xor(l: int16, r: int16) -> int16
std::bit_xor(l: int32, r: int32) -> int32
std::bit_xor(l: int64, r: int64) -> int64
```

Bitwise exclusive OR operator for 2 integers.

```edgeql-repl
db> select bit_xor(17, 3);
{18}
```


## Function: bit_not()

```eql
std::bit_not(r: int16) -> int16
std::bit_not(r: int32) -> int32
std::bit_not(r: int64) -> int64
```

Bitwise negation operator for 2 integers.

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

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


## Function: bit_lshift()

```eql
std::bit_lshift(val: int16, n: int64) -> int16
std::bit_lshift(val: int32, n: int64) -> int32
std::bit_lshift(val: int64, n: int64) -> int64
```

Bitwise left-shift operator for integers.

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 greater than the bit size of the integer results in `0`.

```edgeql-repl
db> select bit_lshift(123, 2);
{492}
db> select bit_lshift(123, 65);
{0}
```

Left-shifting an integer can change the sign bit:

```edgeql-repl
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:

```edgeql-repl
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:

```edgeql-repl
db> select bit_lshift(123, -2);
gel error: InvalidValueError: bit_lshift(): cannot shift by
negative amount
```


## Function: bit_rshift()

```eql
std::bit_rshift(val: int16, n: int64) -> int16
std::bit_rshift(val: int32, n: int64) -> int32
std::bit_rshift(val: int64, n: int64) -> int64
```

Bitwise arithemtic right-shift operator for integers.

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 greater than the bit size of the integer results in `0` for positive numbers or `-1` for negative numbers.

```edgeql-repl
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:

```edgeql-repl
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:

```edgeql-repl
db> select bit_rshift(123, -2);
gel error: InvalidValueError: bit_rshift(): cannot shift by
negative amount
```


## Function: bit_count()

```eql
std::bit_count(val: int16) -> int64
std::bit_count(val: int32) -> int64
std::bit_count(val: int64) -> int64
std::bit_count(bytes: bytes) -> int64
```

Return the number of bits set in the [`bytes`](https://docs.geldata.com/reference/stdlib/bytes.md#type::std::bytes) value.

This is also known as the population count.

```edgeql-repl
db> select bit_count(255);
{8}
db> select bit_count(b'\xff\xff');
{16}
```


## Function: to_bigint()

```eql
std::to_bigint(s: str, fmt: optional str={}) -> bigint
```

Returns a [`bigint`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::bigint) value parsed from the given string.

The function will use an optional format string passed as *fmt*. See the [number formatting options](https://docs.geldata.com/reference/stdlib/string.md#ref-std-converters-number-fmt) for help writing a format string.

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


## Function: to_decimal()

```eql
std::to_decimal(s: str, fmt: optional str={}) -> decimal
```

Returns a [`decimal`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::decimal) value parsed from the given string.

The function will use an optional format string passed as *fmt*. See the [number formatting options](https://docs.geldata.com/reference/stdlib/string.md#ref-std-converters-number-fmt) for help writing a format string.

```edgeql-repl
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}
```


## Function: to_int16()

```eql
std::to_int16(s: str, fmt: optional str={}) -> int16
std::to_int16(val: bytes, endian: Endian) -> int16
```

Returns an [`int16`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int16) value parsed from the given input.

The string parsing function will use an optional format string passed as *fmt*. See the [number formatting options](https://docs.geldata.com/reference/stdlib/string.md#ref-std-converters-number-fmt) for help writing a format string.

```edgeql-repl
db> select to_int16('23');
{23}
db> select to_int16('23%', '99%');
{23}
```

The bytes conversion function expects exactly 2 bytes with specified endianness.

```edgeql-repl
db> select to_int16(b'\x00\x07', Endian.Big);
{7}
db> select to_int16(b'\x07\x00', Endian.Little);
{7}
```

> Note: Due to underlying implementation details using big-endian encoding results in slightly faster performance of `to_int16`.


## Function: to_int32()

```eql
std::to_int32(s: str, fmt: optional str={}) -> int32
std::to_int32(val: bytes, endian: Endian) -> int32
```

Returns an [`int32`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int32) value parsed from the given input.

The string parsin function will use an optional format string passed as *fmt*. See the [number formatting options](https://docs.geldata.com/reference/stdlib/string.md#ref-std-converters-number-fmt) for help writing a format string.

```edgeql-repl
db> select to_int32('1000023');
{1000023}
db> select to_int32('1000023%', '9999999%');
{1000023}
```

The bytes conversion function expects exactly 4 bytes with specified endianness.

```edgeql-repl
db> select to_int32(b'\x01\x02\x00\x07', Endian.Big);
{16908295}
db> select to_int32(b'\x07\x00\x02\x01', Endian.Little);
{16908295}
```

> Note: Due to underlying implementation details using big-endian encoding results in slightly faster performance of `to_int32`.


## Function: to_int64()

```eql
std::to_int64(s: str, fmt: optional str={}) -> int64
std::to_int64(val: bytes, endian: Endian) -> int64
```

Returns an [`int64`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::int64) value parsed from the given input.

The string parsing function will use an optional format string passed as *fmt*. See the [number formatting options](https://docs.geldata.com/reference/stdlib/string.md#ref-std-converters-number-fmt) for help writing a format string.

```edgeql-repl
db> select to_int64('10000234567');
{10000234567}
db> select to_int64('10000234567%', '99999999999%');
{10000234567}
```

The bytes conversion function expects exactly 8 bytes with specified endianness.

```edgeql-repl
db> select to_int64(b'\x01\x02\x00\x07\x11\x22\x33\x44',
...                 Endian.Big);
{72620574343574340}
db> select to_int64(b'\x44\x33\x22\x11\x07\x00\x02\x01',
...                 Endian.Little);
{72620574343574340}
```

> Note: Due to underlying implementation details using big-endian encoding results in slightly faster performance of `to_int64`.


## Function: to_float32()

```eql
std::to_float32(s: str, fmt: optional str={}) -> float32
```

Returns a [`float32`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::float32) value parsed from the given string.

The function will use an optional format string passed as *fmt*. See the [number formatting options](https://docs.geldata.com/reference/stdlib/string.md#ref-std-converters-number-fmt) for help writing a format string.


## Function: to_float64()

```eql
std::to_float64(s: str, fmt: optional str={}) -> float64
```

Returns a [`float64`](https://docs.geldata.com/reference/stdlib/numbers.md#type::std::float64) value parsed from the given string.

The function will use an optional format string passed as *fmt*. See the [number formatting options](https://docs.geldata.com/reference/stdlib/string.md#ref-std-converters-number-fmt) for help writing a format string.


