What is the modulus operator in Nim?
tile % 9 == 0
results in undeclared identifier: '%'
Googling or searching SO doesn't bring up an answer.
The modulo operator, denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division. produces the remainder when x is divided by y.
C++ provides the modulus operator, %, that yields the remainder after integer division. The modulus operator can be used only with integer operands. The expression x % y yields the remainder after x is divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2.
In integer division and modulus, the dividend is divided by the divisor into an integer quotient and a remainder. The integer quotient operation is referred to as integer division, and the integer remainder operation is the modulus.
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
You can use the modulus operator like this:
tile mod 9 == 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With