Sage-Code Laboratory
index<--

Nim Types

Nim is a strongly typed native language. That requires for all variables to be declared with a data type that can't be changed during runtime. This gives Nim compiler an opportunity to detect errors early and improve performance of algorithms.

Integer

Integer are discrete numbers including negative numbers. In Computer Science, integer namber have a range limitation and does not support infinity. The range depend on number of bytes used for representation of the number. In all programming languages, the number of bytes is fixed.

example
proc integer_demo: int =
  let
    a = 11
    b = 4
  
  echo "a + b = ", a + b 
  echo "a - b = ", a - b
  echo "a * b = ", a * b
  echo "a / b = ", a / b
  echo "a div b = ", a div b
  echo "a mod b = ", a mod b
  return 0
console
a + b = 15
a - b = 7
a * b = 44
a / b = 2.75
a div b = 2
a mod b = 3

Note: Integer literals are simple including digits and sign like in example above. The most intresting is the division that can produce a float result, unlike other languages.

Real numbers

Real numbers can't be represented in Computer Science. A good aproximation of Real Numbers is implemented as Float Numbers. These are natively supported by computer procesors and standard for most programming languages.

example
let
  c = 6.75
  d = 2.25

echo "c + d = ", c + d
echo "c - d = ", c - d
echo "c * d = ", c * d
echo "c / d = ", c / d
console
c + d = 9.0  
c - d = 4.5
c * d = 15.1875
c / d = 3.0 

Notes: Integer literal 9 is not the same as literal 9.0 that is float. In Nim you can't use integers and float in the same expression. You must make explicit conversion before computing an expression.


Read next: Selection