Numbers

Construction

Example

Type

Description

123

int

123L

long

12345678901

long

long since out of range for int.

0.0

double

0.0d

double

0.0f

float

NaN

double

Not a number

NaN(double)

double

Not a number

NaN(float)

float

Not a number

Inf

double

Going into infinity in the positive direction.

+Inf

double

Same as Inf.

-Inf

double

Going into infinity in the negative direction

The remaining number types can be constructed with:

Type

tinyint( <number, string or bool>)
byte (<number, string or bool>)

byte

smallint (<number, string or bool >)
short( <number, string or bool >)

short

integer( <number, string or bool >)
int(<number, string or bool >)

int

bigint(<number, string or bool>)
long(<number, string or bool>)

long

real(<number, string or bool>)
float(<number, string or bool>)

float

double(<number, string or bool>)

double

numeric(<number, string or bool>)
decimal(<number, string or bool>)

decimal

Note

To construct a decimal without precision loss, convert from a string as in decimal("12345.123"). The remaining constructors may lead to loss of precision if the destination type is less precise than the source.

All operations described implicitly convert numbers to the most precise type following the type hierarchy:

byte < short < int < long < float < double < decimal.

Note

Overflows are not checked by default. To protect against overflow, either choose an appropriate type that is large enough to accommodate the operation or use the overflow-safe version of the operators, which always checks for overflows.

Addition

Sum two numbers.

Syntax:

<number> + <number>

For example:

1 + 2 // 3

inf + inf // inf

-inf + -inf // -inf

1 + null // null
null + 1 // null

1 + inf // inf
inf + 1 // inf

1 + -inf // -inf
-inf + 1 // -inf

null + inf // null
inf + null // null

null + -inf // null
-inf + null // null

inf + -inf // nan
-inf + inf // nan

1 + nan // nan
nan + 1 // nan

Subtraction

Subtract two numbers.

Syntax:

<number> - <number>

For example:

1 - 2 // -1

inf - inf // nan

-inf - -inf // nan

1 - null // null
null - 1 // null

1 - inf // -inf
inf - 1 // inf

1 - -inf // inf
-inf - 1 // -inf

null - inf // null
inf - null // null

null - -inf // null
-inf - null // null

inf - -inf // inf
-inf - inf // -inf

1 - nan // nan
nan - 1 // nan

Multiplication

Multiply two numbers.

Syntax:

<number> * <number>

For example:

1 * 2 // 2

inf * inf // inf

-inf * -inf // inf

1 * null // null
null * 1 // null

1 * inf // inf
inf * 1 // inf
0 * inf // nan
inf * 0 // nan

1 * -inf // -inf
-inf * 1 // -inf
0 * -inf // nan
-inf * 0 // nan

null * inf // null
inf * null // null

null * -inf // null
-inf * null // null

inf * -inf // -inf
-inf * inf // -inf

1 * nan // nan
nan * 1 // nan

Division

Divide two numbers.

Syntax:

<number> / <number>

For example:

1 / 2 // 1
1.0 / 2 // 0.5
1 / 0 // nan

inf / inf // nan

-inf / -inf // nan

1 / null // null
null / 1 // null

1 / inf // 0.0
inf / 1 // inf

1 / -inf // -0.0
-inf / 1 // -inf

null / inf // null
inf / null // null

null / -inf // null
-inf / null // null

inf / -inf // nan
-inf / inf // nan

1 / nan // nan
nan / 1 // nan

Modulo

Finds the remainder of the division of a number by another.

Syntax:

<number> % <number>

For example:

1 % 2 // 1

inf % inf // nan

-inf % -inf // nan

1 % null // null
null % 1 // null

1 % inf // 1.0
inf % 1 // nan

1 % -inf // -inf
-inf % 1 // nan

null % inf // null
inf % null // null

null % -inf // null
-inf % null // null

inf % -inf // nan
-inf % inf // nan

1 % nan // nan
nan % 1 // nan

Negation

Negate a number.

Syntax:

- <expression>

For example:

-(1+1) // -2

Comparisons

Equal

Checks if two numbers are equal.

Syntax:

<number> = <number>
<number> == <number>

For example:

1 = 1 // true
1 == 1 // Same as 1 = 1

null = null // null

inf = inf // true

-inf = -inf // true

1 = null // null
null = 1 // null

1 = inf // false
inf = 1 // false

1 = -inf // false
-inf = 1 // false

null = inf // null
inf = null // null

null = -inf // null
-inf = null // null

inf = -inf // false
-inf = inf // false

<number or nan> = nan // false
nan = <number or nan> // false

To know if an expression is NaN (not a number), it is not possible to compare it with equality, since the result is always false. Instead use:

nan IS NAN // true
ISNAN(nan) // Same as nan IS NAN
1 IS NAN // false

Not equal

Checks if two numbers are not equal.

Syntax:

<number> <> <number>
<number> != <number>

For example:

1 <> 2 // false
1 != 2 // Same as 1 <> 2

null <> null // null

inf <> inf // false

-inf <> -inf // false

1 <> null // null
null <> 1 // null

1 <> inf // true
inf <> 1 // true

1 <> -inf // true
-inf <> 1 // true

null <> inf // null
inf <> null // null

null <> -inf // null
-inf <> null // null

inf <> -inf // true
-inf <> inf // true

<number or nan> <> nan // true
nan <> <number or nan> // true

Inequality

Check if a number if greater than (>), greater or equal than (>=), less than (<), less or equal than (<=) another number.

Syntax:

<number> > <number>
<number> >= <number>
<number> < <number>
<number> <= <number>

For example:

2 > 2 // false
2 >= 2 // true
1 < 2 // true
1 <= 2 // true

null < null // null
null > null // null

inf < inf // false
inf > inf // false

-inf < -inf // false
-inf > -inf // false

1 > null // null
null > 1 // null

1 > inf // false
inf > 1 // true

1 > -inf // true
-inf > 1 // false

null > inf // null
inf > null // null

null > -inf // null
-inf > null // null

inf > -inf // true
inf < -inf // false
-inf > inf // false
-inf < inf // true

Overflow protection

Addition, subtraction and multiplication binary operators, and the negation unary operator, have variants with overflow protection. To use these, place the suffix ? in the operator.

Syntax:

<number> +? <number>
<number> -? <number>
<number> *? <number>
-? <number>

For example:

1 +? 1
1 -? 9
-? 12
145 *? 999

The operation returns null if an overflow occurred or the value if it succeeded.

Attention

Overflow protection operators are not currently implemented.

Casts

Number types can be casted to:

  • other number types, which may result in loss of precision if the final type is less precise than the original type;

  • boolean, where zero becomes false whilst every other number is true;

  • string, which creates a string representation of the number.

Casting nan to a natural number (int, long, byte, short) will result in 0.

Casting inf to int or long will result in the maximum value of the respective type. Similarly, casting -inf to int or long will result in the minimum value of the respective type.

Casting inf and -inf to short or byte will result in an overflow.