Nim has several primitive types:
- signed integers:
intis the same size as a pointer
- unsigned integers are similar with
uprepended to the type
- floating points numbers:
floatis the processor’s fastest type
char, which is basically an alias for uint8
To indicate the size of an integer literal, append
i and the size you’d like to the end. However, usually this is not necessary.
Integers can also have
0[Bb] prepended to indicate a hex, octal, or binary literal, respectively. Underscores are also valid in literals, and can help with readability.
let a: int8 = 0x7F # Works b: uint8 = 0b1111_1111 # Works d = 0xFF # type is int c: uint8 = 256 # Compile time error
Precedence rules are the same as in most other languages, but instead of
shl operators are used, respectively.
let a: int = 2 b: int = 4 echo 4/2
$ nim c -r numbers2.nim 2.0
Another difference that may be surprising is that the
/ operator returns a floating point result, even when the operands are integers. If integer division is needed, the
div operator should be used.