Arithmetic Operators and Numeric Converter Functions

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.

anyreal = anyreal, anyreal < anyreal, …

Comparison operators.

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.

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