15

What is the modulus operator in Nim?

tile % 9 == 0 results in undeclared identifier: '%'

Googling or searching SO doesn't bring up an answer.

Hugues M.
  • 19,846
  • 6
  • 37
  • 65
Lex
  • 4,749
  • 3
  • 45
  • 66
  • 1
    [manual](https://nim-lang.org/docs/manual.html), Ctrl-F, modulo: "`a %% b`: unsigned integer modulo operation" :) There's also a `mod` operator (see under Operators). – Amadan Sep 25 '18 at 06:17
  • Ahhh thank you! I was looking at these operators https://nim-lang.org/docs/manual.html#lexical-analysis-operators which appeared `%` was valid. Looking closer now I see the is also `mod` – Lex Sep 25 '18 at 06:21
  • @paxdiablo I think Google was stemming `modulus` to `modulo` and returning the manual page, but in my impatience I couldn't find modulus on the page and I left without looking for modulo. – Lex Sep 25 '18 at 22:50

2 Answers2

20

Others have suggested using %%, but don't do that. It is a remnant of a time when Nim used to have only signed integers. The operators ending with % like <% are used to handle these signed integers as unsigned ints. Since Nim has had unsigned integers for a while now, simply use the mod operator that is correctly overloaded for all relevant integral types: https://nim-lang.org/docs/system.html#mod,int,int

def-
  • 5,275
  • 20
  • 18
9

You can use the modulus operator with the mod keyword like this:

tile mod 9 == 0
Snackys
  • 101
  • 1
  • 5