Modulus operator nim
Asked Answered
M

2

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.

Mongo answered 25/9, 2018 at 6:14 Comment(3)
manual, Ctrl-F, modulo: "a %% b: unsigned integer modulo operation" :) There's also a mod operator (see under Operators).Hereford
Ahhh thank you! I was looking at these operators nim-lang.org/docs/manual.html#lexical-analysis-operators which appeared % was valid. Looking closer now I see the is also modMongo
@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.Mongo
F
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

Fanchon answered 25/9, 2018 at 6:51 Comment(0)
M
9

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

tile mod 9 == 0
Macmullin answered 20/10, 2019 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.