Lex
Lex

Reputation: 5014

Modulus operator nim

What is the modulus operator in Nim?

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

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

Upvotes: 15

Views: 5046

Answers (2)

Snackys
Snackys

Reputation: 101

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

tile mod 9 == 0

Upvotes: 9

def-
def-

Reputation: 5393

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

Upvotes: 20

Related Questions