Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 846
Lex Avatar asked Sep 25 '18 06:09

Lex


People also ask

What the modulus operator (%) does?

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.

Is there a modulus operator in C++?

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.

Does modulus work with integers?

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.


2 Answers

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

like image 100
def- Avatar answered Oct 21 '22 12:10

def-


You can use the modulus operator like this:

tile mod 9 == 0
like image 7
Snackys Avatar answered Oct 21 '22 13:10

Snackys