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

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.

## Operations

## Comparisons

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

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